1 package org.codehaus.plexus.redback.struts2.action.admin;
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 java.util.Arrays;
24 import org.apache.archiva.redback.policy.UserSecurityPolicy;
25 import org.codehaus.plexus.redback.rbac.Resource;
26 import org.codehaus.plexus.redback.struts2.action.AbstractUserCredentialsAction;
27 import org.codehaus.plexus.redback.struts2.action.AuditEvent;
28 import org.apache.archiva.redback.users.User;
29 import org.apache.archiva.redback.users.UserManager;
30 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
31 import org.codehaus.redback.integration.interceptor.SecureActionException;
32 import org.codehaus.redback.integration.model.CreateUserCredentials;
33 import org.codehaus.redback.integration.role.RoleConstants;
34 import org.springframework.context.annotation.Scope;
35 import org.springframework.stereotype.Controller;
40 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
43 @Controller("redback-admin-user-create")
45 public class UserCreateAction
46 extends AbstractUserCredentialsAction
48 // ------------------------------------------------------------------
50 // ------------------------------------------------------------------
52 private CreateUserCredentials user;
54 // ------------------------------------------------------------------
55 // Action Entry Points - (aka Names)
56 // ------------------------------------------------------------------
62 user = new CreateUserCredentials();
68 public String submit()
72 user = new CreateUserCredentials();
73 addActionError( getText( "invalid.user.credentials" ) );
79 validateCredentialsLoose();
81 // NOTE: Do not perform Password Rules Validation Here.
83 UserManager manager = super.securitySystem.getUserManager();
85 if ( manager.userExists( user.getUsername() ) )
87 // Means that the role name doesn't exist.
88 // We need to fail fast and return to the previous page.
89 addActionError( getText( "user.already.exists", Arrays.asList( ( Object ) user.getUsername() ) ) );
92 if ( hasActionErrors() || hasFieldErrors() )
97 User u = manager.createUser( user.getUsername(), user.getFullName(), user.getEmail() );
98 u.setPassword( user.getPassword() );
100 // force the user to change their password when they log in next
101 u.setPasswordChangeRequired( true );
103 // Disable Password Rules for this creation.
104 UserSecurityPolicy securityPolicy = securitySystem.getPolicy();
108 securityPolicy.setEnabled( false );
109 u.setValidated( true );
110 manager.addUser( u );
111 String currentUser = getCurrentUser();
112 AuditEvent event = new AuditEvent( getText( "log.account.create" ) );
113 event.setAffectedUser( u.getUsername() );
114 event.setCurrentUser( currentUser );
119 securityPolicy.setEnabled( true );
125 // ------------------------------------------------------------------
126 // Parameter Accessor Methods
127 // ------------------------------------------------------------------
129 public CreateUserCredentials getUser()
134 public void setUser( CreateUserCredentials user )
139 public SecureActionBundle initSecureActionBundle()
140 throws SecureActionException
142 SecureActionBundle bundle = new SecureActionBundle();
143 bundle.setRequiresAuthentication( true );
144 bundle.addRequiredAuthorization( RoleConstants.USER_MANAGEMENT_USER_CREATE_OPERATION, Resource.GLOBAL );