1 package org.apache.archiva.web.security;
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.archiva.admin.model.RepositoryAdminException;
22 import org.apache.archiva.admin.model.runtime.RedbackRuntimeConfigurationAdmin;
23 import org.apache.archiva.redback.rbac.AbstractRBACManager;
24 import org.apache.archiva.redback.rbac.Operation;
25 import org.apache.archiva.redback.rbac.Permission;
26 import org.apache.archiva.redback.rbac.RBACManager;
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.users.UserManager;
34 import org.springframework.context.ApplicationContext;
35 import org.springframework.stereotype.Service;
37 import javax.inject.Inject;
38 import java.util.ArrayList;
39 import java.util.Collection;
40 import java.util.HashMap;
41 import java.util.LinkedHashMap;
42 import java.util.List;
46 * @author Olivier Lamy
49 @Service( "rbacManager#archiva" )
50 public class ArchivaRbacManager
51 extends AbstractRBACManager
52 implements RBACManager
55 private Map<String, RBACManager> rbacManagersPerId;
58 private ApplicationContext applicationContext;
61 private RedbackRuntimeConfigurationAdmin redbackRuntimeConfigurationAdmin;
64 public void initialize()
68 List<String> rbacManagerIds =
69 redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getRbacManagerImpls();
71 log.info( "use rbacManagerIds: '{}'", rbacManagerIds );
73 this.rbacManagersPerId = new LinkedHashMap<String, RBACManager>( rbacManagerIds.size() );
75 for ( String id : rbacManagerIds )
77 RBACManager rbacManager = applicationContext.getBean( "rbacManager#" + id, RBACManager.class );
79 rbacManagersPerId.put( id, rbacManager );
82 catch ( RepositoryAdminException e )
84 // revert to a default one ?
85 log.error( e.getMessage(), e );
86 throw new RuntimeException( e.getMessage(), e );
90 protected RBACManager getRbacManagerForWrite()
92 for ( RBACManager rbacManager : this.rbacManagersPerId.values() )
94 if ( !rbacManager.isReadOnly() )
99 return this.rbacManagersPerId.values().iterator().next();
102 public Role createRole( String name )
104 return getRbacManagerForWrite().createRole( name );
107 public Role saveRole( Role role )
108 throws RbacObjectInvalidException, RbacManagerException
110 Exception lastException = null;
111 boolean allFailed = true;
112 for ( RBACManager rbacManager : rbacManagersPerId.values() )
116 role = rbacManager.saveRole( role );
119 catch ( Exception e )
124 if ( lastException != null && allFailed )
126 throw new RbacManagerException( lastException.getMessage(), lastException );
131 public void saveRoles( Collection<Role> roles )
132 throws RbacObjectInvalidException, RbacManagerException
134 Exception lastException = null;
135 boolean allFailed = true;
136 for ( RBACManager rbacManager : rbacManagersPerId.values() )
140 rbacManager.saveRoles( roles );
143 catch ( Exception e )
148 if ( lastException != null && allFailed )
150 throw new RbacManagerException( lastException.getMessage(), lastException );
154 public Role getRole( String roleName )
155 throws RbacObjectNotFoundException, RbacManagerException
157 Exception lastException = null;
158 for ( RBACManager rbacManager : rbacManagersPerId.values() )
162 Role role = rbacManager.getRole( roleName );
168 catch ( Exception e )
173 log.debug( "cannot find role for name: ‘{}", roleName );
174 if ( lastException != null )
176 throw new RbacManagerException( lastException.getMessage(), lastException );
181 public List<Role> getAllRoles()
182 throws RbacManagerException
184 Map<String, Role> allRoles = new HashMap<String, Role>();
185 boolean allFailed = true;
186 Exception lastException = null;
187 for ( RBACManager rbacManager : rbacManagersPerId.values() )
191 List<Role> roles = rbacManager.getAllRoles();
192 for ( Role role : roles )
194 allRoles.put( role.getName(), role );
198 catch ( Exception e )
204 if ( lastException != null && allFailed )
206 throw new RbacManagerException( lastException.getMessage(), lastException );
209 return new ArrayList<Role>( allRoles.values() );
212 public void removeRole( Role role )
213 throws RbacObjectNotFoundException, RbacObjectInvalidException, RbacManagerException
215 boolean allFailed = true;
216 Exception lastException = null;
217 for ( RBACManager rbacManager : rbacManagersPerId.values() )
221 rbacManager.removeRole( role );
224 catch ( Exception e )
230 if ( lastException != null && allFailed )
232 throw new RbacManagerException( lastException.getMessage(), lastException );
236 public Permission createPermission( String name )
237 throws RbacManagerException
239 return getRbacManagerForWrite().createPermission( name );
242 public Permission createPermission( String name, String operationName, String resourceIdentifier )
243 throws RbacManagerException
245 return getRbacManagerForWrite().createPermission( name, operationName, resourceIdentifier );
248 public Permission savePermission( Permission permission )
249 throws RbacObjectInvalidException, RbacManagerException
251 boolean allFailed = true;
252 Exception lastException = null;
253 for ( RBACManager rbacManager : rbacManagersPerId.values() )
257 permission = rbacManager.savePermission( permission );
260 catch ( Exception e )
266 if ( lastException != null && allFailed )
268 throw new RbacManagerException( lastException.getMessage(), lastException );
274 public Permission getPermission( String permissionName )
275 throws RbacObjectNotFoundException, RbacManagerException
277 Exception lastException = null;
278 for ( RBACManager rbacManager : rbacManagersPerId.values() )
282 Permission p = rbacManager.getPermission( permissionName );
288 catch ( Exception e )
294 if ( lastException != null )
296 throw new RbacManagerException( lastException.getMessage(), lastException );
301 public List<Permission> getAllPermissions()
302 throws RbacManagerException
304 Map<String, Permission> allPermissions = new HashMap<String, Permission>();
305 boolean allFailed = true;
306 Exception lastException = null;
307 for ( RBACManager rbacManager : rbacManagersPerId.values() )
311 List<Permission> permissions = rbacManager.getAllPermissions();
312 for ( Permission p : permissions )
314 allPermissions.put( p.getName(), p );
318 catch ( Exception e )
324 if ( lastException != null && allFailed )
326 throw new RbacManagerException( lastException.getMessage(), lastException );
328 return new ArrayList<Permission>( allPermissions.values() );
331 public void removePermission( Permission permission )
332 throws RbacObjectNotFoundException, RbacObjectInvalidException, RbacManagerException
334 boolean allFailed = true;
335 Exception lastException = null;
336 for ( RBACManager rbacManager : rbacManagersPerId.values() )
340 rbacManager.removePermission( permission );
343 catch ( Exception e )
349 if ( lastException != null && allFailed )
351 throw new RbacManagerException( lastException.getMessage(), lastException );
355 public Operation createOperation( String name )
356 throws RbacManagerException
358 return getRbacManagerForWrite().createOperation( name );
361 public Operation saveOperation( Operation operation )
362 throws RbacObjectInvalidException, RbacManagerException
364 boolean allFailed = true;
365 Exception lastException = null;
366 for ( RBACManager rbacManager : rbacManagersPerId.values() )
370 operation = rbacManager.saveOperation( operation );
373 catch ( Exception e )
379 if ( lastException != null && allFailed )
381 throw new RbacManagerException( lastException.getMessage(), lastException );
386 public Operation getOperation( String operationName )
387 throws RbacObjectNotFoundException, RbacManagerException
389 Exception lastException = null;
390 for ( RBACManager rbacManager : rbacManagersPerId.values() )
394 Operation o = rbacManager.getOperation( operationName );
400 catch ( Exception e )
406 if ( lastException != null )
408 throw new RbacManagerException( lastException.getMessage(), lastException );
413 public List<Operation> getAllOperations()
414 throws RbacManagerException
416 Map<String, Operation> allOperations = new HashMap<String, Operation>();
417 boolean allFailed = true;
418 Exception lastException = null;
419 for ( RBACManager rbacManager : rbacManagersPerId.values() )
423 List<Operation> operations = rbacManager.getAllOperations();
424 for ( Operation o : operations )
426 allOperations.put( o.getName(), o );
430 catch ( Exception e )
436 if ( lastException != null && allFailed )
438 throw new RbacManagerException( lastException.getMessage(), lastException );
440 return new ArrayList<Operation>( allOperations.values() );
443 public void removeOperation( Operation operation )
444 throws RbacObjectNotFoundException, RbacObjectInvalidException, RbacManagerException
446 boolean allFailed = true;
447 Exception lastException = null;
448 for ( RBACManager rbacManager : rbacManagersPerId.values() )
452 rbacManager.removeOperation( operation );
455 catch ( Exception e )
461 if ( lastException != null && allFailed )
463 throw new RbacManagerException( lastException.getMessage(), lastException );
467 public Resource createResource( String identifier )
468 throws RbacManagerException
470 return getRbacManagerForWrite().createResource( identifier );
473 public Resource saveResource( Resource resource )
474 throws RbacObjectInvalidException, RbacManagerException
476 boolean allFailed = true;
477 Exception lastException = null;
478 for ( RBACManager rbacManager : rbacManagersPerId.values() )
482 resource = rbacManager.saveResource( resource );
486 catch ( Exception e )
492 if ( lastException != null && allFailed )
494 throw new RbacManagerException( lastException.getMessage(), lastException );
499 public Resource getResource( String resourceIdentifier )
500 throws RbacObjectNotFoundException, RbacManagerException
503 Exception lastException = null;
504 for ( RBACManager rbacManager : rbacManagersPerId.values() )
508 Resource r = rbacManager.getResource( resourceIdentifier );
514 catch ( Exception e )
520 if ( lastException != null )
522 throw new RbacManagerException( lastException.getMessage(), lastException );
527 public List<Resource> getAllResources()
528 throws RbacManagerException
530 Map<String, Resource> allResources = new HashMap<String, Resource>();
531 boolean allFailed = true;
532 Exception lastException = null;
533 for ( RBACManager rbacManager : rbacManagersPerId.values() )
537 List<Resource> resources = rbacManager.getAllResources();
538 for ( Resource r : resources )
540 allResources.put( r.getIdentifier(), r );
544 catch ( Exception e )
550 if ( lastException != null && allFailed )
552 throw new RbacManagerException( lastException.getMessage(), lastException );
554 return new ArrayList<Resource>( allResources.values() );
557 public void removeResource( Resource resource )
558 throws RbacObjectNotFoundException, RbacObjectInvalidException, RbacManagerException
560 boolean allFailed = true;
561 Exception lastException = null;
562 for ( RBACManager rbacManager : rbacManagersPerId.values() )
566 rbacManager.removeResource( resource );
569 catch ( Exception e )
575 if ( lastException != null && allFailed )
577 throw new RbacManagerException( lastException.getMessage(), lastException );
581 public UserAssignment createUserAssignment( String principal )
582 throws RbacManagerException
584 return getRbacManagerForWrite().createUserAssignment( principal );
587 public UserAssignment saveUserAssignment( UserAssignment userAssignment )
588 throws RbacObjectInvalidException, RbacManagerException
590 boolean allFailed = true;
591 Exception lastException = null;
592 for ( RBACManager rbacManager : rbacManagersPerId.values() )
596 userAssignment = rbacManager.saveUserAssignment( userAssignment );
599 catch ( Exception e )
605 if ( lastException != null && allFailed )
607 throw new RbacManagerException( lastException.getMessage(), lastException );
609 return userAssignment;
612 public UserAssignment getUserAssignment( String principal )
613 throws RbacObjectNotFoundException, RbacManagerException
616 Exception lastException = null;
617 for ( RBACManager rbacManager : rbacManagersPerId.values() )
621 UserAssignment ua = rbacManager.getUserAssignment( principal );
627 catch ( Exception e )
633 if ( lastException != null )
635 throw new RbacManagerException( lastException.getMessage(), lastException );
641 public boolean userAssignmentExists( String principal )
644 for ( RBACManager rbacManager : rbacManagersPerId.values() )
648 boolean exists = rbacManager.userAssignmentExists( principal );
654 catch ( Exception e )
664 public boolean userAssignmentExists( UserAssignment assignment )
666 for ( RBACManager rbacManager : rbacManagersPerId.values() )
670 boolean exists = rbacManager.userAssignmentExists( assignment );
676 catch ( Exception e )
685 public List<UserAssignment> getAllUserAssignments()
686 throws RbacManagerException
688 Map<String, UserAssignment> allUserAssignments = new HashMap<String, UserAssignment>();
689 boolean allFailed = true;
690 Exception lastException = null;
691 for ( RBACManager rbacManager : rbacManagersPerId.values() )
695 List<UserAssignment> userAssignments = rbacManager.getAllUserAssignments();
696 for ( UserAssignment ua : userAssignments )
698 allUserAssignments.put( ua.getPrincipal(), ua );
702 catch ( Exception e )
708 if ( lastException != null && allFailed )
710 throw new RbacManagerException( lastException.getMessage(), lastException );
712 return new ArrayList<UserAssignment>( allUserAssignments.values() );
715 public List<UserAssignment> getUserAssignmentsForRoles( Collection<String> roleNames )
716 throws RbacManagerException
718 List<UserAssignment> allUserAssignments = new ArrayList<UserAssignment>();
719 boolean allFailed = true;
720 Exception lastException = null;
721 for ( RBACManager rbacManager : rbacManagersPerId.values() )
725 List<UserAssignment> userAssignments = rbacManager.getUserAssignmentsForRoles( roleNames );
727 allUserAssignments.addAll( userAssignments );
731 catch ( Exception e )
737 if ( lastException != null && allFailed )
739 throw new RbacManagerException( lastException.getMessage(), lastException );
741 return allUserAssignments;
744 public void removeUserAssignment( UserAssignment userAssignment )
745 throws RbacObjectNotFoundException, RbacObjectInvalidException, RbacManagerException
747 boolean allFailed = true;
748 Exception lastException = null;
749 for ( RBACManager rbacManager : rbacManagersPerId.values() )
753 rbacManager.removeUserAssignment( userAssignment );
756 catch ( Exception e )
762 if ( lastException != null && allFailed )
764 throw new RbacManagerException( lastException.getMessage(), lastException );
769 public boolean roleExists( String name )
770 throws RbacManagerException
772 boolean allFailed = true;
773 Exception lastException = null;
774 for ( RBACManager rbacManager : rbacManagersPerId.values() )
778 boolean exists = rbacManager.roleExists( name );
784 catch ( Exception e )
790 if ( lastException != null && allFailed )
792 throw new RbacManagerException( lastException.getMessage(), lastException );
798 public boolean roleExists( Role role )
799 throws RbacManagerException
801 return roleExists( role.getName() );
804 public void eraseDatabase()
806 log.warn( "eraseDatabase not implemented" );
810 public boolean isFinalImplementation()
815 public String getDescriptionKey()
817 return "archiva.redback.rbacmanager.archiva";
820 public boolean isReadOnly()