1 package org.apache.fulcrum.security.model.dynamic;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Map;
23
24 import org.apache.fulcrum.security.entity.Group;
25 import org.apache.fulcrum.security.entity.Permission;
26 import org.apache.fulcrum.security.entity.Role;
27 import org.apache.fulcrum.security.util.GroupSet;
28 import org.apache.fulcrum.security.util.PermissionSet;
29 import org.apache.fulcrum.security.util.RoleSet;
30
31
32
33
34
35
36
37
38 public class DynamicAccessControlListImpl implements DynamicAccessControlList
39 {
40
41
42
43
44
45
46 private static final long serialVersionUID = -5180551537096244085L;
47
48
49 private Map<? extends Group, ? extends RoleSet> roleSets;
50
51
52 private Map<? extends Role, ? extends PermissionSet> permissionSets;
53
54
55 private GroupSety/util/GroupSet.html#GroupSet">GroupSet groupSet = new GroupSet();
56
57
58 private RoleSetity/util/RoleSet.html#RoleSet">RoleSet roleSet = new RoleSet();
59
60
61 private PermissionSetmissionSet.html#PermissionSet">PermissionSet permissionSet = new PermissionSet();
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 public DynamicAccessControlListImpl(Map<? extends Group, ? extends RoleSet> roleSets,
81 Map<? extends Role, ? extends PermissionSet> permissionSets) {
82 this.roleSets = roleSets;
83 this.permissionSets = permissionSets;
84 for (Map.Entry<? extends Group, ? extends RoleSet> entry : roleSets.entrySet())
85 {
86 Group group = entry.getKey();
87 groupSet.add(group);
88 RoleSet rs = entry.getValue();
89 roleSet.add(rs);
90 }
91
92 for (Map.Entry<? extends Role, ? extends PermissionSet> entry : permissionSets.entrySet())
93 {
94 Role role = entry.getKey();
95 roleSet.add(role);
96 PermissionSet ps = entry.getValue();
97 permissionSet.add(ps);
98 }
99 }
100
101
102
103
104
105
106
107 public RoleSet getRoles(Group group) {
108 if (group == null)
109 {
110 return null;
111 }
112
113 return roleSets.get(group);
114 }
115
116
117
118
119
120
121 public RoleSet getRoles() {
122 return roleSet;
123 }
124
125
126
127
128
129
130
131 public PermissionSet getPermissions(Group group) {
132 PermissionSet permissionSet = new PermissionSet();
133 if (roleSets.containsKey(group))
134 {
135 for (Role role : roleSets.get(group))
136 {
137 if (permissionSets.containsKey(role))
138 {
139 permissionSet.add(permissionSets.get(role));
140 }
141 }
142 }
143 return permissionSet;
144 }
145
146
147
148
149
150
151 public PermissionSet getPermissions() {
152 return permissionSet;
153 }
154
155
156
157
158
159
160
161
162 public boolean hasRole(Role role, Group group) {
163 RoleSet set = getRoles(group);
164 if (set == null || role == null)
165 {
166 return false;
167 }
168 return set.contains(role);
169 }
170
171
172
173
174
175
176
177
178
179 public boolean hasRole(Role role, GroupSet groupset) {
180 if (role == null)
181 {
182 return false;
183 }
184
185 for (Group group : groupset)
186 {
187 RoleSet roles = getRoles(group);
188 if (roles != null && roles.contains(role))
189 {
190 return true;
191 }
192 }
193 return false;
194 }
195
196
197
198
199
200
201
202
203 public boolean hasRole(String role, String group) {
204 boolean roleFound = false;
205 try
206 {
207 for (Map.Entry<? extends Group, ? extends RoleSet> entry : roleSets.entrySet())
208 {
209 Group g = entry.getKey();
210 if (g.getName().equalsIgnoreCase(group))
211 {
212 RoleSet rs = entry.getValue();
213 roleFound = rs.containsName(role);
214 }
215 }
216 }
217 catch (Exception e)
218 {
219 roleFound = false;
220 }
221 return roleFound;
222 }
223
224
225
226
227
228
229
230
231
232 public boolean hasRole(String rolename, GroupSet groupset) {
233 Role role;
234 try
235 {
236 role = roleSet.getByName(rolename);
237 }
238 catch (Exception e)
239 {
240 return false;
241 }
242
243 if (role == null)
244 {
245 return false;
246 }
247
248 for (Group group : groupset)
249 {
250 RoleSet roles = getRoles(group);
251 if (roles != null && roles.contains(role))
252 {
253 return true;
254 }
255 }
256 return false;
257 }
258
259
260
261
262
263
264
265
266 public boolean hasRole(Role role) {
267 return roleSet.contains(role);
268 }
269
270
271
272
273
274
275
276 public boolean hasRole(String role) {
277 try
278 {
279 return roleSet.containsName(role);
280 }
281 catch (Exception e)
282 {
283 return false;
284 }
285 }
286
287
288
289
290
291
292
293
294
295 public boolean hasPermission(Permission permission, Group group) {
296 PermissionSet set = getPermissions(group);
297 if (set == null || permission == null)
298 {
299 return false;
300 }
301 return set.contains(permission);
302 }
303
304
305
306
307
308
309
310
311
312
313 public boolean hasPermission(Permission permission, GroupSet groupset) {
314 if (permission == null)
315 {
316 return false;
317 }
318
319 for (Group group : groupset)
320 {
321 PermissionSet permissions = getPermissions(group);
322 if (permissions != null && permissions.contains(permission))
323 {
324 return true;
325 }
326 }
327 return false;
328 }
329
330
331
332
333
334
335
336
337
338 public boolean hasPermission(String permission, String group) {
339 try
340 {
341 return hasPermission(permissionSet.getByName(permission), groupSet.getByName(group));
342 }
343 catch (Exception e)
344 {
345 return false;
346 }
347 }
348
349
350
351
352
353
354
355
356
357 public boolean hasPermission(String permission, Group group) {
358 try
359 {
360 return hasPermission(permissionSet.getByName(permission), group);
361 }
362 catch (Exception e)
363 {
364 return false;
365 }
366 }
367
368
369
370
371
372
373
374
375
376
377 public boolean hasPermission(String permissionName, GroupSet groupset) {
378 Permission permission;
379 try
380 {
381 permission = permissionSet.getByName(permissionName);
382 } catch (Exception e) {
383 return false;
384 }
385
386 if (permission == null)
387 {
388 return false;
389 }
390
391 for (Group group : groupset)
392 {
393 PermissionSet permissions = getPermissions(group);
394 if (permissions != null && permissions.contains(permission))
395 {
396 return true;
397 }
398 }
399 return false;
400 }
401
402
403
404
405
406
407
408 public boolean hasPermission(Permission permission) {
409 return permissionSet.contains(permission);
410 }
411
412
413
414
415
416
417
418
419 public boolean hasPermission(String permission) {
420 try
421 {
422 return permissionSet.containsName(permission);
423 }
424 catch (Exception e)
425 {
426 return false;
427 }
428 }
429 }