1 package org.apache.archiva.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.apache.archiva.redback.rbac.Permission;
23 import org.apache.archiva.redback.rbac.RBACManager;
24 import org.apache.archiva.redback.rbac.Resource;
25 import org.apache.archiva.redback.rbac.Role;
26 import org.apache.archiva.redback.users.User;
27 import org.apache.archiva.redback.policy.PasswordRuleViolationException;
28 import org.apache.archiva.redback.rbac.RbacManagerException;
29 import org.apache.archiva.redback.system.SecuritySystem;
30 import org.codehaus.plexus.util.StringUtils;
31 import org.apache.archiva.redback.integration.model.UserCredentials;
32 import org.apache.archiva.redback.integration.security.role.RedbackRoleConstants;
33 import org.apache.archiva.redback.integration.util.RoleSorter;
35 import javax.inject.Inject;
36 import javax.inject.Named;
37 import javax.mail.internet.AddressException;
38 import javax.mail.internet.InternetAddress;
39 import java.util.ArrayList;
40 import java.util.Collections;
41 import java.util.List;
45 * AbstractUserCredentialsAction
47 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
50 public abstract class AbstractUserCredentialsAction
51 extends AbstractSecurityAction
53 // ------------------------------------------------------------------
54 // Component Requirements
55 // ------------------------------------------------------------------
61 @Named( value = "rBACManager#cached" )
62 private RBACManager manager;
68 protected SecuritySystem securitySystem;
70 // ------------------------------------------------------------------
72 // ------------------------------------------------------------------
74 protected UserCredentials internalUser;
76 protected final String VALID_USERNAME_CHARS = "[a-zA-Z_0-9\\-.@]*";
78 public RBACManager getManager()
83 public void setManager( RBACManager manager )
85 this.manager = manager;
88 public SecuritySystem getSecuritySystem()
90 return securitySystem;
93 public void setSecuritySystem( SecuritySystem securitySystem )
95 this.securitySystem = securitySystem;
98 // ------------------------------------------------------------------
99 // Action Entry Points - (aka Names)
100 // ------------------------------------------------------------------
102 public void validateCredentialsLoose()
104 if ( StringUtils.isEmpty( internalUser.getUsername() ) )
106 addFieldError( "user.username", getText( "username.required" ) );
110 if ( !internalUser.getUsername().matches( VALID_USERNAME_CHARS ) )
112 addFieldError( "user.username", getText( "username.invalid.characters" ) );
116 if ( StringUtils.isEmpty( internalUser.getFullName() ) )
118 addFieldError( "user.fullName", getText( "fullName.required" ) );
121 if ( StringUtils.isEmpty( internalUser.getEmail() ) )
123 addFieldError( "user.email", getText( "email.required" ) );
126 if ( !StringUtils.equals( internalUser.getPassword(), internalUser.getConfirmPassword() ) )
128 addFieldError( "user.confirmPassword", getText( "passwords.does.not.match" ) );
133 if ( !StringUtils.isEmpty( internalUser.getEmail() ) )
135 new InternetAddress( internalUser.getEmail(), true );
138 catch ( AddressException e )
140 addFieldError( "user.email", getText( "email.invalid" ) );
144 public void validateCredentialsStrict()
146 validateCredentialsLoose();
148 User tmpuser = internalUser.createUser( securitySystem.getUserManager() );
152 securitySystem.getPolicy().validatePassword( tmpuser );
154 catch ( PasswordRuleViolationException e )
156 processPasswordRuleViolations( e );
159 if ( ( StringUtils.isEmpty( internalUser.getPassword() ) ) )
161 addFieldError( "user.password", getText( "password.required" ) );
166 * this is a hack. this is a hack around the requirements of putting RBAC constraints into the model. this adds one
167 * very major restriction to this security system, that a role name must contain the identifiers of the resource
168 * that is being constrained for adding and granting of roles, this is unacceptable in the long term and we need to
169 * get the model refactored to include this RBAC concept
173 * @throws org.apache.archiva.redback.rbac.RbacManagerException
176 protected List<Role> filterRolesForCurrentUserAccess( List<Role> roleList )
177 throws RbacManagerException
179 String currentUser = getCurrentUser();
181 List<Role> filteredRoleList = new ArrayList<Role>();
183 Map<String, List<Permission>> assignedPermissionMap = manager.getAssignedPermissionMap( currentUser );
184 List<String> resourceGrants = new ArrayList<String>();
186 if ( assignedPermissionMap.containsKey( RedbackRoleConstants.USER_MANAGEMENT_ROLE_GRANT_OPERATION ) )
188 List<Permission> roleGrantPermissions =
189 assignedPermissionMap.get( RedbackRoleConstants.USER_MANAGEMENT_ROLE_GRANT_OPERATION );
191 for ( Permission permission : roleGrantPermissions )
193 if ( permission.getResource().getIdentifier().equals( Resource.GLOBAL ) )
195 // the current user has the rights to assign any given role
200 resourceGrants.add( permission.getResource().getIdentifier() );
206 return Collections.emptyList();
209 String delimiter = " - ";
211 // we should have a list of resourceGrants now, this will provide us with the information necessary to restrict
213 for ( Role role : roleList )
215 int delimiterIndex = role.getName().indexOf( delimiter );
216 for ( String resourceIdentifier : resourceGrants )
219 if ( ( role.getName().indexOf( resourceIdentifier ) != -1 ) && ( delimiterIndex != -1 ) )
221 String resourceName = role.getName().substring( delimiterIndex + delimiter.length() );
222 if ( resourceName.equals( resourceIdentifier ) )
224 filteredRoleList.add( role );
230 Collections.sort( filteredRoleList, new RoleSorter() );
231 return filteredRoleList;
234 protected List<Role> getFilteredRolesForCurrentUserAccess()
235 throws RbacManagerException
237 List<Role> roles = manager.getAllRoles();
241 return Collections.emptyList();
244 return filterRolesForCurrentUserAccess( roles );