1 package org.apache.archiva.redback.struts2.action.admin;
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.Resource;
23 import org.apache.archiva.redback.rbac.Role;
24 import org.apache.archiva.redback.rbac.UserAssignment;
25 import org.apache.archiva.redback.role.RoleManager;
26 import org.apache.archiva.redback.role.model.ModelApplication;
27 import org.apache.archiva.redback.struts2.action.AbstractUserCredentialsAction;
28 import org.apache.archiva.redback.users.User;
29 import org.apache.archiva.redback.users.UserNotFoundException;
30 import org.apache.archiva.redback.rbac.RbacManagerException;
31 import org.apache.archiva.redback.struts2.action.AuditEvent;
32 import org.apache.archiva.redback.struts2.model.ApplicationRoleDetails;
33 import org.apache.archiva.redback.struts2.model.ApplicationRoleDetails.RoleTableCell;
34 import org.apache.archiva.redback.users.UserManager;
35 import org.codehaus.plexus.util.StringUtils;
36 import org.apache.archiva.redback.integration.interceptor.SecureActionBundle;
37 import org.apache.archiva.redback.integration.interceptor.SecureActionException;
38 import org.apache.archiva.redback.integration.model.AdminEditUserCredentials;
39 import org.apache.archiva.redback.integration.role.RoleConstants;
40 import org.springframework.context.annotation.Scope;
41 import org.springframework.stereotype.Controller;
43 import javax.inject.Inject;
44 import java.util.ArrayList;
45 import java.util.Arrays;
46 import java.util.Collection;
47 import java.util.HashSet;
48 import java.util.Iterator;
49 import java.util.List;
55 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
58 @Controller("redback-assignments")
60 public class AssignmentsAction
61 extends AbstractUserCredentialsAction
63 // ------------------------------------------------------------------
64 // Component Requirements
65 // ------------------------------------------------------------------
71 private RoleManager rmanager;
73 // ------------------------------------------------------------------
75 // ------------------------------------------------------------------
77 private String principal;
79 private AdminEditUserCredentials user;
82 * A List of {@link Role} objects.
84 private List<Role> assignedRoles;
87 * A List of {@link Role} objects.
89 private List<Role> availableRoles;
91 private List<Role> effectivelyAssignedRoles;
94 * List of names (received from client) of dynamic roles to set/unset
96 private List<String> addDSelectedRoles;
99 * List of names (received from client) of nondynamic roles to set/unset
101 private List<String> addNDSelectedRoles;
103 private List<Role> nondynamicroles;
105 private List<Role> dynamicroles;
107 private List<String> NDRoles;
109 private List<String> DRoles;
111 private List<ApplicationRoleDetails> applicationRoleDetails = new ArrayList<ApplicationRoleDetails>();
113 // ------------------------------------------------------------------
114 // Action Entry Points - (aka Names)
115 // ------------------------------------------------------------------
117 public List<ApplicationRoleDetails> getApplicationRoleDetails()
119 return applicationRoleDetails;
123 * Display the edit user panel. <p/> This should consist of the Role details for the specified user. <p/> A table of
124 * currently assigned roles. This table should have a column to remove the role from the user. This table should
125 * also have a column of checkboxes that can be selected and then removed from the user. <p/> A table of roles that
126 * can be assigned. This table should have a set of checkboxes that can be selected and then added to the user. <p/>
127 * Duplicate role assignment needs to be taken care of.
129 * @throws RbacManagerException
130 * @throws org.apache.archiva.redback.rbac.RbacObjectNotFoundException
132 @SuppressWarnings( "unchecked" )
134 throws RbacManagerException
136 this.addNDSelectedRoles = new ArrayList<String>();
137 this.addDSelectedRoles = new ArrayList<String>();
139 if ( StringUtils.isEmpty( principal ) )
141 addActionError( getText( "rbac.edit.user.empty.principal" ) );
145 UserManager userManager = super.securitySystem.getUserManager();
147 if ( !userManager.userExists( principal ) )
149 addActionError( getText( "user.does.not.exist", new String[]{principal} ) );
155 User u = userManager.findUser( principal );
159 addActionError( getText( "cannot.operate.on.null.user" ) );
163 user = new AdminEditUserCredentials( u );
165 catch ( UserNotFoundException e )
167 addActionError( getText( "user.not.found.exception", Arrays.asList( ( Object ) principal, e.getMessage() ) ) );
171 // check first if role assignments for user exist
172 if ( !getManager().userAssignmentExists( principal ) )
174 UserAssignment assignment = getManager().createUserAssignment( principal );
175 getManager().saveUserAssignment( assignment );
178 List<Role> assignableRoles = getFilteredRolesForCurrentUserAccess();
179 List<ApplicationRoleDetails> appRoleDetails = lookupAppRoleDetails( principal, assignableRoles );
180 applicationRoleDetails.addAll( appRoleDetails );
185 @SuppressWarnings( "unchecked" )
186 private List<ApplicationRoleDetails> lookupAppRoleDetails( String principal, List<Role> assignableRoles )
187 throws RbacManagerException
189 List<ApplicationRoleDetails> appRoleDetails = new ArrayList<ApplicationRoleDetails>();
190 for ( Iterator<ModelApplication> i = rmanager.getModel().getApplications().iterator(); i.hasNext(); )
192 ModelApplication application = i.next();
193 ApplicationRoleDetails details =
194 new ApplicationRoleDetails( application, getManager().getEffectivelyAssignedRoles( principal ),
195 getManager().getAssignedRoles( principal ), assignableRoles );
196 appRoleDetails.add( details );
198 return appRoleDetails;
202 * Applies role additions and removals and then displays the edit user panel.
206 public String edituser()
210 Collection<Role> assignedRoles = getManager().getAssignedRoles( principal );
211 List<Role> assignableRoles = getFilteredRolesForCurrentUserAccess();
213 // Compute set of roles usable by configured apps, add/del from this set only
214 List<ApplicationRoleDetails> appRoleDetails = lookupAppRoleDetails( principal, assignableRoles );
215 applicationRoleDetails.addAll( appRoleDetails );
217 Set<String> availableAppRoleNames = new HashSet<String>();
218 for ( ApplicationRoleDetails appRoleDetail : applicationRoleDetails )
220 availableAppRoleNames.addAll( appRoleDetail.getAssignedRoles() );
221 availableAppRoleNames.addAll( appRoleDetail.getAvailableRoles() );
223 // Add dynamic roles offered on page
224 for ( List<RoleTableCell> row : appRoleDetail.getTable() )
226 for ( RoleTableCell col : row )
228 if ( !col.isLabel() )
230 availableAppRoleNames.add( col.getName() );
236 Set<Role> availableRoles = new HashSet<Role>( assignedRoles );
237 availableRoles.addAll( assignableRoles );
239 // Filter the available roles so we only consider configured app roles
240 Iterator<Role> availableRoleIterator = availableRoles.iterator();
241 while ( availableRoleIterator.hasNext() )
243 Role availableRole = availableRoleIterator.next();
244 if ( !availableAppRoleNames.contains( availableRole.getName() ) )
246 availableRoleIterator.remove();
250 List<String> selectedRoleNames = new ArrayList<String>();
251 addSelectedRoles( availableRoles, selectedRoleNames, addNDSelectedRoles );
252 addSelectedRoles( availableRoles, selectedRoleNames, addDSelectedRoles );
254 List<String> newRoles = new ArrayList<String>( selectedRoleNames );
255 String currentUser = getCurrentUser();
256 for ( Role assignedRole : assignedRoles )
258 if ( !selectedRoleNames.contains( assignedRole.getName() ) )
260 // removing a currently assigned role, check if we have permission
261 if ( !availableRoles.contains( assignedRole )
262 || !checkRoleName( assignableRoles, assignedRole.getName() ) )
264 // it may have not been on the page. Leave it assigned.
265 selectedRoleNames.add( assignedRole.getName() );
269 String role = assignedRole.getName();
270 AuditEvent event = new AuditEvent( getText( "log.revoke.role" ) );
271 event.setAffectedUser( principal );
272 event.setRole( role );
273 event.setCurrentUser( currentUser );
279 newRoles.remove( assignedRole.getName() );
282 for ( String r : newRoles )
284 AuditEvent event = new AuditEvent( getText( "log.assign.role" ) );
285 event.setAffectedUser( principal );
287 event.setCurrentUser( currentUser );
291 UserAssignment assignment;
293 if ( getManager().userAssignmentExists( principal ) )
295 assignment = getManager().getUserAssignment( principal );
299 assignment = getManager().createUserAssignment( principal );
302 assignment.setRoleNames( selectedRoleNames );
304 assignment = getManager().saveUserAssignment( assignment );
306 catch ( RbacManagerException ne )
308 addActionError( getText( "error.removing.selected.roles", Arrays.asList( ( Object ) ne.getMessage() ) ) );
314 private void addSelectedRoles( Collection<Role> assignableRoles, List<String> roles, List<String> selectedRoles )
316 if ( selectedRoles != null )
318 for ( String r : selectedRoles )
320 if ( checkRoleName( assignableRoles, r ) )
328 private boolean checkRoleName( Collection<Role> assignableRoles, String r )
330 for ( Role role : assignableRoles )
332 if ( role.getName().equals( r ) )
340 // ------------------------------------------------------------------
341 // Parameter Accessor Methods
342 // ------------------------------------------------------------------
344 public List<Role> getAssignedRoles()
346 return assignedRoles;
349 public void setAssignedRoles( List<Role> assignedRoles )
351 this.assignedRoles = assignedRoles;
354 public List<Role> getAvailableRoles()
356 return availableRoles;
359 public void setAvailableRoles( List<Role> availableRoles )
361 this.availableRoles = availableRoles;
364 public List<Role> getEffectivelyAssignedRoles()
366 return effectivelyAssignedRoles;
369 public void setEffectivelyAssignedRoles( List<Role> effectivelyAssignedRoles )
371 this.effectivelyAssignedRoles = effectivelyAssignedRoles;
374 public String getPrincipal()
379 public void setPrincipal( String principal )
381 this.principal = principal;
384 public void setUsername( String username )
386 this.principal = username;
389 public AdminEditUserCredentials getUser()
394 public SecureActionBundle initSecureActionBundle()
395 throws SecureActionException
397 SecureActionBundle bundle = new SecureActionBundle();
398 bundle.setRequiresAuthentication( true );
399 bundle.addRequiredAuthorization( RoleConstants.USER_MANAGEMENT_USER_EDIT_OPERATION, Resource.GLOBAL );
400 bundle.addRequiredAuthorization( RoleConstants.USER_MANAGEMENT_RBAC_ADMIN_OPERATION, Resource.GLOBAL );
401 bundle.addRequiredAuthorization( RoleConstants.USER_MANAGEMENT_ROLE_GRANT_OPERATION, Resource.GLOBAL );
402 bundle.addRequiredAuthorization( RoleConstants.USER_MANAGEMENT_ROLE_DROP_OPERATION, Resource.GLOBAL );
403 bundle.addRequiredAuthorization( RoleConstants.USER_MANAGEMENT_USER_ROLE_OPERATION, Resource.GLOBAL );
408 public List<Role> getNondynamicroles()
410 return nondynamicroles;
413 public void setNondynamicroles( List<Role> nondynamicroles )
415 this.nondynamicroles = nondynamicroles;
418 public List<Role> getDynamicroles()
423 public void setDynamicroles( List<Role> dynamicroles )
425 this.dynamicroles = dynamicroles;
428 public List<String> getNDRoles()
433 public void setNDRoles( List<String> roles )
438 public List<String> getDRoles()
443 public void setDRoles( List<String> roles )
448 public List<String> getAddDSelectedRoles()
450 return addDSelectedRoles;
453 public void setAddDSelectedRoles( List<String> addDSelectedRoles )
455 this.addDSelectedRoles = addDSelectedRoles;
458 public List<String> getAddNDSelectedRoles()
460 return addNDSelectedRoles;
463 public void setAddNDSelectedRoles( List<String> addNDSelectedRoles )
465 this.addNDSelectedRoles = addNDSelectedRoles;