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.Permission;
23 import org.apache.archiva.redback.rbac.RbacManagerException;
24 import org.apache.archiva.redback.rbac.Resource;
25 import org.apache.archiva.redback.rbac.Role;
26 import org.apache.archiva.redback.rbac.UserAssignment;
27 import org.apache.archiva.redback.users.User;
28 import org.apache.commons.lang.StringEscapeUtils;
29 import org.apache.archiva.redback.struts2.action.AbstractUserCredentialsAction;
30 import org.apache.archiva.redback.struts2.action.AuditEvent;
31 import org.apache.archiva.redback.users.UserManager;
32 import org.apache.archiva.redback.users.UserNotFoundException;
33 import org.codehaus.plexus.util.StringUtils;
34 import org.apache.archiva.redback.integration.interceptor.SecureActionBundle;
35 import org.apache.archiva.redback.integration.interceptor.SecureActionException;
36 import org.apache.archiva.redback.integration.security.role.RedbackRoleConstants;
37 import org.springframework.context.annotation.Scope;
38 import org.springframework.stereotype.Controller;
40 import java.util.ArrayList;
41 import java.util.List;
47 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
50 @Controller( "redback-role-edit" )
52 public class EditRoleAction
53 extends AbstractUserCredentialsAction
55 // ------------------------------------------------------------------
57 // ------------------------------------------------------------------
61 private String description;
63 private String newDescription;
65 private List<String> childRoleNames = new ArrayList<String>();
67 private List<String> parentRoleNames = new ArrayList<String>();
69 private List<Permission> permissions = new ArrayList<Permission>();
71 private List<User> users = new ArrayList<User>();
73 private List<User> parentUsers = new ArrayList<User>();
75 private List<User> allUsers = new ArrayList<User>();
77 private List<String> usersList = new ArrayList<String>();
79 private List<String> availableUsers = new ArrayList<String>();
81 private List<String> currentUsers = new ArrayList<String>();
83 // ------------------------------------------------------------------
84 // Action Entry Points - (aka Names)
85 // ------------------------------------------------------------------
91 addActionError( getText( "cannot.edit.null.role" ) );
95 if ( StringUtils.isEmpty( name ) )
97 addActionError( getText( "cannot.edit.empty.role" ) );
101 name = StringEscapeUtils.escapeXml( name );
103 if ( !getManager().roleExists( name ) )
105 // Means that the role name doesn't exist.
106 // We should exit early and not attempt to look up the role information.
112 if ( !isAuthorized() )
114 log.warn( getCurrentUser() + " isn't authorized to access to the role '" + name + "'" );
115 addActionError( getText( "alert.message" ) );
119 Role role = getManager().getRole( name );
122 addActionError( getText( "cannot.operate.null.role" ) );
126 description = role.getDescription();
127 childRoleNames = role.getChildRoleNames();
128 Map<String, Role> parentRoles = getManager().getParentRoles( role );
129 for ( String roleName : parentRoles.keySet() )
131 parentRoleNames.add( roleName );
133 permissions = role.getPermissions();
135 //Get users of the current role
136 List<String> roles = new ArrayList<String>();
138 List<UserAssignment> userAssignments = getManager().getUserAssignmentsForRoles( roles );
139 users = new ArrayList<User>();
140 if ( userAssignments != null )
142 for ( UserAssignment userAssignment : userAssignments )
146 User user = getUserManager().findUser( userAssignment.getPrincipal() );
149 catch ( UserNotFoundException e )
151 log.warn( "User '" + userAssignment.getPrincipal() + "' doesn't exist.", e );
156 //Get users of the parent roles
157 parentUsers = new ArrayList<User>();
158 if ( !parentRoles.isEmpty() )
160 List<UserAssignment> userParentAssignments =
161 getManager().getUserAssignmentsForRoles( parentRoles.keySet() );
162 if ( userParentAssignments != null )
164 for ( UserAssignment userAssignment : userParentAssignments )
168 User user = getUserManager().findUser( userAssignment.getPrincipal() );
169 parentUsers.add( user );
171 catch ( UserNotFoundException e )
173 log.warn( "User '" + userAssignment.getPrincipal() + "' doesn't exist.", e );
179 catch ( RbacManagerException e )
181 List<Object> list = new ArrayList<Object>();
183 list.add( e.getMessage() );
184 addActionError( getText( "cannot.get.role", list ) );
191 private boolean isAuthorized()
192 throws RbacManagerException
194 List<Role> assignableRoles = getFilteredRolesForCurrentUserAccess();
195 boolean updatableRole = false;
196 for ( Role r : assignableRoles )
198 if ( r.getName().equalsIgnoreCase( name ) )
200 updatableRole = true;
204 return updatableRole;
209 String result = input();
210 if ( ERROR.equals( result ) )
215 newDescription = description;
217 //TODO: Remove all users defined in parent roles too
218 allUsers = getUserManager().getUsers();
220 for ( User user : users )
222 if ( allUsers.contains( user ) )
224 allUsers.remove( user );
228 for ( User user : parentUsers )
230 if ( allUsers.contains( user ) )
232 allUsers.remove( user );
241 String result = input();
242 if ( ERROR.equals( result ) )
249 addActionError( getText( "cannot.edit.null.role" ) );
253 if ( StringUtils.isEmpty( name ) )
255 addActionError( getText( "cannot.edit.empty.role" ) );
262 if ( getManager().roleExists( name ) )
264 role = getManager().getRole( name );
268 role = getManager().createRole( name );
271 //TODO: allow to modify childRoleNames and permissions
272 role.setDescription( newDescription );
273 //role.setChildRoleNames( childRoleNames );
274 //role.setPermissions( permissions );
276 getManager().saveRole( role );
278 List<Object> list = new ArrayList<Object>();
280 String currentUser = getCurrentUser();
281 AuditEvent event = new AuditEvent( getText( "log.role.edit" ) );
282 event.setRole( name );
283 event.setCurrentUser( currentUser );
285 addActionMessage( getText( "save.role.success", list ) );
287 catch ( RbacManagerException e )
289 List<Object> list = new ArrayList<Object>();
291 list.add( e.getMessage() );
292 addActionError( getText( "cannot.get.role", list ) );
299 public String addUsers()
301 if ( availableUsers == null || availableUsers.isEmpty() )
306 for ( String principal : availableUsers )
308 if ( !getUserManager().userExists( principal ) )
310 // Means that the role name doesn't exist.
311 // We need to fail fast and return to the previous page.
312 List<Object> list = new ArrayList<Object>();
313 list.add( principal );
314 addActionError( getText( "user.does.not.exist", list ) );
320 UserAssignment assignment;
322 if ( getManager().userAssignmentExists( principal ) )
324 assignment = getManager().getUserAssignment( principal );
328 assignment = getManager().createUserAssignment( principal );
331 assignment.addRoleName( name );
332 assignment = getManager().saveUserAssignment( assignment );
333 log.info( "{} role assigned to {}", name, principal );
335 catch ( RbacManagerException e )
337 List<Object> list = new ArrayList<Object>();
338 list.add( principal );
339 list.add( e.getMessage() );
340 addActionError( getText( "cannot.assign.role", list ) );
349 public String removeUsers()
351 if ( currentUsers == null || currentUsers.isEmpty() )
356 for ( String principal : currentUsers )
358 if ( !getUserManager().userExists( principal ) )
360 // Means that the role name doesn't exist.
361 // We need to fail fast and return to the previous page.
362 List<Object> list = new ArrayList<Object>();
363 list.add( principal );
364 addActionError( getText( "user.does.not.exist", list ) );
370 UserAssignment assignment;
372 if ( getManager().userAssignmentExists( principal ) )
374 assignment = getManager().getUserAssignment( principal );
378 assignment = getManager().createUserAssignment( principal );
381 assignment.removeRoleName( name );
382 assignment = getManager().saveUserAssignment( assignment );
383 log.info( "{} role unassigned to {}", name, principal );
385 catch ( RbacManagerException e )
387 List<Object> list = new ArrayList<Object>();
388 list.add( principal );
389 list.add( e.getMessage() );
390 addActionError( getText( "cannot.assign.role", list ) );
399 private UserManager getUserManager()
401 return securitySystem.getUserManager();
404 // ------------------------------------------------------------------
405 // Parameter Accessor Methods
406 // ------------------------------------------------------------------
408 public String getName()
413 public void setName( String roleName )
415 this.name = roleName;
418 public List<String> getChildRoleNames()
420 return childRoleNames;
423 public void setChildRoleNames( List<String> childRoleNames )
425 this.childRoleNames = childRoleNames;
428 public String getDescription()
433 public void setDescription( String description )
435 this.description = description;
438 public String getNewDescription()
440 return newDescription;
443 public void setNewDescription( String newDescription )
445 this.newDescription = newDescription;
448 public List<Permission> getPermissions()
453 public void setPermissions( List<Permission> permissions )
455 this.permissions = permissions;
458 public List<User> getUsers()
463 public void setUsers( List<User> users )
468 public List<User> getAllUsers()
473 public void setAllUsers( List<User> allUsers )
475 this.allUsers = allUsers;
478 public List<String> getUsersList()
483 public void setUsersList( List<String> usersList )
485 this.usersList = usersList;
488 public List<String> getAvailableUsers()
490 return availableUsers;
493 public void setAvailableUsers( List<String> availableUsers )
495 this.availableUsers = availableUsers;
498 public List<String> getCurrentUsers()
503 public void setCurrentUsers( List<String> currentUsers )
505 this.currentUsers = currentUsers;
508 public List<String> getParentRoleNames()
510 return parentRoleNames;
513 public void setParentRoleNames( List<String> parentRoleNames )
515 this.parentRoleNames = parentRoleNames;
518 public List<User> getParentUsers()
523 public void setParentUsers( List<User> parentUsers )
525 this.parentUsers = parentUsers;
528 // ------------------------------------------------------------------
529 // Internal Support Methods
530 // ------------------------------------------------------------------
532 public SecureActionBundle initSecureActionBundle()
533 throws SecureActionException
535 SecureActionBundle bundle = new SecureActionBundle();
536 bundle.setRequiresAuthentication( true );
537 bundle.addRequiredAuthorization( RedbackRoleConstants.USER_MANAGEMENT_USER_EDIT_OPERATION, Resource.GLOBAL );
538 bundle.addRequiredAuthorization( RedbackRoleConstants.USER_MANAGEMENT_RBAC_ADMIN_OPERATION, Resource.GLOBAL );
539 bundle.addRequiredAuthorization( RedbackRoleConstants.USER_MANAGEMENT_ROLE_GRANT_OPERATION, Resource.GLOBAL );
540 bundle.addRequiredAuthorization( RedbackRoleConstants.USER_MANAGEMENT_ROLE_DROP_OPERATION, Resource.GLOBAL );
541 bundle.addRequiredAuthorization( RedbackRoleConstants.USER_MANAGEMENT_USER_ROLE_OPERATION, Resource.GLOBAL );