1 package org.codehaus.plexus.redback.struts2.action.admin;
4 * Copyright 2005-2006 The Codehaus.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.apache.struts2.ServletActionContext;
20 import org.codehaus.plexus.redback.authentication.AuthenticationConstants;
21 import org.codehaus.plexus.redback.authentication.AuthenticationDataSource;
22 import org.codehaus.plexus.redback.authentication.AuthenticationException;
23 import org.codehaus.plexus.redback.authentication.AuthenticationResult;
24 import org.codehaus.plexus.redback.authentication.PasswordBasedAuthenticationDataSource;
25 import org.codehaus.plexus.redback.configuration.UserConfiguration;
26 import org.codehaus.plexus.redback.policy.AccountLockedException;
27 import org.codehaus.plexus.redback.policy.MustChangePasswordException;
28 import org.codehaus.plexus.redback.role.RoleManager;
29 import org.codehaus.plexus.redback.role.RoleManagerException;
30 import org.codehaus.plexus.redback.struts2.action.AuditEvent;
31 import org.codehaus.plexus.redback.system.SecuritySession;
32 import org.codehaus.plexus.redback.users.User;
33 import org.codehaus.plexus.redback.users.UserManager;
34 import org.codehaus.plexus.redback.users.UserNotFoundException;
35 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
36 import org.codehaus.redback.integration.interceptor.SecureActionException;
37 import org.codehaus.redback.integration.model.EditUserCredentials;
38 import org.codehaus.redback.integration.util.AutoLoginCookies;
39 import org.springframework.context.annotation.Scope;
40 import org.springframework.stereotype.Controller;
42 import javax.inject.Inject;
43 import java.util.Arrays;
44 import java.util.Date;
49 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
52 @Controller( "redback-admin-account" )
54 public class AddAdminUserAction
55 extends AbstractAdminUserCredentialsAction
57 private static final String LOGIN_ERROR = "login-error";
59 private static final String LOGIN_SUCCESS = "security-login-success";
61 private static final String PASSWORD_CHANGE = "security-must-change-password";
63 private static final String ACCOUNT_LOCKED = "security-login-locked";
66 private RoleManager roleManager;
70 private UserConfiguration config;
72 private EditUserCredentials user;
75 private AutoLoginCookies autologinCookies;
81 user = new EditUserCredentials( config.getString( "redback.default.admin" ) );
88 * TODO this must done in a service !!
91 public String submit()
95 user = new EditUserCredentials( config.getString( "redback.default.admin" ) );
96 addActionError( getText( "invalid.admin.credentials" ) );
100 log.info( "user = {}", user );
104 validateCredentialsStrict();
106 UserManager userManager = super.securitySystem.getUserManager();
108 if ( userManager.userExists( config.getString( "redback.default.admin" ) ) )
110 // Means that the role name exist already.
111 // We need to fail fast and return to the previous page.
112 addActionError( getText( "admin.user.already.exists" ) );
116 if ( hasActionErrors() || hasFieldErrors() )
122 userManager.createUser( config.getString( "redback.default.admin" ), user.getFullName(), user.getEmail() );
125 addActionError( getText( "cannot.operate.on.null.user" ) );
129 u.setPassword( user.getPassword() );
130 u.setLocked( false );
131 u.setPasswordChangeRequired( false );
132 u.setPermanent( true );
134 userManager.addUser( u );
136 AuditEvent event = new AuditEvent( getText( "log.account.create" ) );
137 event.setAffectedUser( u.getUsername() );
142 roleManager.assignRole( "system-administrator", u.getPrincipal().toString() );
143 event = new AuditEvent( getText( "log.assign.role" ) );
144 event.setAffectedUser( u.getUsername() );
145 event.setRole( "system-administrator" );
148 catch ( RoleManagerException rpe )
150 addActionError( getText( "cannot.assign.admin.role" ) );
154 PasswordBasedAuthenticationDataSource authdatasource = new PasswordBasedAuthenticationDataSource();
155 authdatasource.setPrincipal( user.getUsername() );
156 authdatasource.setPassword( user.getPassword() );
158 return webLogin( authdatasource );
161 public EditUserCredentials getUser()
166 public void setUser( EditUserCredentials user )
171 public SecureActionBundle initSecureActionBundle()
172 throws SecureActionException
174 return SecureActionBundle.OPEN;
178 * 1) attempts to authentication based on the passed in data source
179 * 2) if successful sets cookies and returns LOGIN_SUCCESS
180 * 3) if failure then check what kinda failure and return error
182 * @param authdatasource
185 private String webLogin( AuthenticationDataSource authdatasource )
187 // An attempt should log out your authentication tokens first!
188 setAuthTokens( null );
190 clearErrorsAndMessages();
192 String principal = authdatasource.getPrincipal();
196 SecuritySession securitySession = securitySystem.authenticate( authdatasource );
198 if ( securitySession.getAuthenticationResult().isAuthenticated() )
200 // Success! Create tokens.
201 setAuthTokens( securitySession );
203 setCookies( authdatasource );
205 AuditEvent event = new AuditEvent( getText( "log.login.success" ) );
206 event.setAffectedUser( principal );
209 User u = securitySession.getUser();
210 u.setLastLoginDate( new Date() );
211 securitySystem.getUserManager().updateUser( u );
213 return LOGIN_SUCCESS;
217 log.debug( "Login Action failed against principal : {}",
218 securitySession.getAuthenticationResult().getPrincipal(),
219 securitySession.getAuthenticationResult().getException() );
221 AuthenticationResult result = securitySession.getAuthenticationResult();
222 if ( result.getExceptionsMap() != null && !result.getExceptionsMap().isEmpty() )
224 if ( result.getExceptionsMap().get( AuthenticationConstants.AUTHN_NO_SUCH_USER ) != null )
226 addActionError( getText( "incorrect.username.password" ) );
230 addActionError( getText( "authentication.failed" ) );
235 addActionError( getText( "authentication.failed" ) );
238 AuditEvent event = new AuditEvent( getText( "log.login.fail" ) );
239 event.setAffectedUser( principal );
245 catch ( AuthenticationException ae )
247 addActionError( getText( "authentication.exception", Arrays.asList( (Object) ae.getMessage() ) ) );
250 catch ( UserNotFoundException ue )
253 getText( "user.not.found.exception", Arrays.asList( (Object) principal, ue.getMessage() ) ) );
255 AuditEvent event = new AuditEvent( getText( "log.login.fail" ) );
256 event.setAffectedUser( principal );
260 catch ( AccountLockedException e )
262 addActionError( getText( "account.locked" ) );
264 AuditEvent event = new AuditEvent( getText( "log.login.fail.locked" ) );
265 event.setAffectedUser( principal );
267 return ACCOUNT_LOCKED;
269 catch ( MustChangePasswordException e )
271 // TODO: preferably we would not set the cookies for this "partial" login state
272 setCookies( authdatasource );
274 AuditEvent event = new AuditEvent( getText( "log.login.fail.locked" ) );
275 event.setAffectedUser( principal );
277 return PASSWORD_CHANGE;
281 private void setCookies( AuthenticationDataSource authdatasource )
283 autologinCookies.setSignonCookie( authdatasource.getPrincipal(), ServletActionContext.getResponse(),
284 ServletActionContext.getRequest() );