1 package org.apache.archiva.redback.role;
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.RBACManager;
23 import org.apache.archiva.redback.rbac.RbacManagerException;
24 import org.apache.archiva.redback.rbac.Role;
25 import org.apache.archiva.redback.rbac.UserAssignment;
26 import org.apache.archiva.redback.role.model.ModelApplication;
27 import org.apache.archiva.redback.role.model.ModelRole;
28 import org.apache.archiva.redback.role.model.ModelTemplate;
29 import org.apache.archiva.redback.role.model.RedbackRoleModel;
30 import org.apache.archiva.redback.role.model.io.stax.RedbackRoleModelStaxReader;
31 import org.apache.archiva.redback.role.processor.RoleModelProcessor;
32 import org.apache.commons.io.IOUtils;
33 import org.apache.commons.lang.SystemUtils;
34 import org.apache.archiva.redback.rbac.Resource;
35 import org.apache.archiva.redback.role.template.RoleTemplateProcessor;
36 import org.apache.archiva.redback.role.util.RoleModelUtils;
37 import org.apache.archiva.redback.role.validator.RoleModelValidator;
38 import org.apache.commons.lang.time.StopWatch;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.stereotype.Service;
43 import javax.annotation.PostConstruct;
44 import javax.inject.Inject;
45 import javax.inject.Named;
46 import javax.xml.stream.XMLStreamException;
47 import java.io.IOException;
48 import java.io.InputStreamReader;
49 import java.net.MalformedURLException;
51 import java.util.Arrays;
52 import java.util.Enumeration;
53 import java.util.HashMap;
54 import java.util.List;
60 * @author: Jesse McConnell <jesse@codehaus.org>
62 @Service("roleManager")
63 public class DefaultRoleManager
64 implements RoleManager
66 private Logger log = LoggerFactory.getLogger( DefaultRoleManager.class );
69 * the blessed model that has been validated as complete
71 private RedbackRoleModel blessedModel;
74 * the merged model that can be validated as complete
76 private RedbackRoleModel unblessedModel;
79 * a map of the resources, and the model that they loaded
81 private Map<String, ModelApplication> knownResources = new HashMap<String, ModelApplication>();
84 @Named(value = "roleModelValidator")
85 private RoleModelValidator modelValidator;
88 @Named(value = "roleModelProcessor")
89 private RoleModelProcessor modelProcessor;
92 @Named(value = "roleTemplateProcessor")
93 private RoleTemplateProcessor templateProcessor;
96 @Named(value = "rbacManager#cached")
97 private RBACManager rbacManager;
100 @SuppressWarnings("unchecked")
101 public void loadRoleModel( URL resource )
102 throws RoleManagerException
104 RedbackRoleModelStaxReader reader = new RedbackRoleModelStaxReader();
106 InputStreamReader inputStreamReader = null;
111 inputStreamReader = new InputStreamReader( resource.openStream() );
113 RedbackRoleModel roleModel = reader.read( inputStreamReader );
115 for ( ModelApplication app : roleModel.getApplications() )
117 if ( !knownResources.containsKey( app.getId() ) )
119 log.info( "loading {}", app.getId() );
120 loadApplication( app );
124 catch ( MalformedURLException e )
126 throw new RoleManagerException( "error locating redback profile", e );
128 catch ( IOException e )
130 throw new RoleManagerException( "error reading redback profile", e );
132 catch ( XMLStreamException e )
134 throw new RoleManagerException( "error parsing redback profile", e );
138 IOUtils.closeQuietly( inputStreamReader );
142 @SuppressWarnings("unchecked")
143 public void loadRoleModel( RedbackRoleModel roleModel )
144 throws RoleManagerException
146 for ( ModelApplication app : roleModel.getApplications() )
148 if ( !knownResources.containsKey( app.getId() ) )
150 loadApplication( app );
156 public void loadApplication( ModelApplication app )
157 throws RoleManagerException
159 if ( unblessedModel == null )
161 unblessedModel = new RedbackRoleModel();
164 unblessedModel.addApplication( app );
166 if ( modelValidator.validate( unblessedModel ) )
168 blessedModel = unblessedModel;
172 StringBuilder stringBuilder = new StringBuilder( "Role Model Validation Errors:" );
174 for ( String error : modelValidator.getValidationErrors() )
176 stringBuilder.append( error ).append( SystemUtils.LINE_SEPARATOR );
179 log.error( stringBuilder.toString() );
181 throw new RoleManagerException(
182 "Role Model Validation Error " + SystemUtils.LINE_SEPARATOR + stringBuilder.toString() );
185 modelProcessor.process( blessedModel );
187 knownResources.put( app.getId(), app );
191 * create a role for the given roleName using the resource passed in for
192 * resolving the ${resource} expression
194 public void createTemplatedRole( String templateId, String resource )
195 throws RoleManagerException
197 templateProcessor.create( blessedModel, templateId, resource );
201 * remove the role corresponding to the role using the resource passed in for resolving the
202 * ${resource} expression
204 public void removeTemplatedRole( String templateId, String resource )
205 throws RoleManagerException
207 ModelTemplate template = RoleModelUtils.getModelTemplate( blessedModel, templateId );
209 String roleName = template.getNamePrefix() + template.getDelimiter() + resource;
213 Role role = rbacManager.getRole( roleName );
215 for ( UserAssignment assignment : rbacManager.getUserAssignmentsForRoles(
216 Arrays.asList( role.getName() ) ) )
218 assignment.removeRoleName( role );
219 rbacManager.saveUserAssignment( assignment );
223 catch ( RbacManagerException e )
225 throw new RoleManagerException( "unable to remove role", e );
228 templateProcessor.remove( blessedModel, templateId, resource );
232 * update the role from templateId from oldResource to newResource
234 * NOTE: this requires removal and creation of the role since the jdo store does not tolerate renaming
235 * because of the use of the name as an identifier
237 public void updateRole( String templateId, String oldResource, String newResource )
238 throws RoleManagerException
241 templateProcessor.create( blessedModel, templateId, newResource );
243 ModelTemplate template = RoleModelUtils.getModelTemplate( blessedModel, templateId );
245 String oldRoleName = template.getNamePrefix() + template.getDelimiter() + oldResource;
246 String newRoleName = template.getNamePrefix() + template.getDelimiter() + newResource;
250 Role role = rbacManager.getRole( oldRoleName );
252 // remove the user assignments
253 for ( UserAssignment assignment : rbacManager.getUserAssignmentsForRoles(
254 Arrays.asList( role.getName() ) ) )
256 assignment.removeRoleName( oldRoleName );
257 assignment.addRoleName( newRoleName );
258 rbacManager.saveUserAssignment( assignment );
261 catch ( RbacManagerException e )
263 throw new RoleManagerException( "unable to update role", e );
266 templateProcessor.remove( blessedModel, templateId, oldResource );
269 public void assignRole( String roleId, String principal )
270 throws RoleManagerException
272 ModelRole modelRole = RoleModelUtils.getModelRole( blessedModel, roleId );
274 if ( modelRole == null )
276 throw new RoleManagerException( "Unable to assign role: " + roleId + " does not exist." );
281 UserAssignment userAssignment;
283 if ( rbacManager.userAssignmentExists( principal ) )
285 userAssignment = rbacManager.getUserAssignment( principal );
289 userAssignment = rbacManager.createUserAssignment( principal );
292 userAssignment.addRoleName( modelRole.getName() );
293 rbacManager.saveUserAssignment( userAssignment );
295 catch ( RbacManagerException e )
297 throw new RoleManagerException( "Unable to assign role: unable to manage user assignment", e );
301 public void assignRoleByName( String roleName, String principal )
302 throws RoleManagerException
306 UserAssignment userAssignment;
308 if ( rbacManager.userAssignmentExists( principal ) )
310 userAssignment = rbacManager.getUserAssignment( principal );
314 userAssignment = rbacManager.createUserAssignment( principal );
317 if ( !rbacManager.roleExists( roleName ) )
319 throw new RoleManagerException( "Unable to assign role: " + roleName + " does not exist." );
322 userAssignment.addRoleName( roleName );
323 rbacManager.saveUserAssignment( userAssignment );
325 catch ( RbacManagerException e )
327 throw new RoleManagerException( "Unable to assign role: unable to manage user assignment", e );
331 public void assignTemplatedRole( String templateId, String resource, String principal )
332 throws RoleManagerException
334 ModelTemplate modelTemplate = RoleModelUtils.getModelTemplate( blessedModel, templateId );
336 if ( modelTemplate == null )
338 throw new RoleManagerException( "Unable to assign role: " + templateId + " does not exist." );
342 if ( !rbacManager.resourceExists( resource ) )
344 Resource newResource = rbacManager.createResource( resource );
345 rbacManager.saveResource( newResource );
348 UserAssignment userAssignment;
350 if ( rbacManager.userAssignmentExists( principal ) )
352 userAssignment = rbacManager.getUserAssignment( principal );
356 userAssignment = rbacManager.createUserAssignment( principal );
359 userAssignment.addRoleName( modelTemplate.getNamePrefix() + modelTemplate.getDelimiter() + resource );
360 rbacManager.saveUserAssignment( userAssignment );
362 catch ( RbacManagerException e )
364 throw new RoleManagerException( "Unable to assign role: unable to manage user assignment", e );
368 public void unassignRole( String roleId, String principal )
369 throws RoleManagerException
371 ModelRole modelRole = RoleModelUtils.getModelRole( blessedModel, roleId );
373 if ( modelRole == null )
375 throw new RoleManagerException( "Unable to assign role: " + roleId + " does not exist." );
380 UserAssignment userAssignment;
382 if ( rbacManager.userAssignmentExists( principal ) )
384 userAssignment = rbacManager.getUserAssignment( principal );
388 throw new RoleManagerException(
389 "UserAssignment for principal " + principal + "does not exist, can't unassign role." );
392 userAssignment.removeRoleName( modelRole.getName() );
393 rbacManager.saveUserAssignment( userAssignment );
395 catch ( RbacManagerException e )
397 throw new RoleManagerException( "Unable to unassign role: unable to manage user assignment", e );
401 public void unassignRoleByName( String roleName, String principal )
402 throws RoleManagerException
406 UserAssignment userAssignment;
408 if ( rbacManager.userAssignmentExists( principal ) )
410 userAssignment = rbacManager.getUserAssignment( principal );
414 throw new RoleManagerException(
415 "UserAssignment for principal " + principal + "does not exist, can't unassign role." );
418 if ( !rbacManager.roleExists( roleName ) )
420 throw new RoleManagerException( "Unable to unassign role: " + roleName + " does not exist." );
423 userAssignment.removeRoleName( roleName );
424 rbacManager.saveUserAssignment( userAssignment );
426 catch ( RbacManagerException e )
428 throw new RoleManagerException( "Unable to unassign role: unable to manage user assignment", e );
432 public boolean roleExists( String roleId )
433 throws RoleManagerException
435 ModelRole modelRole = RoleModelUtils.getModelRole( blessedModel, roleId );
437 if ( modelRole == null )
445 if ( rbacManager.roleExists( modelRole.getName() ) )
451 // perhaps try and reload the model here?
452 throw new RoleManagerException(
453 "breakdown in role management, role exists in configuration but was not created in underlying store" );
456 catch ( RbacManagerException e )
458 throw new RoleManagerException( e.getMessage(), e );
463 public boolean templatedRoleExists( String templateId, String resource )
464 throws RoleManagerException
466 ModelTemplate modelTemplate = RoleModelUtils.getModelTemplate( blessedModel, templateId );
468 // template not existing is valid to check, it will throw exception on trying to create
469 if ( modelTemplate == null )
477 if ( rbacManager.roleExists( modelTemplate.getNamePrefix() + modelTemplate.getDelimiter() + resource ) )
486 catch ( RbacManagerException e )
488 throw new RoleManagerException( e.getMessage(), e );
494 public void initialize()
497 StopWatch stopWatch = new StopWatch();
502 URL baseResource = RoleManager.class.getResource( "/META-INF/redback/redback-core.xml" );
504 if ( baseResource == null )
506 throw new RuntimeException( "unable to initialize role manager, missing redback-core.xml" );
509 loadRoleModel( baseResource );
511 Enumeration<URL> enumerator =
512 RoleManager.class.getClassLoader().getResources( "META-INF/redback/redback.xml" );
514 while ( enumerator.hasMoreElements() )
516 URL redbackResource = enumerator.nextElement();
518 loadRoleModel( redbackResource );
521 catch ( RoleManagerException e )
523 throw new RuntimeException( "unable to initialize RoleManager", e );
525 catch ( IOException e )
527 throw new RuntimeException( "unable to initialize RoleManager, problem with redback.xml loading", e );
531 log.info( "DefaultRoleManager initialize time {}", stopWatch.getTime() );
534 public RedbackRoleModel getModel()
539 public void verifyTemplatedRole( String templateId, String resource )
540 throws RoleManagerException
542 // create also serves as update
543 templateProcessor.create( blessedModel, templateId, resource );
546 public RedbackRoleModel getBlessedModel()
551 public void setBlessedModel( RedbackRoleModel blessedModel )
553 this.blessedModel = blessedModel;
556 public RedbackRoleModel getUnblessedModel()
558 return unblessedModel;
561 public void setUnblessedModel( RedbackRoleModel unblessedModel )
563 this.unblessedModel = unblessedModel;
566 public Map<String, ModelApplication> getKnownResources()
568 return knownResources;
571 public void setKnownResources( Map<String, ModelApplication> knownResources )
573 this.knownResources = knownResources;
576 public RoleModelValidator getModelValidator()
578 return modelValidator;
581 public void setModelValidator( RoleModelValidator modelValidator )
583 this.modelValidator = modelValidator;
586 public RoleModelProcessor getModelProcessor()
588 return modelProcessor;
591 public void setModelProcessor( RoleModelProcessor modelProcessor )
593 this.modelProcessor = modelProcessor;
596 public RoleTemplateProcessor getTemplateProcessor()
598 return templateProcessor;
601 public void setTemplateProcessor( RoleTemplateProcessor templateProcessor )
603 this.templateProcessor = templateProcessor;
606 public RBACManager getRbacManager()
611 public void setRbacManager( RBACManager rbacManager )
613 this.rbacManager = rbacManager;