This project has retired. For details please refer to its
Attic page.
MemoryRbacManager xref
1 package org.apache.archiva.redback.rbac.memory;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.archiva.redback.rbac.AbstractRBACManager;
23 import org.apache.archiva.redback.rbac.Operation;
24 import org.apache.archiva.redback.rbac.Permission;
25 import org.apache.archiva.redback.rbac.RBACManager;
26 import org.apache.archiva.redback.rbac.RBACObjectAssertions;
27 import org.apache.archiva.redback.rbac.RbacManagerException;
28 import org.apache.archiva.redback.rbac.RbacObjectInvalidException;
29 import org.apache.archiva.redback.rbac.RbacObjectNotFoundException;
30 import org.apache.archiva.redback.rbac.Resource;
31 import org.apache.archiva.redback.rbac.Role;
32 import org.apache.archiva.redback.rbac.UserAssignment;
33 import org.apache.archiva.redback.rbac.RbacPermanentException;
34 import org.apache.commons.lang.StringUtils;
35 import org.springframework.stereotype.Service;
36
37 import java.util.ArrayList;
38 import java.util.Collection;
39 import java.util.Collections;
40 import java.util.HashMap;
41 import java.util.List;
42 import java.util.Map;
43
44
45
46
47
48
49
50
51
52
53 @Service( "rbacManager#memory" )
54 public class MemoryRbacManager
55 extends AbstractRBACManager
56 implements RBACManager
57 {
58 private Map<String, Role> roles = new HashMap<String, Role>();
59
60 private Map<String, Permission> permissions = new HashMap<String, Permission>();
61
62 private Map<String, Operation> operations = new HashMap<String, Operation>();
63
64 private Map<String, Resource> resources = new HashMap<String, Resource>();
65
66 private Map<String, UserAssignment> userAssignments = new HashMap<String, UserAssignment>();
67
68 private boolean hasTriggeredInit = false;
69
70
71
72
73
74 public Role saveRole( Role role )
75 throws RbacManagerException
76 {
77 RBACObjectAssertions.assertValid( "Save Role", role );
78
79 triggerInit();
80
81 roles.put( role.getName(), role );
82
83 fireRbacRoleSaved( role );
84
85 if ( role.getPermissions() != null )
86 {
87 for ( Permission p : role.getPermissions() )
88 {
89 savePermission( p );
90 }
91 }
92
93 return role;
94 }
95
96 public void saveRoles( Collection<Role> roles )
97 throws RbacObjectInvalidException, RbacManagerException
98 {
99 if ( roles == null )
100 {
101
102 return;
103 }
104
105 for ( Role role : roles )
106 {
107 saveRole( role );
108 }
109 }
110
111 private void assertRoleExists( String roleName )
112 throws RbacObjectNotFoundException
113 {
114 if ( !roles.containsKey( roleName ) )
115 {
116 throw new RbacObjectNotFoundException( "Role '" + roleName + "' does not exist." );
117 }
118 }
119
120 public Role getRole( String roleName )
121 throws RbacObjectNotFoundException
122 {
123 triggerInit();
124
125 assertRoleExists( roleName );
126
127 return roles.get( roleName );
128 }
129
130 public void removeRole( Role role )
131 throws RbacManagerException, RbacObjectNotFoundException
132 {
133 RBACObjectAssertions.assertValid( "Remove Role", role );
134
135 if ( role.isPermanent() )
136 {
137 throw new RbacPermanentException( "Unable to delete permanent role [" + role.getName() + "]" );
138 }
139
140 assertRoleExists( role.getName() );
141
142 fireRbacRoleRemoved( role );
143
144 roles.remove( role.getName() );
145 }
146
147 public List<Role> getAllRoles()
148 throws RbacManagerException
149 {
150 triggerInit();
151
152 return Collections.unmodifiableList( new ArrayList<Role>( roles.values() ) );
153 }
154
155
156
157
158
159 public Operation saveOperation( Operation operation )
160 throws RbacManagerException
161 {
162 triggerInit();
163
164 RBACObjectAssertions.assertValid( "Save Operation", operation );
165
166 operations.put( operation.getName(), operation );
167 return operation;
168 }
169
170 public Permission savePermission( Permission permission )
171 throws RbacManagerException
172 {
173 triggerInit();
174
175 RBACObjectAssertions.assertValid( "Save Permission", permission );
176
177 permissions.put( permission.getName(), permission );
178
179 fireRbacPermissionSaved( permission );
180
181 saveOperation( permission.getOperation() );
182 saveResource( permission.getResource() );
183 return permission;
184 }
185
186 public Resource saveResource( Resource resource )
187 throws RbacManagerException
188 {
189 triggerInit();
190
191 RBACObjectAssertions.assertValid( "Save Resource", resource );
192
193 resources.put( resource.getIdentifier(), resource );
194 return resource;
195 }
196
197 public UserAssignment saveUserAssignment( UserAssignment userAssignment )
198 throws RbacManagerException
199 {
200 triggerInit();
201
202 RBACObjectAssertions.assertValid( "Save UserAssignment", userAssignment );
203
204 fireRbacUserAssignmentSaved( userAssignment );
205
206 userAssignments.put( userAssignment.getPrincipal(), userAssignment );
207 return userAssignment;
208 }
209
210 public Operation createOperation( String name )
211 throws RbacManagerException
212 {
213 Operation operation;
214
215 try
216 {
217 operation = getOperation( name );
218 }
219 catch ( RbacObjectNotFoundException e )
220 {
221 operation = new MemoryOperation();
222 operation.setName( name );
223 }
224
225 return operation;
226 }
227
228 public Permission createPermission( String name )
229 throws RbacManagerException
230 {
231 Permission permission;
232
233 try
234 {
235 permission = getPermission( name );
236 }
237 catch ( RbacObjectNotFoundException e )
238 {
239 permission = new MemoryPermission();
240 permission.setName( name );
241 }
242
243 return permission;
244 }
245
246 public Permission createPermission( String name, String operationName, String resourceIdentifier )
247 throws RbacManagerException
248 {
249 Permission permission;
250
251 try
252 {
253 permission = getPermission( name );
254
255 if ( StringUtils.equals( operationName, permission.getOperation().getName() ) )
256 {
257 throw new RbacManagerException( "Attempted to create a permission named '" + name +
258 "' with an operation named '" + operationName
259 + "', but that overides the existing '" + name +
260 "' permission with operation '"
261 + permission.getOperation().getName() + "'" );
262 }
263
264 }
265 catch ( RbacObjectNotFoundException e )
266 {
267 permission = new MemoryPermission();
268 permission.setName( name );
269
270 permission.setOperation( createOperation( operationName ) );
271 permission.setResource( createResource( resourceIdentifier ) );
272 }
273
274 return permission;
275 }
276
277 public Resource createResource( String identifier )
278 throws RbacManagerException
279 {
280 Resource resource;
281
282 try
283 {
284 resource = getResource( identifier );
285 }
286 catch ( RbacObjectNotFoundException e )
287 {
288 resource = new MemoryResource();
289 resource.setIdentifier( identifier );
290 }
291
292 return resource;
293 }
294
295 public Role createRole( String name )
296 {
297 Role role = new MemoryRole();
298 role.setName( name );
299
300 return role;
301 }
302
303 private void assertPermissionExists( String permissionName )
304 throws RbacObjectNotFoundException
305 {
306 if ( !permissions.containsKey( permissionName ) )
307 {
308 throw new RbacObjectNotFoundException( "Permission '" + permissionName + "' does not exist." );
309 }
310 }
311
312 public Permission getPermission( String permissionName )
313 throws RbacObjectNotFoundException, RbacManagerException
314 {
315 triggerInit();
316
317 assertPermissionExists( permissionName );
318
319 return permissions.get( permissionName );
320 }
321
322 public List<Resource> getResources()
323 throws RbacManagerException
324 {
325 triggerInit();
326
327 return Collections.unmodifiableList( new ArrayList<Resource>( resources.values() ) );
328 }
329
330 public void removeOperation( Operation operation )
331 throws RbacObjectNotFoundException, RbacManagerException
332 {
333 RBACObjectAssertions.assertValid( "Remove Operation", operation );
334
335 if ( operation.isPermanent() )
336 {
337 throw new RbacPermanentException( "Unable to delete permanent operation [" + operation.getName() + "]" );
338 }
339
340 assertOpertionExists( operation.getName() );
341
342 operations.remove( operation.getName() );
343 }
344
345 private void assertOpertionExists( String operationName )
346 throws RbacObjectNotFoundException
347 {
348 if ( !operations.containsKey( operationName ) )
349 {
350 throw new RbacObjectNotFoundException( "Operation '" + operationName + "' not found." );
351 }
352 }
353
354 public void removePermission( Permission permission )
355 throws RbacObjectNotFoundException, RbacManagerException
356 {
357 RBACObjectAssertions.assertValid( "Remove Permission", permission );
358
359 if ( permission.isPermanent() )
360 {
361 throw new RbacPermanentException( "Unable to delete permanent permission [" + permission.getName() + "]" );
362 }
363
364 assertPermissionExists( permission.getName() );
365
366 fireRbacPermissionRemoved( permission );
367
368 permissions.remove( permission.getName() );
369 }
370
371 public void removeResource( Resource resource )
372 throws RbacObjectNotFoundException, RbacManagerException
373 {
374 RBACObjectAssertions.assertValid( "Remove Resource", resource );
375
376 if ( resource.isPermanent() )
377 {
378 throw new RbacPermanentException(
379 "Unable to delete permanent resource [" + resource.getIdentifier() + "]" );
380 }
381
382 assertResourceExists( resource.getIdentifier() );
383
384 resources.remove( resource.getIdentifier() );
385 }
386
387 private void assertResourceExists( String resourceIdentifier )
388 throws RbacObjectNotFoundException
389 {
390 if ( !resources.containsKey( resourceIdentifier ) )
391 {
392 throw new RbacObjectNotFoundException( "Resource '" + resourceIdentifier + "' not found." );
393 }
394 }
395
396 private void assertUserAssignmentExists( String principal )
397 throws RbacObjectNotFoundException
398 {
399 if ( !userAssignments.containsKey( principal ) )
400 {
401 throw new RbacObjectNotFoundException( "UserAssignment '" + principal + "' not found." );
402 }
403 }
404
405 public void removeUserAssignment( UserAssignment userAssignment )
406 throws RbacObjectNotFoundException, RbacManagerException
407 {
408 RBACObjectAssertions.assertValid( "Remove User Assignment", userAssignment );
409
410 if ( userAssignment.isPermanent() )
411 {
412 throw new RbacPermanentException(
413 "Unable to delete permanent user assignment [" + userAssignment.getPrincipal() + "]" );
414 }
415
416 fireRbacUserAssignmentRemoved( userAssignment );
417
418 assertUserAssignmentExists( userAssignment.getPrincipal() );
419
420 userAssignments.remove( userAssignment.getPrincipal() );
421 }
422
423 public void eraseDatabase()
424 {
425 userAssignments.clear();
426 resources.clear();
427 operations.clear();
428 permissions.clear();
429 roles.clear();
430 }
431
432 public UserAssignment createUserAssignment( String principal )
433 throws RbacManagerException
434 {
435 try
436 {
437 return getUserAssignment( principal );
438 }
439 catch ( RbacObjectNotFoundException e )
440 {
441 UserAssignment ua = new MemoryUserAssignment();
442 ua.setPrincipal( principal );
443
444 fireRbacUserAssignmentSaved( ua );
445
446 return ua;
447 }
448 }
449
450 public List<Operation> getAllOperations()
451 throws RbacManagerException
452 {
453 triggerInit();
454
455 return Collections.unmodifiableList( new ArrayList<Operation>( operations.values() ) );
456 }
457
458 public List<Permission> getAllPermissions()
459 throws RbacManagerException
460 {
461 triggerInit();
462
463 return Collections.unmodifiableList( new ArrayList<Permission>( permissions.values() ) );
464 }
465
466 public List<Resource> getAllResources()
467 throws RbacManagerException
468 {
469 triggerInit();
470
471 return Collections.unmodifiableList( new ArrayList<Resource>( resources.values() ) );
472 }
473
474 public List<UserAssignment> getAllUserAssignments()
475 throws RbacManagerException
476 {
477 triggerInit();
478
479 return Collections.unmodifiableList( new ArrayList<UserAssignment>( userAssignments.values() ) );
480 }
481
482 public List<UserAssignment> getUserAssignmentsForRoles( Collection<String> roleNames )
483 throws RbacManagerException
484 {
485
486 List<UserAssignment> allUserAssignments = getAllUserAssignments();
487 List<UserAssignment> userAssignments = new ArrayList<UserAssignment>( allUserAssignments.size() );
488
489 for ( UserAssignment ua : allUserAssignments )
490 {
491 for ( String roleName : roleNames )
492 {
493 if ( ua.getRoleNames().contains( roleName ) )
494 {
495 userAssignments.add( ua );
496 break;
497 }
498 }
499 }
500
501 return userAssignments;
502 }
503
504 public UserAssignment getUserAssignment( String principal )
505 throws RbacObjectNotFoundException, RbacManagerException
506 {
507 triggerInit();
508
509 assertUserAssignmentExists( principal );
510
511 return userAssignments.get( principal );
512 }
513
514 public Operation getOperation( String operationName )
515 throws RbacObjectNotFoundException, RbacManagerException
516 {
517 triggerInit();
518
519 assertOpertionExists( operationName );
520
521 return operations.get( operationName );
522 }
523
524 public Resource getResource( String resourceIdentifier )
525 throws RbacObjectNotFoundException, RbacManagerException
526 {
527 triggerInit();
528
529 assertResourceExists( resourceIdentifier );
530
531 return resources.get( resourceIdentifier );
532 }
533
534 public void triggerInit()
535 {
536 if ( !hasTriggeredInit )
537 {
538 fireRbacInit( roles.isEmpty() );
539 hasTriggeredInit = true;
540 }
541 }
542
543 @Override
544 public boolean isFinalImplementation()
545 {
546 return true;
547 }
548
549 public String getDescriptionKey()
550 {
551 return "archiva.redback.rbacmanager.memory";
552 }
553
554 public boolean isReadOnly()
555 {
556 return false;
557 }
558 }