1 package org.codehaus.plexus.redback.struts2.action;
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.codehaus.plexus.redback.policy.PasswordRuleViolationException;
23 import org.codehaus.plexus.redback.rbac.Permission;
24 import org.codehaus.plexus.redback.rbac.RBACManager;
25 import org.codehaus.plexus.redback.rbac.RbacManagerException;
26 import org.codehaus.plexus.redback.rbac.Resource;
27 import org.codehaus.plexus.redback.rbac.Role;
28 import org.codehaus.plexus.redback.system.SecuritySystem;
29 import org.codehaus.plexus.redback.users.User;
30 import org.codehaus.plexus.util.StringUtils;
31 import org.codehaus.redback.integration.model.UserCredentials;
32 import org.codehaus.redback.integration.role.RoleConstants;
33 import org.codehaus.redback.integration.security.role.RedbackRoleConstants;
34 import org.codehaus.redback.integration.util.RoleSorter;
36 import javax.inject.Inject;
37 import javax.inject.Named;
38 import javax.mail.internet.AddressException;
39 import javax.mail.internet.InternetAddress;
40 import java.util.ArrayList;
41 import java.util.Collections;
42 import java.util.List;
46 * AbstractUserCredentialsAction
48 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
51 public abstract class AbstractUserCredentialsAction
52 extends AbstractSecurityAction
54 // ------------------------------------------------------------------
55 // Component Requirements
56 // ------------------------------------------------------------------
62 @Named( value = "rBACManager#cached" )
63 private RBACManager manager;
69 protected SecuritySystem securitySystem;
71 // ------------------------------------------------------------------
73 // ------------------------------------------------------------------
75 protected UserCredentials internalUser;
77 protected final String VALID_USERNAME_CHARS = "[a-zA-Z_0-9\\-.@]*";
79 public RBACManager getManager()
84 public void setManager( RBACManager manager )
86 this.manager = manager;
89 public SecuritySystem getSecuritySystem()
91 return securitySystem;
94 public void setSecuritySystem( SecuritySystem securitySystem )
96 this.securitySystem = securitySystem;
99 // ------------------------------------------------------------------
100 // Action Entry Points - (aka Names)
101 // ------------------------------------------------------------------
103 public void validateCredentialsLoose()
105 if ( StringUtils.isEmpty( internalUser.getUsername() ) )
107 addFieldError( "user.username", getText( "username.required" ) );
111 if ( !internalUser.getUsername().matches( VALID_USERNAME_CHARS ) )
113 addFieldError( "user.username", getText( "username.invalid.characters" ) );
117 if ( StringUtils.isEmpty( internalUser.getFullName() ) )
119 addFieldError( "user.fullName", getText( "fullName.required" ) );
122 if ( StringUtils.isEmpty( internalUser.getEmail() ) )
124 addFieldError( "user.email", getText( "email.required" ) );
127 if ( !StringUtils.equals( internalUser.getPassword(), internalUser.getConfirmPassword() ) )
129 addFieldError( "user.confirmPassword", getText( "passwords.does.not.match" ) );
134 if ( !StringUtils.isEmpty( internalUser.getEmail() ) )
136 new InternetAddress( internalUser.getEmail(), true );
139 catch ( AddressException e )
141 addFieldError( "user.email", getText( "email.invalid" ) );
145 public void validateCredentialsStrict()
147 validateCredentialsLoose();
149 User tmpuser = internalUser.createUser( securitySystem.getUserManager() );
153 securitySystem.getPolicy().validatePassword( tmpuser );
155 catch ( PasswordRuleViolationException e )
157 processPasswordRuleViolations( e );
160 if ( ( StringUtils.isEmpty( internalUser.getPassword() ) ) )
162 addFieldError( "user.password", getText( "password.required" ) );
167 * this is a hack. this is a hack around the requirements of putting RBAC constraints into the model. this adds one
168 * very major restriction to this security system, that a role name must contain the identifiers of the resource
169 * that is being constrained for adding and granting of roles, this is unacceptable in the long term and we need to
170 * get the model refactored to include this RBAC concept
174 * @throws org.codehaus.plexus.redback.rbac.RbacManagerException
177 protected List<Role> filterRolesForCurrentUserAccess( List<Role> roleList )
178 throws RbacManagerException
180 String currentUser = getCurrentUser();
182 List<Role> filteredRoleList = new ArrayList<Role>();
184 Map<String, List<Permission>> assignedPermissionMap = manager.getAssignedPermissionMap( currentUser );
185 List<String> resourceGrants = new ArrayList<String>();
187 if ( assignedPermissionMap.containsKey( RedbackRoleConstants.USER_MANAGEMENT_ROLE_GRANT_OPERATION ) )
189 List<Permission> roleGrantPermissions =
190 assignedPermissionMap.get( RedbackRoleConstants.USER_MANAGEMENT_ROLE_GRANT_OPERATION );
192 for ( Permission permission : roleGrantPermissions )
194 if ( permission.getResource().getIdentifier().equals( Resource.GLOBAL ) )
196 // the current user has the rights to assign any given role
201 resourceGrants.add( permission.getResource().getIdentifier() );
207 return Collections.emptyList();
210 String delimiter = " - ";
212 // we should have a list of resourceGrants now, this will provide us with the information necessary to restrict
214 for ( Role role : roleList )
216 int delimiterIndex = role.getName().indexOf( delimiter );
217 for ( String resourceIdentifier : resourceGrants )
220 if ( ( role.getName().indexOf( resourceIdentifier ) != -1 ) && ( delimiterIndex != -1 ) )
222 String resourceName = role.getName().substring( delimiterIndex + delimiter.length() );
223 if ( resourceName.equals( resourceIdentifier ) )
225 filteredRoleList.add( role );
231 Collections.sort( filteredRoleList, new RoleSorter() );
232 return filteredRoleList;
235 protected List<Role> getFilteredRolesForCurrentUserAccess()
236 throws RbacManagerException
238 List<Role> roles = manager.getAllRoles();
242 return Collections.emptyList();
245 return filterRolesForCurrentUserAccess( roles );