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.Collection;
39 import java.util.LinkedHashMap;
40 import java.util.List;
44 * @author Olivier Lamy
47 @Service("rbacManager#archiva")
48 public class ArchivaRbacManager
49 extends AbstractRBACManager
50 implements RBACManager
53 private Map<String, RBACManager> rbacManagersPerId;
56 private ApplicationContext applicationContext;
59 private RedbackRuntimeConfigurationAdmin redbackRuntimeConfigurationAdmin;
62 public void initialize()
66 List<String> rbacManagerIds =
67 redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getRbacManagerImpls();
69 log.info( "use rbacManagerIds: '{}'", rbacManagerIds );
71 this.rbacManagersPerId = new LinkedHashMap<String, RBACManager>( rbacManagerIds.size() );
73 for ( String id : rbacManagerIds )
75 RBACManager rbacManager = applicationContext.getBean( "rbacManager#" + id, RBACManager.class );
77 rbacManagersPerId.put( id, rbacManager );
80 catch ( RepositoryAdminException e )
82 // revert to a default one ?
83 log.error( e.getMessage(), e );
84 throw new RuntimeException( e.getMessage(), e );
88 protected RBACManager getRbacManagerForCommon()
90 return this.rbacManagersPerId.values().iterator().next();
93 public Role createRole( String name )
95 return getRbacManagerForCommon().createRole( name );
98 public Role saveRole( Role role )
99 throws RbacObjectInvalidException, RbacManagerException
101 return getRbacManagerForCommon().saveRole( role );
104 public void saveRoles( Collection<Role> roles )
105 throws RbacObjectInvalidException, RbacManagerException
107 getRbacManagerForCommon().saveRoles( roles );
110 public Role getRole( String roleName )
111 throws RbacObjectNotFoundException, RbacManagerException
113 for ( RBACManager rbacManager : rbacManagersPerId.values() )
115 Role role = rbacManager.getRole( roleName );
121 log.debug( "cannot find role for name: ‘{}", roleName );
125 public List<Role> getAllRoles()
126 throws RbacManagerException
128 // iterate and aggregate results ?
129 return getRbacManagerForCommon().getAllRoles();
132 public void removeRole( Role role )
133 throws RbacObjectNotFoundException, RbacObjectInvalidException, RbacManagerException
136 getRbacManagerForCommon().removeRole( role );
139 public Permission createPermission( String name )
140 throws RbacManagerException
143 return getRbacManagerForCommon().createPermission( name );
146 public Permission createPermission( String name, String operationName, String resourceIdentifier )
147 throws RbacManagerException
150 return getRbacManagerForCommon().createPermission( name, operationName, resourceIdentifier );
153 public Permission savePermission( Permission permission )
154 throws RbacObjectInvalidException, RbacManagerException
157 return getRbacManagerForCommon().savePermission( permission );
160 public Permission getPermission( String permissionName )
161 throws RbacObjectNotFoundException, RbacManagerException
164 return getRbacManagerForCommon().getPermission( permissionName );
167 public List<Permission> getAllPermissions()
168 throws RbacManagerException
170 // iterate and aggregate ?
171 return getRbacManagerForCommon().getAllPermissions();
174 public void removePermission( Permission permission )
175 throws RbacObjectNotFoundException, RbacObjectInvalidException, RbacManagerException
178 getRbacManagerForCommon().removePermission( permission );
181 public Operation createOperation( String name )
182 throws RbacManagerException
185 return getRbacManagerForCommon().createOperation( name );
188 public Operation saveOperation( Operation operation )
189 throws RbacObjectInvalidException, RbacManagerException
192 return getRbacManagerForCommon().saveOperation( operation );
195 public Operation getOperation( String operationName )
196 throws RbacObjectNotFoundException, RbacManagerException
199 return getRbacManagerForCommon().getOperation( operationName );
202 public List<Operation> getAllOperations()
203 throws RbacManagerException
205 // iterate and aggregate ?
206 return getRbacManagerForCommon().getAllOperations();
209 public void removeOperation( Operation operation )
210 throws RbacObjectNotFoundException, RbacObjectInvalidException, RbacManagerException
213 getRbacManagerForCommon().removeOperation( operation );
216 public Resource createResource( String identifier )
217 throws RbacManagerException
220 return getRbacManagerForCommon().createResource( identifier );
223 public Resource saveResource( Resource resource )
224 throws RbacObjectInvalidException, RbacManagerException
227 return getRbacManagerForCommon().saveResource( resource );
230 public Resource getResource( String resourceIdentifier )
231 throws RbacObjectNotFoundException, RbacManagerException
234 return getRbacManagerForCommon().getResource( resourceIdentifier );
237 public List<Resource> getAllResources()
238 throws RbacManagerException
240 // iterate and aggregate ?
241 return getRbacManagerForCommon().getAllResources();
244 public void removeResource( Resource resource )
245 throws RbacObjectNotFoundException, RbacObjectInvalidException, RbacManagerException
248 getRbacManagerForCommon().removeResource( resource );
251 public UserAssignment createUserAssignment( String principal )
252 throws RbacManagerException
255 return getRbacManagerForCommon().createUserAssignment( principal );
258 public UserAssignment saveUserAssignment( UserAssignment userAssignment )
259 throws RbacObjectInvalidException, RbacManagerException
262 return getRbacManagerForCommon().saveUserAssignment( userAssignment );
265 public UserAssignment getUserAssignment( String principal )
266 throws RbacObjectNotFoundException, RbacManagerException
269 return getRbacManagerForCommon().getUserAssignment( principal );
273 public boolean userAssignmentExists( String principal )
275 return getRbacManagerForCommon().userAssignmentExists( principal );
279 public boolean userAssignmentExists( UserAssignment assignment )
281 return getRbacManagerForCommon().userAssignmentExists( assignment );
284 public List<UserAssignment> getAllUserAssignments()
285 throws RbacManagerException
288 return getRbacManagerForCommon().getAllUserAssignments();
291 public List<UserAssignment> getUserAssignmentsForRoles( Collection<String> roleNames )
292 throws RbacManagerException
295 return getRbacManagerForCommon().getUserAssignmentsForRoles( roleNames );
298 public void removeUserAssignment( UserAssignment userAssignment )
299 throws RbacObjectNotFoundException, RbacObjectInvalidException, RbacManagerException
302 getRbacManagerForCommon().removeUserAssignment( userAssignment );
306 public boolean roleExists( String name )
307 throws RbacManagerException
309 boolean exists = false;
310 for ( RBACManager manager : rbacManagersPerId.values() )
312 exists = manager.roleExists( name );
322 public boolean roleExists( Role role )
323 throws RbacManagerException
325 return roleExists( role.getName() );
328 public void eraseDatabase()
330 log.warn( "eraseDatabase not implemented" );