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.users.User;
23 import org.apache.archiva.redback.users.UserNotFoundException;
24 import org.apache.archiva.redback.policy.PasswordEncoder;
25 import org.apache.archiva.redback.policy.PasswordRuleViolationException;
26 import org.apache.archiva.redback.system.DefaultSecuritySession;
27 import org.apache.archiva.redback.system.SecuritySession;
28 import org.apache.archiva.redback.system.SecuritySystemConstants;
29 import org.apache.archiva.redback.users.UserManager;
30 import org.codehaus.plexus.util.StringUtils;
31 import org.apache.archiva.redback.integration.interceptor.SecureActionBundle;
32 import org.apache.archiva.redback.integration.interceptor.SecureActionException;
33 import org.apache.archiva.redback.integration.model.EditUserCredentials;
34 import org.springframework.context.annotation.Scope;
35 import org.springframework.stereotype.Controller;
37 import java.util.Arrays;
42 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
45 @Controller( "redback-account" )
47 public class AccountAction
48 extends AbstractUserCredentialsAction
49 implements CancellableAction
51 private static final String ACCOUNT_SUCCESS = "security-account-success";
53 // ------------------------------------------------------------------
55 // ------------------------------------------------------------------
57 private EditUserCredentials user;
59 private String oldPassword;
61 // ------------------------------------------------------------------
62 // Action Entry Points - (aka Names)
63 // ------------------------------------------------------------------
67 SecuritySession session = getSecuritySession();
69 if ( !session.isAuthenticated() )
71 addActionError( getText( "cannot.show.account.login.required" ) );
72 return REQUIRES_AUTHENTICATION;
75 String username = session.getUser().getUsername();
77 if ( username == null )
79 addActionError( getText( "cannot.edit.user.null.username" ) );
83 if ( StringUtils.isEmpty( username ) )
85 addActionError( getText( "cannot.edit.user.empty.username" ) );
89 UserManager manager = super.securitySystem.getUserManager();
91 if ( !manager.userExists( username ) )
93 // Means that the role name doesn't exist.
94 // We need to fail fast and return to the previous page.
95 addActionError( getText( "user.does.not.exist", Arrays.asList( (Object) username ) ) );
103 User u = manager.findUser( username );
106 addActionError( getText( "cannot.operate.on.null.user" ) );
110 user = new EditUserCredentials( u );
112 catch ( UserNotFoundException e )
114 addActionError( getText( "cannot.get.user", Arrays.asList( (Object) username, e.getMessage() ) ) );
121 public String submit()
123 SecuritySession session = getSecuritySession();
125 if ( !session.isAuthenticated() )
127 addActionError( getText( "cannot.show.account.login.required" ) );
128 return REQUIRES_AUTHENTICATION;
131 String username = session.getUser().getUsername();
133 if ( username == null )
135 addActionError( getText( "cannot.edit.user.null.username" ) );
139 if ( StringUtils.isEmpty( username ) )
141 addActionError( getText( "cannot.edit.user.empty.username" ) );
147 addActionError( getText( "cannot.edit.user.null.credentials" ) );
151 if ( !user.getPassword().equals( user.getConfirmPassword() ) )
153 addFieldError( "user.confirmPassword", getText( "password.confimation.failed" ) );
157 UserManager manager = super.securitySystem.getUserManager();
159 if ( !manager.userExists( username ) )
161 // Means that the role name doesn't exist.
162 // We need to fail fast and return to the previous page.
163 addActionError( getText( "user.does.not.exist", Arrays.asList( (Object) username ) ) );
171 User u = manager.findUser( username );
174 addActionError( getText( "cannot.operate.on.null.user" ) );
178 if ( StringUtils.isNotEmpty( user.getPassword() ) )
180 PasswordEncoder encoder = securitySystem.getPolicy().getPasswordEncoder();
182 if ( !encoder.isPasswordValid( u.getEncodedPassword(), oldPassword ) )
184 addFieldError( "oldPassword", getText( "password.provided.does.not.match.existing" ) );
188 u.setPassword( user.getPassword() );
191 u.setFullName( user.getFullName() );
192 u.setEmail( user.getEmail() );
193 u.setPassword( user.getPassword() );
195 manager.updateUser( u );
197 //check if current user then update the session
198 if ( getSecuritySession().getUser().getUsername().equals( u.getUsername() ) )
200 SecuritySession securitySession =
201 new DefaultSecuritySession( getSecuritySession().getAuthenticationResult(), u );
203 this.session.put( SecuritySystemConstants.SECURITY_SESSION_KEY, securitySession );
205 setSession( this.session );
208 catch ( UserNotFoundException e )
210 addActionError( getText( "cannot.get.user", Arrays.asList( (Object) username, e.getMessage() ) ) );
213 catch ( PasswordRuleViolationException e )
215 processPasswordRuleViolations( e );
219 return ACCOUNT_SUCCESS;
222 public String cancel()
227 // ------------------------------------------------------------------
228 // Parameter Accessor Methods
229 // ------------------------------------------------------------------
231 public EditUserCredentials getUser()
236 public void setUser( EditUserCredentials user )
241 public SecureActionBundle initSecureActionBundle()
242 throws SecureActionException
244 SecureActionBundle bundle = new SecureActionBundle();
245 bundle.setRequiresAuthentication( true );
249 public void setOldPassword( String oldPassword )
251 this.oldPassword = oldPassword;
254 public boolean isSelf()