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.keys.AuthenticationKey;
23 import org.apache.archiva.redback.policy.UserSecurityPolicy;
24 import org.apache.archiva.redback.role.RoleManager;
25 import org.apache.archiva.redback.role.RoleManagerException;
26 import org.apache.archiva.redback.users.User;
27 import org.apache.archiva.redback.keys.KeyManagerException;
28 import org.apache.archiva.redback.users.UserManager;
29 import org.apache.archiva.redback.users.UserNotFoundException;
30 import org.apache.archiva.redback.integration.interceptor.SecureActionBundle;
31 import org.apache.archiva.redback.integration.interceptor.SecureActionException;
32 import org.apache.archiva.redback.integration.mail.Mailer;
33 import org.apache.archiva.redback.integration.model.CreateUserCredentials;
34 import org.apache.archiva.redback.integration.security.role.RedbackRoleConstants;
35 import org.springframework.context.annotation.Scope;
36 import org.springframework.stereotype.Controller;
38 import javax.inject.Inject;
39 import java.util.Arrays;
44 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
47 @Controller( "redback-register" )
49 public class RegisterAction
50 extends AbstractUserCredentialsAction
51 implements CancellableAction
53 protected static final String REGISTER_SUCCESS = "security-register-success";
55 private static final String VALIDATION_NOTE = "validation-note";
57 private static final String RESEND_VALIDATION_EMAIL = "security-resend-validation-email";
59 // ------------------------------------------------------------------
60 // Component Requirements
61 // ------------------------------------------------------------------
67 private Mailer mailer;
73 private RoleManager roleManager;
75 private CreateUserCredentials user;
77 private boolean emailValidationRequired;
79 private String username;
81 // ------------------------------------------------------------------
82 // Action Entry Points - (aka Names)
83 // ------------------------------------------------------------------
89 user = new CreateUserCredentials();
92 emailValidationRequired = securitySystem.getPolicy().getUserValidationSettings().isEmailValidationRequired();
97 public String register()
101 user = new CreateUserCredentials();
102 addActionError( getText( "invalid.user.credentials" ) );
106 UserSecurityPolicy securityPolicy = securitySystem.getPolicy();
108 emailValidationRequired = securityPolicy.getUserValidationSettings().isEmailValidationRequired();
112 if ( securityPolicy.getUserValidationSettings().isEmailValidationRequired() )
114 validateCredentialsLoose();
118 validateCredentialsStrict();
121 // NOTE: Do not perform Password Rules Validation Here.
122 UserManager manager = super.securitySystem.getUserManager();
124 if ( manager.userExists( user.getUsername() ) )
126 // Means that the role name doesn't exist.
127 // We need to fail fast and return to the previous page.
128 addActionError( getText( "user.already.exists", Arrays.asList( (Object) user.getUsername() ) ) );
131 if ( hasActionErrors() || hasFieldErrors() )
136 User u = manager.createUser( user.getUsername(), user.getFullName(), user.getEmail() );
137 u.setPassword( user.getPassword() );
138 u.setValidated( false );
139 u.setLocked( false );
143 roleManager.assignRole( RedbackRoleConstants.REGISTERED_USER_ROLE_ID, u.getPrincipal().toString() );
145 catch ( RoleManagerException rpe )
147 addActionError( getText( "assign.role.failure" ) );
148 log.error( "RoleProfile Error: " + rpe.getMessage(), rpe );
152 if ( securityPolicy.getUserValidationSettings().isEmailValidationRequired() )
158 AuthenticationKey authkey =
159 securitySystem.getKeyManager().createKey( u.getPrincipal().toString(), "New User Email Validation",
160 securityPolicy.getUserValidationSettings().getEmailValidationTimeout() );
162 mailer.sendAccountValidationEmail( Arrays.asList( u.getEmail() ), authkey, getBaseUrl() );
164 securityPolicy.setEnabled( false );
165 manager.addUser( u );
167 return VALIDATION_NOTE;
169 catch ( KeyManagerException e )
171 addActionError( getText( "cannot.register.user" ) );
172 log.error( "Unable to register a new user.", e );
177 securityPolicy.setEnabled( true );
182 manager.addUser( u );
185 AuditEvent event = new AuditEvent( getText( "log.account.create" ) );
186 event.setAffectedUser( username );
189 return REGISTER_SUCCESS;
192 public String resendRegistrationEmail()
194 UserSecurityPolicy securityPolicy = securitySystem.getPolicy();
198 User user = super.securitySystem.getUserManager().findUser( username );
200 AuthenticationKey authkey =
201 securitySystem.getKeyManager().createKey( user.getPrincipal().toString(), "New User Email Validation",
202 securityPolicy.getUserValidationSettings().getEmailValidationTimeout() );
204 mailer.sendAccountValidationEmail( Arrays.asList( user.getEmail() ), authkey, getBaseUrl() );
206 return RESEND_VALIDATION_EMAIL;
208 catch ( KeyManagerException e )
210 addActionError( getText( "cannot.register.user" ) );
211 log.error( "Unable to register a new user.", e );
214 catch ( UserNotFoundException e )
216 addActionError( getText( "cannot.find.user" ) );
217 log.error( "Unable to find user.", e );
222 public String cancel()
227 // ------------------------------------------------------------------
228 // Parameter Accessor Methods
229 // ------------------------------------------------------------------
231 public CreateUserCredentials getUser()
236 public void setUser( CreateUserCredentials user )
241 public boolean isEmailValidationRequired()
243 return emailValidationRequired;
246 public void setEmailValidationRequired( boolean emailValidationRequired )
248 this.emailValidationRequired = emailValidationRequired;
251 public String getUsername()
256 public void setUsername( String username )
258 this.username = username;
261 public SecureActionBundle initSecureActionBundle()
262 throws SecureActionException
264 return SecureActionBundle.OPEN;