|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| package org.acegisecurity.afterinvocation; |
|
17 |
| |
|
18 |
| import org.acegisecurity.AccessDeniedException; |
|
19 |
| import org.acegisecurity.Authentication; |
|
20 |
| import org.acegisecurity.AuthorizationServiceException; |
|
21 |
| import org.acegisecurity.ConfigAttribute; |
|
22 |
| import org.acegisecurity.ConfigAttributeDefinition; |
|
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.collections.iterators.ArrayIterator; |
|
29 |
| import org.apache.commons.logging.Log; |
|
30 |
| import org.apache.commons.logging.LogFactory; |
|
31 |
| |
|
32 |
| import org.springframework.beans.factory.InitializingBean; |
|
33 |
| |
|
34 |
| import org.springframework.util.Assert; |
|
35 |
| |
|
36 |
| import java.lang.reflect.Array; |
|
37 |
| |
|
38 |
| import java.util.Collection; |
|
39 |
| import java.util.HashSet; |
|
40 |
| import java.util.Iterator; |
|
41 |
| import java.util.Set; |
|
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 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
| public class BasicAclEntryAfterInvocationCollectionFilteringProvider |
|
106 |
| implements AfterInvocationProvider, InitializingBean { |
|
107 |
| |
|
108 |
| |
|
109 |
| protected static final Log logger = LogFactory.getLog(BasicAclEntryAfterInvocationCollectionFilteringProvider.class); |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| private AclManager aclManager; |
|
114 |
| private String processConfigAttribute = "AFTER_ACL_COLLECTION_READ"; |
|
115 |
| private int[] requirePermission = {SimpleAclEntry.READ}; |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
10
| public void setAclManager(AclManager aclManager) {
|
|
120 |
10
| this.aclManager = aclManager;
|
|
121 |
| } |
|
122 |
| |
|
123 |
2
| public AclManager getAclManager() {
|
|
124 |
2
| return aclManager;
|
|
125 |
| } |
|
126 |
| |
|
127 |
2
| public void setProcessConfigAttribute(String processConfigAttribute) {
|
|
128 |
2
| this.processConfigAttribute = processConfigAttribute;
|
|
129 |
| } |
|
130 |
| |
|
131 |
12
| public String getProcessConfigAttribute() {
|
|
132 |
12
| return processConfigAttribute;
|
|
133 |
| } |
|
134 |
| |
|
135 |
2
| public void setRequirePermission(int[] requirePermission) {
|
|
136 |
2
| this.requirePermission = requirePermission;
|
|
137 |
| } |
|
138 |
| |
|
139 |
2
| public int[] getRequirePermission() {
|
|
140 |
2
| return requirePermission;
|
|
141 |
| } |
|
142 |
| |
|
143 |
11
| public void afterPropertiesSet() throws Exception {
|
|
144 |
11
| Assert.notNull(processConfigAttribute,
|
|
145 |
| "A processConfigAttribute is mandatory"); |
|
146 |
10
| Assert.notNull(aclManager, "An aclManager is mandatory");
|
|
147 |
| |
|
148 |
9
| if ((requirePermission == null) || (requirePermission.length == 0)) {
|
|
149 |
1
| throw new IllegalArgumentException(
|
|
150 |
| "One or more requirePermission entries is mandatory"); |
|
151 |
| } |
|
152 |
| } |
|
153 |
| |
|
154 |
9
| public Object decide(Authentication authentication, Object object,
|
|
155 |
| ConfigAttributeDefinition config, Object returnedObject) |
|
156 |
| throws AccessDeniedException { |
|
157 |
9
| Iterator iter = config.getConfigAttributes();
|
|
158 |
| |
|
159 |
9
| while (iter.hasNext()) {
|
|
160 |
10
| ConfigAttribute attr = (ConfigAttribute) iter.next();
|
|
161 |
| |
|
162 |
10
| if (this.supports(attr)) {
|
|
163 |
| |
|
164 |
8
| if (returnedObject == null) {
|
|
165 |
1
| if (logger.isDebugEnabled()) {
|
|
166 |
0
| logger.debug("Return object is null, skipping");
|
|
167 |
| } |
|
168 |
| |
|
169 |
1
| return null;
|
|
170 |
| } |
|
171 |
| |
|
172 |
7
| Filterer filterer = null;
|
|
173 |
| |
|
174 |
7
| if (returnedObject instanceof Collection) {
|
|
175 |
5
| Collection collection = (Collection) returnedObject;
|
|
176 |
5
| filterer = new CollectionFilterer(collection);
|
|
177 |
2
| } else if (returnedObject.getClass().isArray()) {
|
|
178 |
1
| Object[] array = (Object[]) returnedObject;
|
|
179 |
1
| filterer = new ArrayFilterer(array);
|
|
180 |
| } else { |
|
181 |
1
| throw new AuthorizationServiceException(
|
|
182 |
| "A Collection or an array (or null) was required as the returnedObject, but the returnedObject was: " |
|
183 |
| + returnedObject); |
|
184 |
| } |
|
185 |
| |
|
186 |
| |
|
187 |
6
| Iterator collectionIter = filterer.iterator();
|
|
188 |
| |
|
189 |
6
| while (collectionIter.hasNext()) {
|
|
190 |
24
| Object domainObject = collectionIter.next();
|
|
191 |
| |
|
192 |
24
| boolean hasPermission = false;
|
|
193 |
| |
|
194 |
24
| AclEntry[] acls = null;
|
|
195 |
| |
|
196 |
24
| if (domainObject == null) {
|
|
197 |
0
| hasPermission = true;
|
|
198 |
| } else { |
|
199 |
24
| acls = aclManager.getAcls(domainObject, authentication);
|
|
200 |
| } |
|
201 |
| |
|
202 |
24
| if ((acls != null) && (acls.length != 0)) {
|
|
203 |
5
| for (int i = 0; i < acls.length; i++) {
|
|
204 |
| |
|
205 |
13
| if (acls[i] instanceof BasicAclEntry) {
|
|
206 |
9
| BasicAclEntry processableAcl = (BasicAclEntry) acls[i];
|
|
207 |
| |
|
208 |
| |
|
209 |
9
| for (int y = 0; y < requirePermission.length;
|
|
210 |
| y++) { |
|
211 |
9
| if (processableAcl.isPermitted(
|
|
212 |
| requirePermission[y])) { |
|
213 |
4
| hasPermission = true;
|
|
214 |
| |
|
215 |
4
| if (logger.isDebugEnabled()) {
|
|
216 |
0
| logger.debug(
|
|
217 |
| "Principal is authorised for element: " |
|
218 |
| + domainObject |
|
219 |
| + " due to ACL: " |
|
220 |
| + processableAcl.toString()); |
|
221 |
| } |
|
222 |
| } |
|
223 |
| } |
|
224 |
| } |
|
225 |
| } |
|
226 |
| } |
|
227 |
| |
|
228 |
24
| if (!hasPermission) {
|
|
229 |
20
| filterer.remove(domainObject);
|
|
230 |
| |
|
231 |
20
| if (logger.isDebugEnabled()) {
|
|
232 |
0
| logger.debug(
|
|
233 |
| "Principal is NOT authorised for element: " |
|
234 |
| + domainObject); |
|
235 |
| } |
|
236 |
| } |
|
237 |
| } |
|
238 |
| |
|
239 |
6
| return filterer.getFilteredObject();
|
|
240 |
| } |
|
241 |
| } |
|
242 |
| |
|
243 |
1
| return returnedObject;
|
|
244 |
| } |
|
245 |
| |
|
246 |
10
| public boolean supports(ConfigAttribute attribute) {
|
|
247 |
10
| if ((attribute.getAttribute() != null)
|
|
248 |
| && attribute.getAttribute().equals(getProcessConfigAttribute())) { |
|
249 |
8
| return true;
|
|
250 |
| } else { |
|
251 |
2
| return false;
|
|
252 |
| } |
|
253 |
| } |
|
254 |
| |
|
255 |
| |
|
256 |
| |
|
257 |
| |
|
258 |
| |
|
259 |
| |
|
260 |
| |
|
261 |
| |
|
262 |
| |
|
263 |
1
| public boolean supports(Class clazz) {
|
|
264 |
1
| return true;
|
|
265 |
| } |
|
266 |
| } |
|
267 |
| |
|
268 |
| |
|
269 |
| |
|
270 |
| |
|
271 |
| |
|
272 |
| interface Filterer { |
|
273 |
| |
|
274 |
| |
|
275 |
| |
|
276 |
| |
|
277 |
| |
|
278 |
| |
|
279 |
| |
|
280 |
| public Object getFilteredObject(); |
|
281 |
| |
|
282 |
| |
|
283 |
| |
|
284 |
| |
|
285 |
| |
|
286 |
| |
|
287 |
| public Iterator iterator(); |
|
288 |
| |
|
289 |
| |
|
290 |
| |
|
291 |
| |
|
292 |
| |
|
293 |
| |
|
294 |
| public void remove(Object object); |
|
295 |
| } |
|
296 |
| |
|
297 |
| |
|
298 |
| |
|
299 |
| |
|
300 |
| |
|
301 |
| class CollectionFilterer implements Filterer { |
|
302 |
| |
|
303 |
| |
|
304 |
| protected static final Log logger = LogFactory.getLog(BasicAclEntryAfterInvocationCollectionFilteringProvider.class); |
|
305 |
| |
|
306 |
| |
|
307 |
| |
|
308 |
| private Collection collection; |
|
309 |
| |
|
310 |
| |
|
311 |
| |
|
312 |
| private Iterator collectionIter; |
|
313 |
| private Set removeList; |
|
314 |
| |
|
315 |
| |
|
316 |
| |
|
317 |
5
| CollectionFilterer(Collection collection) {
|
|
318 |
5
| this.collection = collection;
|
|
319 |
| |
|
320 |
| |
|
321 |
| |
|
322 |
| |
|
323 |
| |
|
324 |
| |
|
325 |
| |
|
326 |
| |
|
327 |
5
| removeList = new HashSet();
|
|
328 |
| } |
|
329 |
| |
|
330 |
| |
|
331 |
| |
|
332 |
| |
|
333 |
| |
|
334 |
| |
|
335 |
5
| public Object getFilteredObject() {
|
|
336 |
| |
|
337 |
5
| Iterator removeIter = removeList.iterator();
|
|
338 |
| |
|
339 |
5
| int originalSize = collection.size();
|
|
340 |
| |
|
341 |
5
| while (removeIter.hasNext()) {
|
|
342 |
0
| collection.remove(removeIter.next());
|
|
343 |
| } |
|
344 |
| |
|
345 |
5
| if (logger.isDebugEnabled()) {
|
|
346 |
0
| logger.debug("Original collection contained " + originalSize
|
|
347 |
| + " elements; now contains " + collection.size() + " elements"); |
|
348 |
| } |
|
349 |
| |
|
350 |
5
| return collection;
|
|
351 |
| } |
|
352 |
| |
|
353 |
| |
|
354 |
| |
|
355 |
| |
|
356 |
5
| public Iterator iterator() {
|
|
357 |
5
| collectionIter = collection.iterator();
|
|
358 |
| |
|
359 |
5
| return collectionIter;
|
|
360 |
| } |
|
361 |
| |
|
362 |
| |
|
363 |
| |
|
364 |
| |
|
365 |
17
| public void remove(Object object) {
|
|
366 |
17
| collectionIter.remove();
|
|
367 |
| } |
|
368 |
| } |
|
369 |
| |
|
370 |
| |
|
371 |
| |
|
372 |
| |
|
373 |
| |
|
374 |
| class ArrayFilterer implements Filterer { |
|
375 |
| |
|
376 |
| |
|
377 |
| protected static final Log logger = LogFactory.getLog(BasicAclEntryAfterInvocationCollectionFilteringProvider.class); |
|
378 |
| |
|
379 |
| |
|
380 |
| |
|
381 |
| private Set removeList; |
|
382 |
| private Object[] list; |
|
383 |
| |
|
384 |
| |
|
385 |
| |
|
386 |
1
| ArrayFilterer(Object[] list) {
|
|
387 |
1
| this.list = list;
|
|
388 |
| |
|
389 |
| |
|
390 |
| |
|
391 |
| |
|
392 |
1
| removeList = new HashSet();
|
|
393 |
| } |
|
394 |
| |
|
395 |
| |
|
396 |
| |
|
397 |
| |
|
398 |
| |
|
399 |
| |
|
400 |
1
| public Object getFilteredObject() {
|
|
401 |
| |
|
402 |
1
| int originalSize = list.length;
|
|
403 |
1
| int sizeOfResultingList = originalSize - removeList.size();
|
|
404 |
1
| Object[] filtered = (Object[]) Array.newInstance(list.getClass()
|
|
405 |
| .getComponentType(), |
|
406 |
| sizeOfResultingList); |
|
407 |
| |
|
408 |
1
| for (int i = 0, j = 0; i < list.length; i++) {
|
|
409 |
4
| Object object = list[i];
|
|
410 |
| |
|
411 |
4
| if (!removeList.contains(object)) {
|
|
412 |
1
| filtered[j] = object;
|
|
413 |
1
| j++;
|
|
414 |
| } |
|
415 |
| } |
|
416 |
| |
|
417 |
1
| if (logger.isDebugEnabled()) {
|
|
418 |
0
| logger.debug("Original array contained " + originalSize
|
|
419 |
| + " elements; now contains " + sizeOfResultingList |
|
420 |
| + " elements"); |
|
421 |
| } |
|
422 |
| |
|
423 |
1
| return filtered;
|
|
424 |
| } |
|
425 |
| |
|
426 |
| |
|
427 |
| |
|
428 |
| |
|
429 |
1
| public Iterator iterator() {
|
|
430 |
1
| return new ArrayIterator(list);
|
|
431 |
| } |
|
432 |
| |
|
433 |
| |
|
434 |
| |
|
435 |
| |
|
436 |
3
| public void remove(Object object) {
|
|
437 |
3
| removeList.add(object);
|
|
438 |
| } |
|
439 |
| } |