1 package org.apache.archiva.redback.rbac.memory;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
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.codehaus.plexus.util.StringUtils;
35 import org.springframework.stereotype.Service;
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;
45 * MemoryRbacManager: a very quick and dirty implementation of a rbac store
47 * WARNING: not for actual usage, its not sound - jesse
49 * @author Jesse McConnell <jmcconnell@apache.org>
50 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
53 @Service( "rBACManager#memory" )
54 public class MemoryRbacManager
55 extends AbstractRBACManager
56 implements RBACManager
58 private Map<String, Role> roles = new HashMap<String, Role>();
60 private Map<String, Permission> permissions = new HashMap<String, Permission>();
62 private Map<String, Operation> operations = new HashMap<String, Operation>();
64 private Map<String, Resource> resources = new HashMap<String, Resource>();
66 private Map<String, UserAssignment> userAssignments = new HashMap<String, UserAssignment>();
68 // ----------------------------------------------------------------------
70 // ----------------------------------------------------------------------
72 public Role saveRole( Role role )
73 throws RbacManagerException
75 RBACObjectAssertions.assertValid( "Save Role", role );
79 roles.put( role.getName(), role );
81 fireRbacRoleSaved( role );
83 if ( role.getPermissions() != null )
85 for ( Permission p : role.getPermissions() )
94 public void saveRoles( Collection<Role> roles )
95 throws RbacObjectInvalidException, RbacManagerException
103 for ( Role role : roles )
109 private void assertRoleExists( String roleName )
110 throws RbacObjectNotFoundException
112 if ( !roles.containsKey( roleName ) )
114 throw new RbacObjectNotFoundException( "Role '" + roleName + "' does not exist." );
118 public Role getRole( String roleName )
119 throws RbacObjectNotFoundException
123 assertRoleExists( roleName );
125 return roles.get( roleName );
128 public void removeRole( Role role )
129 throws RbacManagerException, RbacObjectNotFoundException
131 RBACObjectAssertions.assertValid( "Remove Role", role );
133 if ( role.isPermanent() )
135 throw new RbacPermanentException( "Unable to delete permanent role [" + role.getName() + "]" );
138 assertRoleExists( role.getName() );
140 fireRbacRoleRemoved( role );
142 roles.remove( role.getName() );
145 public List<Role> getAllRoles()
146 throws RbacManagerException
150 return Collections.unmodifiableList( new ArrayList<Role>( roles.values() ) );
153 // ----------------------------------------------------------------------
154 // Permission methods
155 // ----------------------------------------------------------------------
157 public Operation saveOperation( Operation operation )
158 throws RbacManagerException
162 RBACObjectAssertions.assertValid( "Save Operation", operation );
164 operations.put( operation.getName(), operation );
168 public Permission savePermission( Permission permission )
169 throws RbacManagerException
173 RBACObjectAssertions.assertValid( "Save Permission", permission );
175 permissions.put( permission.getName(), permission );
177 fireRbacPermissionSaved( permission );
179 saveOperation( permission.getOperation() );
180 saveResource( permission.getResource() );
184 public Resource saveResource( Resource resource )
185 throws RbacManagerException
189 RBACObjectAssertions.assertValid( "Save Resource", resource );
191 resources.put( resource.getIdentifier(), resource );
195 public UserAssignment saveUserAssignment( UserAssignment userAssignment )
196 throws RbacManagerException
200 RBACObjectAssertions.assertValid( "Save UserAssignment", userAssignment );
202 fireRbacUserAssignmentSaved( userAssignment );
204 userAssignments.put( userAssignment.getPrincipal(), userAssignment );
205 return userAssignment;
208 public Operation createOperation( String name )
209 throws RbacManagerException
215 operation = getOperation( name );
217 catch ( RbacObjectNotFoundException e )
219 operation = new MemoryOperation();
220 operation.setName( name );
226 public Permission createPermission( String name )
227 throws RbacManagerException
229 Permission permission;
233 permission = getPermission( name );
235 catch ( RbacObjectNotFoundException e )
237 permission = new MemoryPermission();
238 permission.setName( name );
244 public Permission createPermission( String name, String operationName, String resourceIdentifier )
245 throws RbacManagerException
247 Permission permission;
251 permission = getPermission( name );
253 if ( StringUtils.equals( operationName, permission.getOperation().getName() ) )
255 throw new RbacManagerException( "Attempted to create a permission named '" + name +
256 "' with an operation named '" + operationName
257 + "', but that overides the existing '" + name +
258 "' permission with operation '"
259 + permission.getOperation().getName() + "'" );
263 catch ( RbacObjectNotFoundException e )
265 permission = new MemoryPermission();
266 permission.setName( name );
268 permission.setOperation( createOperation( operationName ) );
269 permission.setResource( createResource( resourceIdentifier ) );
275 public Resource createResource( String identifier )
276 throws RbacManagerException
282 resource = getResource( identifier );
284 catch ( RbacObjectNotFoundException e )
286 resource = new MemoryResource();
287 resource.setIdentifier( identifier );
293 public Role createRole( String name )
295 Role role = new MemoryRole();
296 role.setName( name );
301 private void assertPermissionExists( String permissionName )
302 throws RbacObjectNotFoundException
304 if ( !permissions.containsKey( permissionName ) )
306 throw new RbacObjectNotFoundException( "Permission '" + permissionName + "' does not exist." );
310 public Permission getPermission( String permissionName )
311 throws RbacObjectNotFoundException, RbacManagerException
315 assertPermissionExists( permissionName );
317 return permissions.get( permissionName );
320 public List<Resource> getResources()
321 throws RbacManagerException
325 return Collections.unmodifiableList( new ArrayList<Resource>( resources.values() ) );
328 public void removeOperation( Operation operation )
329 throws RbacObjectNotFoundException, RbacManagerException
331 RBACObjectAssertions.assertValid( "Remove Operation", operation );
333 if ( operation.isPermanent() )
335 throw new RbacPermanentException( "Unable to delete permanent operation [" + operation.getName() + "]" );
338 assertOpertionExists( operation.getName() );
340 operations.remove( operation.getName() );
343 private void assertOpertionExists( String operationName )
344 throws RbacObjectNotFoundException
346 if ( !operations.containsKey( operationName ) )
348 throw new RbacObjectNotFoundException( "Operation '" + operationName + "' not found." );
352 public void removePermission( Permission permission )
353 throws RbacObjectNotFoundException, RbacManagerException
355 RBACObjectAssertions.assertValid( "Remove Permission", permission );
357 if ( permission.isPermanent() )
359 throw new RbacPermanentException( "Unable to delete permanent permission [" + permission.getName() + "]" );
362 assertPermissionExists( permission.getName() );
364 fireRbacPermissionRemoved( permission );
366 permissions.remove( permission.getName() );
369 public void removeResource( Resource resource )
370 throws RbacObjectNotFoundException, RbacManagerException
372 RBACObjectAssertions.assertValid( "Remove Resource", resource );
374 if ( resource.isPermanent() )
376 throw new RbacPermanentException(
377 "Unable to delete permanent resource [" + resource.getIdentifier() + "]" );
380 assertResourceExists( resource.getIdentifier() );
382 resources.remove( resource.getIdentifier() );
385 private void assertResourceExists( String resourceIdentifier )
386 throws RbacObjectNotFoundException
388 if ( !resources.containsKey( resourceIdentifier ) )
390 throw new RbacObjectNotFoundException( "Resource '" + resourceIdentifier + "' not found." );
394 private void assertUserAssignmentExists( String principal )
395 throws RbacObjectNotFoundException
397 if ( !userAssignments.containsKey( principal ) )
399 throw new RbacObjectNotFoundException( "UserAssignment '" + principal + "' not found." );
403 public void removeUserAssignment( UserAssignment userAssignment )
404 throws RbacObjectNotFoundException, RbacManagerException
406 RBACObjectAssertions.assertValid( "Remove User Assignment", userAssignment );
408 if ( userAssignment.isPermanent() )
410 throw new RbacPermanentException(
411 "Unable to delete permanent user assignment [" + userAssignment.getPrincipal() + "]" );
414 fireRbacUserAssignmentRemoved( userAssignment );
416 assertUserAssignmentExists( userAssignment.getPrincipal() );
418 userAssignments.remove( userAssignment.getPrincipal() );
421 public void eraseDatabase()
423 userAssignments.clear();
430 public UserAssignment createUserAssignment( String principal )
431 throws RbacManagerException
435 return getUserAssignment( principal );
437 catch ( RbacObjectNotFoundException e )
439 UserAssignment ua = new MemoryUserAssignment();
440 ua.setPrincipal( principal );
442 fireRbacUserAssignmentSaved( ua );
448 public List<Operation> getAllOperations()
449 throws RbacManagerException
453 return Collections.unmodifiableList( new ArrayList<Operation>( operations.values() ) );
456 public List<Permission> getAllPermissions()
457 throws RbacManagerException
461 return Collections.unmodifiableList( new ArrayList<Permission>( permissions.values() ) );
464 public List<Resource> getAllResources()
465 throws RbacManagerException
469 return Collections.unmodifiableList( new ArrayList<Resource>( resources.values() ) );
472 public List<UserAssignment> getAllUserAssignments()
473 throws RbacManagerException
477 return Collections.unmodifiableList( new ArrayList<UserAssignment>( userAssignments.values() ) );
480 public List<UserAssignment> getUserAssignmentsForRoles( Collection<String> roleNames )
481 throws RbacManagerException
484 List<UserAssignment> allUserAssignments = getAllUserAssignments();
485 List<UserAssignment> userAssignments = new ArrayList<UserAssignment>( allUserAssignments.size() );
487 for ( UserAssignment ua : allUserAssignments )
489 for ( String roleName : roleNames )
491 if ( ua.getRoleNames().contains( roleName ) )
493 userAssignments.add( ua );
499 return userAssignments;
502 public UserAssignment getUserAssignment( String principal )
503 throws RbacObjectNotFoundException, RbacManagerException
507 assertUserAssignmentExists( principal );
509 return userAssignments.get( principal );
512 public Operation getOperation( String operationName )
513 throws RbacObjectNotFoundException, RbacManagerException
517 assertOpertionExists( operationName );
519 return operations.get( operationName );
522 public Resource getResource( String resourceIdentifier )
523 throws RbacObjectNotFoundException, RbacManagerException
527 assertResourceExists( resourceIdentifier );
529 return resources.get( resourceIdentifier );
532 private boolean hasTriggeredInit = false;
534 public void triggerInit()
536 if ( !hasTriggeredInit )
538 fireRbacInit( roles.isEmpty() );
539 hasTriggeredInit = true;