1 package org.codehaus.plexus.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.commons.lang.StringEscapeUtils;
23 import org.codehaus.plexus.redback.rbac.Permission;
24 import org.codehaus.plexus.redback.rbac.RbacManagerException;
25 import org.codehaus.plexus.redback.rbac.Resource;
26 import org.codehaus.plexus.redback.rbac.Role;
27 import org.codehaus.plexus.redback.rbac.UserAssignment;
28 import org.codehaus.plexus.redback.struts2.action.AbstractUserCredentialsAction;
29 import org.codehaus.plexus.redback.struts2.action.AuditEvent;
30 import org.codehaus.plexus.redback.users.User;
31 import org.codehaus.plexus.redback.users.UserManager;
32 import org.codehaus.plexus.redback.users.UserNotFoundException;
33 import org.codehaus.plexus.util.StringUtils;
34 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
35 import org.codehaus.redback.integration.interceptor.SecureActionException;
36 import org.codehaus.redback.integration.role.RoleConstants;
37 import org.codehaus.redback.integration.security.role.RedbackRoleConstants;
38 import org.springframework.context.annotation.Scope;
39 import org.springframework.stereotype.Controller;
41 import java.util.ArrayList;
42 import java.util.List;
48 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
51 @Controller( "redback-role-edit" )
53 public class EditRoleAction
54 extends AbstractUserCredentialsAction
56 // ------------------------------------------------------------------
58 // ------------------------------------------------------------------
62 private String description;
64 private String newDescription;
66 private List<String> childRoleNames = new ArrayList<String>();
68 private List<String> parentRoleNames = new ArrayList<String>();
70 private List<Permission> permissions = new ArrayList<Permission>();
72 private List<User> users = new ArrayList<User>();
74 private List<User> parentUsers = new ArrayList<User>();
76 private List<User> allUsers = new ArrayList<User>();
78 private List<String> usersList = new ArrayList<String>();
80 private List<String> availableUsers = new ArrayList<String>();
82 private List<String> currentUsers = new ArrayList<String>();
84 // ------------------------------------------------------------------
85 // Action Entry Points - (aka Names)
86 // ------------------------------------------------------------------
92 addActionError( getText( "cannot.edit.null.role" ) );
96 if ( StringUtils.isEmpty( name ) )
98 addActionError( getText( "cannot.edit.empty.role" ) );
102 name = StringEscapeUtils.escapeXml( name );
104 if ( !getManager().roleExists( name ) )
106 // Means that the role name doesn't exist.
107 // We should exit early and not attempt to look up the role information.
113 if ( !isAuthorized() )
115 log.warn( getCurrentUser() + " isn't authorized to access to the role '" + name + "'" );
116 addActionError( getText( "alert.message" ) );
120 Role role = getManager().getRole( name );
123 addActionError( getText( "cannot.operate.null.role" ) );
127 description = role.getDescription();
128 childRoleNames = role.getChildRoleNames();
129 Map<String, Role> parentRoles = getManager().getParentRoles( role );
130 for ( String roleName : parentRoles.keySet() )
132 parentRoleNames.add( roleName );
134 permissions = role.getPermissions();
136 //Get users of the current role
137 List<String> roles = new ArrayList<String>();
139 List<UserAssignment> userAssignments = getManager().getUserAssignmentsForRoles( roles );
140 users = new ArrayList<User>();
141 if ( userAssignments != null )
143 for ( UserAssignment userAssignment : userAssignments )
147 User user = getUserManager().findUser( userAssignment.getPrincipal() );
150 catch ( UserNotFoundException e )
152 log.warn( "User '" + userAssignment.getPrincipal() + "' doesn't exist.", e );
157 //Get users of the parent roles
158 parentUsers = new ArrayList<User>();
159 if ( !parentRoles.isEmpty() )
161 List<UserAssignment> userParentAssignments =
162 getManager().getUserAssignmentsForRoles( parentRoles.keySet() );
163 if ( userParentAssignments != null )
165 for ( UserAssignment userAssignment : userParentAssignments )
169 User user = getUserManager().findUser( userAssignment.getPrincipal() );
170 parentUsers.add( user );
172 catch ( UserNotFoundException e )
174 log.warn( "User '" + userAssignment.getPrincipal() + "' doesn't exist.", e );
180 catch ( RbacManagerException e )
182 List<Object> list = new ArrayList<Object>();
184 list.add( e.getMessage() );
185 addActionError( getText( "cannot.get.role", list ) );
192 private boolean isAuthorized()
193 throws RbacManagerException
195 List<Role> assignableRoles = getFilteredRolesForCurrentUserAccess();
196 boolean updatableRole = false;
197 for ( Role r : assignableRoles )
199 if ( r.getName().equalsIgnoreCase( name ) )
201 updatableRole = true;
205 return updatableRole;
210 String result = input();
211 if ( ERROR.equals( result ) )
216 newDescription = description;
218 //TODO: Remove all users defined in parent roles too
219 allUsers = getUserManager().getUsers();
221 for ( User user : users )
223 if ( allUsers.contains( user ) )
225 allUsers.remove( user );
229 for ( User user : parentUsers )
231 if ( allUsers.contains( user ) )
233 allUsers.remove( user );
242 String result = input();
243 if ( ERROR.equals( result ) )
250 addActionError( getText( "cannot.edit.null.role" ) );
254 if ( StringUtils.isEmpty( name ) )
256 addActionError( getText( "cannot.edit.empty.role" ) );
263 if ( getManager().roleExists( name ) )
265 role = getManager().getRole( name );
269 role = getManager().createRole( name );
272 //TODO: allow to modify childRoleNames and permissions
273 role.setDescription( newDescription );
274 //role.setChildRoleNames( childRoleNames );
275 //role.setPermissions( permissions );
277 getManager().saveRole( role );
279 List<Object> list = new ArrayList<Object>();
281 String currentUser = getCurrentUser();
282 AuditEvent event = new AuditEvent( getText( "log.role.edit" ) );
283 event.setRole( name );
284 event.setCurrentUser( currentUser );
286 addActionMessage( getText( "save.role.success", list ) );
288 catch ( RbacManagerException e )
290 List<Object> list = new ArrayList<Object>();
292 list.add( e.getMessage() );
293 addActionError( getText( "cannot.get.role", list ) );
300 public String addUsers()
302 if ( availableUsers == null || availableUsers.isEmpty() )
307 for ( String principal : availableUsers )
309 if ( !getUserManager().userExists( principal ) )
311 // Means that the role name doesn't exist.
312 // We need to fail fast and return to the previous page.
313 List<Object> list = new ArrayList<Object>();
314 list.add( principal );
315 addActionError( getText( "user.does.not.exist", list ) );
321 UserAssignment assignment;
323 if ( getManager().userAssignmentExists( principal ) )
325 assignment = getManager().getUserAssignment( principal );
329 assignment = getManager().createUserAssignment( principal );
332 assignment.addRoleName( name );
333 assignment = getManager().saveUserAssignment( assignment );
334 log.info( "{} role assigned to {}", name, principal );
336 catch ( RbacManagerException e )
338 List<Object> list = new ArrayList<Object>();
339 list.add( principal );
340 list.add( e.getMessage() );
341 addActionError( getText( "cannot.assign.role", list ) );
350 public String removeUsers()
352 if ( currentUsers == null || currentUsers.isEmpty() )
357 for ( String principal : currentUsers )
359 if ( !getUserManager().userExists( principal ) )
361 // Means that the role name doesn't exist.
362 // We need to fail fast and return to the previous page.
363 List<Object> list = new ArrayList<Object>();
364 list.add( principal );
365 addActionError( getText( "user.does.not.exist", list ) );
371 UserAssignment assignment;
373 if ( getManager().userAssignmentExists( principal ) )
375 assignment = getManager().getUserAssignment( principal );
379 assignment = getManager().createUserAssignment( principal );
382 assignment.removeRoleName( name );
383 assignment = getManager().saveUserAssignment( assignment );
384 log.info( "{} role unassigned to {}", name, principal );
386 catch ( RbacManagerException e )
388 List<Object> list = new ArrayList<Object>();
389 list.add( principal );
390 list.add( e.getMessage() );
391 addActionError( getText( "cannot.assign.role", list ) );
400 private UserManager getUserManager()
402 return securitySystem.getUserManager();
405 // ------------------------------------------------------------------
406 // Parameter Accessor Methods
407 // ------------------------------------------------------------------
409 public String getName()
414 public void setName( String roleName )
416 this.name = roleName;
419 public List<String> getChildRoleNames()
421 return childRoleNames;
424 public void setChildRoleNames( List<String> childRoleNames )
426 this.childRoleNames = childRoleNames;
429 public String getDescription()
434 public void setDescription( String description )
436 this.description = description;
439 public String getNewDescription()
441 return newDescription;
444 public void setNewDescription( String newDescription )
446 this.newDescription = newDescription;
449 public List<Permission> getPermissions()
454 public void setPermissions( List<Permission> permissions )
456 this.permissions = permissions;
459 public List<User> getUsers()
464 public void setUsers( List<User> users )
469 public List<User> getAllUsers()
474 public void setAllUsers( List<User> allUsers )
476 this.allUsers = allUsers;
479 public List<String> getUsersList()
484 public void setUsersList( List<String> usersList )
486 this.usersList = usersList;
489 public List<String> getAvailableUsers()
491 return availableUsers;
494 public void setAvailableUsers( List<String> availableUsers )
496 this.availableUsers = availableUsers;
499 public List<String> getCurrentUsers()
504 public void setCurrentUsers( List<String> currentUsers )
506 this.currentUsers = currentUsers;
509 public List<String> getParentRoleNames()
511 return parentRoleNames;
514 public void setParentRoleNames( List<String> parentRoleNames )
516 this.parentRoleNames = parentRoleNames;
519 public List<User> getParentUsers()
524 public void setParentUsers( List<User> parentUsers )
526 this.parentUsers = parentUsers;
529 // ------------------------------------------------------------------
530 // Internal Support Methods
531 // ------------------------------------------------------------------
533 public SecureActionBundle initSecureActionBundle()
534 throws SecureActionException
536 SecureActionBundle bundle = new SecureActionBundle();
537 bundle.setRequiresAuthentication( true );
538 bundle.addRequiredAuthorization( RedbackRoleConstants.USER_MANAGEMENT_USER_EDIT_OPERATION, Resource.GLOBAL );
539 bundle.addRequiredAuthorization( RedbackRoleConstants.USER_MANAGEMENT_RBAC_ADMIN_OPERATION, Resource.GLOBAL );
540 bundle.addRequiredAuthorization( RedbackRoleConstants.USER_MANAGEMENT_ROLE_GRANT_OPERATION, Resource.GLOBAL );
541 bundle.addRequiredAuthorization( RedbackRoleConstants.USER_MANAGEMENT_ROLE_DROP_OPERATION, Resource.GLOBAL );
542 bundle.addRequiredAuthorization( RedbackRoleConstants.USER_MANAGEMENT_USER_ROLE_OPERATION, Resource.GLOBAL );