| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
package org.acegisecurity.afterinvocation; |
| 16 |
|
|
| 17 |
|
import org.acegisecurity.AccessDeniedException; |
| 18 |
|
import org.acegisecurity.Authentication; |
| 19 |
|
import org.acegisecurity.AuthorizationServiceException; |
| 20 |
|
import org.acegisecurity.ConfigAttribute; |
| 21 |
|
import org.acegisecurity.ConfigAttributeDefinition; |
| 22 |
|
|
| 23 |
|
import org.acegisecurity.acl.AclEntry; |
| 24 |
|
import org.acegisecurity.acl.AclManager; |
| 25 |
|
import org.acegisecurity.acl.basic.BasicAclEntry; |
| 26 |
|
import org.acegisecurity.acl.basic.SimpleAclEntry; |
| 27 |
|
|
| 28 |
|
import org.apache.commons.logging.Log; |
| 29 |
|
import org.apache.commons.logging.LogFactory; |
| 30 |
|
|
| 31 |
|
import org.springframework.beans.factory.InitializingBean; |
| 32 |
|
|
| 33 |
|
import org.springframework.util.Assert; |
| 34 |
|
|
| 35 |
|
import java.util.Collection; |
| 36 |
|
import java.util.Iterator; |
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
12 |
public class BasicAclEntryAfterInvocationCollectionFilteringProvider implements AfterInvocationProvider, |
| 70 |
|
InitializingBean { |
| 71 |
|
|
| 72 |
|
|
| 73 |
3 |
protected static final Log logger = LogFactory.getLog(BasicAclEntryAfterInvocationCollectionFilteringProvider.class); |
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
private AclManager aclManager; |
| 78 |
12 |
private Class processDomainObjectClass = Object.class; |
| 79 |
12 |
private String processConfigAttribute = "AFTER_ACL_COLLECTION_READ"; |
| 80 |
12 |
private int[] requirePermission = {SimpleAclEntry.READ}; |
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| 84 |
|
public void afterPropertiesSet() throws Exception { |
| 85 |
11 |
Assert.notNull(processConfigAttribute, "A processConfigAttribute is mandatory"); |
| 86 |
10 |
Assert.notNull(aclManager, "An aclManager is mandatory"); |
| 87 |
|
|
| 88 |
9 |
if ((requirePermission == null) || (requirePermission.length == 0)) { |
| 89 |
1 |
throw new IllegalArgumentException("One or more requirePermission entries is mandatory"); |
| 90 |
|
} |
| 91 |
8 |
} |
| 92 |
|
|
| 93 |
|
public Object decide(Authentication authentication, Object object, ConfigAttributeDefinition config, |
| 94 |
|
Object returnedObject) throws AccessDeniedException { |
| 95 |
9 |
Iterator iter = config.getConfigAttributes(); |
| 96 |
|
|
| 97 |
11 |
while (iter.hasNext()) { |
| 98 |
10 |
ConfigAttribute attr = (ConfigAttribute) iter.next(); |
| 99 |
|
|
| 100 |
10 |
if (this.supports(attr)) { |
| 101 |
|
|
| 102 |
8 |
if (returnedObject == null) { |
| 103 |
1 |
if (logger.isDebugEnabled()) { |
| 104 |
0 |
logger.debug("Return object is null, skipping"); |
| 105 |
|
} |
| 106 |
|
|
| 107 |
1 |
return null; |
| 108 |
|
} |
| 109 |
|
|
| 110 |
7 |
Filterer filterer = null; |
| 111 |
|
|
| 112 |
7 |
if (returnedObject instanceof Collection) { |
| 113 |
5 |
Collection collection = (Collection) returnedObject; |
| 114 |
5 |
filterer = new CollectionFilterer(collection); |
| 115 |
5 |
} else if (returnedObject.getClass().isArray()) { |
| 116 |
1 |
Object[] array = (Object[]) returnedObject; |
| 117 |
1 |
filterer = new ArrayFilterer(array); |
| 118 |
1 |
} else { |
| 119 |
1 |
throw new AuthorizationServiceException("A Collection or an array (or null) was required as the " |
| 120 |
|
+ "returnedObject, but the returnedObject was: " + returnedObject); |
| 121 |
|
} |
| 122 |
|
|
| 123 |
|
|
| 124 |
6 |
Iterator collectionIter = filterer.iterator(); |
| 125 |
|
|
| 126 |
30 |
while (collectionIter.hasNext()) { |
| 127 |
24 |
Object domainObject = collectionIter.next(); |
| 128 |
|
|
| 129 |
24 |
boolean hasPermission = false; |
| 130 |
|
|
| 131 |
24 |
if (domainObject == null) { |
| 132 |
0 |
hasPermission = true; |
| 133 |
24 |
} else if (!processDomainObjectClass.isAssignableFrom(domainObject.getClass())) { |
| 134 |
0 |
hasPermission = true; |
| 135 |
|
} else { |
| 136 |
24 |
AclEntry[] acls = aclManager.getAcls(domainObject, authentication); |
| 137 |
|
|
| 138 |
24 |
if ((acls != null) && (acls.length != 0)) { |
| 139 |
18 |
for (int i = 0; i < acls.length; i++) { |
| 140 |
|
|
| 141 |
13 |
if (acls[i] instanceof BasicAclEntry) { |
| 142 |
9 |
BasicAclEntry processableAcl = (BasicAclEntry) acls[i]; |
| 143 |
|
|
| 144 |
|
|
| 145 |
18 |
for (int y = 0; y < requirePermission.length; y++) { |
| 146 |
9 |
if (processableAcl.isPermitted(requirePermission[y])) { |
| 147 |
4 |
hasPermission = true; |
| 148 |
|
|
| 149 |
4 |
if (logger.isDebugEnabled()) { |
| 150 |
0 |
logger.debug("Principal is authorised for element: " + domainObject |
| 151 |
|
+ " due to ACL: " + processableAcl.toString()); |
| 152 |
|
} |
| 153 |
|
} |
| 154 |
|
} |
| 155 |
|
} |
| 156 |
|
} |
| 157 |
|
} |
| 158 |
|
|
| 159 |
24 |
if (!hasPermission) { |
| 160 |
20 |
filterer.remove(domainObject); |
| 161 |
|
|
| 162 |
20 |
if (logger.isDebugEnabled()) { |
| 163 |
0 |
logger.debug("Principal is NOT authorised for element: " + domainObject); |
| 164 |
|
} |
| 165 |
|
} |
| 166 |
|
} |
| 167 |
24 |
} |
| 168 |
|
|
| 169 |
6 |
return filterer.getFilteredObject(); |
| 170 |
|
} |
| 171 |
2 |
} |
| 172 |
|
|
| 173 |
1 |
return returnedObject; |
| 174 |
|
} |
| 175 |
|
|
| 176 |
|
public AclManager getAclManager() { |
| 177 |
2 |
return aclManager; |
| 178 |
|
} |
| 179 |
|
|
| 180 |
|
public String getProcessConfigAttribute() { |
| 181 |
12 |
return processConfigAttribute; |
| 182 |
|
} |
| 183 |
|
|
| 184 |
|
public int[] getRequirePermission() { |
| 185 |
2 |
return requirePermission; |
| 186 |
|
} |
| 187 |
|
|
| 188 |
|
public void setAclManager(AclManager aclManager) { |
| 189 |
10 |
this.aclManager = aclManager; |
| 190 |
10 |
} |
| 191 |
|
|
| 192 |
|
public void setProcessConfigAttribute(String processConfigAttribute) { |
| 193 |
2 |
this.processConfigAttribute = processConfigAttribute; |
| 194 |
2 |
} |
| 195 |
|
|
| 196 |
|
public void setProcessDomainObjectClass(Class processDomainObjectClass) { |
| 197 |
0 |
Assert.notNull(processDomainObjectClass, "processDomainObjectClass cannot be set to null"); |
| 198 |
0 |
this.processDomainObjectClass = processDomainObjectClass; |
| 199 |
0 |
} |
| 200 |
|
|
| 201 |
|
public void setRequirePermission(int[] requirePermission) { |
| 202 |
2 |
this.requirePermission = requirePermission; |
| 203 |
2 |
} |
| 204 |
|
|
| 205 |
|
|
| 206 |
|
|
| 207 |
|
|
| 208 |
|
|
| 209 |
|
|
| 210 |
|
|
| 211 |
|
|
| 212 |
|
|
| 213 |
|
public void setRequirePermissionFromString(String[] requiredPermissions) { |
| 214 |
0 |
setRequirePermission(SimpleAclEntry.parsePermissions(requiredPermissions)); |
| 215 |
0 |
} |
| 216 |
|
|
| 217 |
|
public boolean supports(ConfigAttribute attribute) { |
| 218 |
10 |
if ((attribute.getAttribute() != null) && attribute.getAttribute().equals(getProcessConfigAttribute())) { |
| 219 |
8 |
return true; |
| 220 |
|
} else { |
| 221 |
2 |
return false; |
| 222 |
|
} |
| 223 |
|
} |
| 224 |
|
|
| 225 |
|
|
| 226 |
|
|
| 227 |
|
|
| 228 |
|
|
| 229 |
|
|
| 230 |
|
|
| 231 |
|
|
| 232 |
|
public boolean supports(Class clazz) { |
| 233 |
1 |
return true; |
| 234 |
|
} |
| 235 |
|
} |