From a2c359a19ff4e16064fa2e57df1dc7b52022d24e Mon Sep 17 00:00:00 2001 From: Jesse McConnell Date: Tue, 12 Sep 2006 22:31:02 +0000 Subject: [PATCH] new setup wizard that forces the creation of an administrator user git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/trunk@442742 13f79535-47bb-0310-9956-ffa450edef68 --- .../web/action/admin/NewUserAction.java | 77 +++++++++++++++ .../interceptor/ConfigurationInterceptor.java | 13 +++ .../archiva/web/util/DefaultRoleManager.java | 22 +++++ .../maven/archiva/web/util/RoleManager.java | 3 + .../plexus/plexus-security.properties | 4 + archiva-webapp/src/main/resources/xwork.xml | 12 +++ .../WEB-INF/jsp/admin/registerAdmin.jsp | 99 +++++++++++++++++++ 7 files changed, 230 insertions(+) create mode 100644 archiva-webapp/src/main/webapp/WEB-INF/jsp/admin/registerAdmin.jsp diff --git a/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/admin/NewUserAction.java b/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/admin/NewUserAction.java index a3d078734..8f0e70614 100644 --- a/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/admin/NewUserAction.java +++ b/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/admin/NewUserAction.java @@ -22,6 +22,7 @@ import org.codehaus.plexus.security.user.User; import org.codehaus.plexus.security.user.UserManager; import org.codehaus.plexus.security.user.policy.PasswordRuleViolationException; import org.codehaus.plexus.security.user.policy.PasswordRuleViolations; +import org.codehaus.plexus.security.rbac.RBACManager; import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.xwork.action.PlexusActionSupport; @@ -51,6 +52,11 @@ public class NewUserAction */ private RoleManager roleManager; + /** + * @plexus.requirement + */ + private RBACManager rbacManager; + private String username; private String password; @@ -118,6 +124,7 @@ public class NewUserAction addActionError( (String) it.next() ); } } + roleManager.addUser( user.getPrincipal().toString() ); addActionMessage( "user " + username + " was successfully registered!"); @@ -131,6 +138,76 @@ public class NewUserAction return SUCCESS; } + public String createAdminUser() + { + if ( username == null ) + { + return INPUT; + } + + // TODO: use commons-validator for these fields. + + if ( StringUtils.isEmpty( username ) ) + { + addActionError( "User Name is required." ); + } + + if ( StringUtils.isEmpty( fullName ) ) + { + addActionError( "Full Name is required." ); + } + + if ( StringUtils.isEmpty( email ) ) + { + addActionError( "Email Address is required." ); + } + + // TODO: Validate Email Address (use commons-validator) + + if ( StringUtils.equals( password, passwordConfirm ) ) + { + addActionError( "Passwords do not match." ); + } + + UserManager um = securitySystem.getUserManager(); + + if ( um.userExists( username ) ) + { + addActionError( "User already exists!" ); + } + else + { + User user = um.createUser( username, fullName, email ); + + user.setPassword( password ); + + try + { + um.addUser( user ); + } + catch ( PasswordRuleViolationException e ) + { + PasswordRuleViolations violations = e.getViolations(); + List violationList = violations.getLocalizedViolations(); + Iterator it = violationList.iterator(); + while ( it.hasNext() ) + { + addActionError( (String) it.next() ); + } + } + + roleManager.addAdminUser( user.getPrincipal().toString() ); + + } + + if ( hasActionErrors() ) + { + return INPUT; + } + + return SUCCESS; + } + public String getUsername() { return username; diff --git a/archiva-webapp/src/main/java/org/apache/maven/archiva/web/interceptor/ConfigurationInterceptor.java b/archiva-webapp/src/main/java/org/apache/maven/archiva/web/interceptor/ConfigurationInterceptor.java index 5695f01aa..946b5f01a 100644 --- a/archiva-webapp/src/main/java/org/apache/maven/archiva/web/interceptor/ConfigurationInterceptor.java +++ b/archiva-webapp/src/main/java/org/apache/maven/archiva/web/interceptor/ConfigurationInterceptor.java @@ -22,6 +22,7 @@ import org.apache.maven.archiva.configuration.Configuration; import org.apache.maven.archiva.configuration.ConfigurationStore; import org.apache.maven.archiva.web.util.RoleManager; import org.codehaus.plexus.logging.AbstractLogEnabled; +import org.codehaus.plexus.security.rbac.RBACManager; /** * An interceptor that makes the application configuration available @@ -43,6 +44,11 @@ public class ConfigurationInterceptor */ private RoleManager roleManager; + /** + * @plexus.requirement + */ + private RBACManager rbacManager; + /** * * @param actionInvocation @@ -52,6 +58,13 @@ public class ConfigurationInterceptor public String intercept( ActionInvocation actionInvocation ) throws Exception { + + if ( rbacManager.getAllUserAssignments().size() == 0 ) + { + getLogger().info( "no accounts setup, create user account, forwarding to registration" ); + return "admin-account-needed"; + } + Configuration configuration = configurationStore.getConfigurationFromStore(); if ( !configuration.isValid() ) diff --git a/archiva-webapp/src/main/java/org/apache/maven/archiva/web/util/DefaultRoleManager.java b/archiva-webapp/src/main/java/org/apache/maven/archiva/web/util/DefaultRoleManager.java index ce00d0906..42a46759e 100644 --- a/archiva-webapp/src/main/java/org/apache/maven/archiva/web/util/DefaultRoleManager.java +++ b/archiva-webapp/src/main/java/org/apache/maven/archiva/web/util/DefaultRoleManager.java @@ -253,6 +253,28 @@ public class DefaultRoleManager } + /** + * helper method for just creating an admin user assignment + * + * @param principal + * @throws RbacStoreException + * @throws RbacObjectNotFoundException + */ + public void addAdminUser( String principal ) + throws RbacStoreException + { + try + { + UserAssignment assignment = manager.createUserAssignment( principal ); + assignment.addRole( manager.getRole( "System Administrator" ) ); + manager.saveUserAssignment( assignment ); + } + catch ( RbacObjectNotFoundException ne ) + { + throw new RbacStoreException( "unable to find administrator role, this of course is bad", ne ); + } + } + public void addRepository( String repositoryName ) throws RbacStoreException { diff --git a/archiva-webapp/src/main/java/org/apache/maven/archiva/web/util/RoleManager.java b/archiva-webapp/src/main/java/org/apache/maven/archiva/web/util/RoleManager.java index 220ee8ea3..b9510bb59 100644 --- a/archiva-webapp/src/main/java/org/apache/maven/archiva/web/util/RoleManager.java +++ b/archiva-webapp/src/main/java/org/apache/maven/archiva/web/util/RoleManager.java @@ -35,5 +35,8 @@ public interface RoleManager public void addUser( String principal ) throws RbacStoreException; + public void addAdminUser( String principal ) + throws RbacStoreException; + public boolean isInitialized(); } diff --git a/archiva-webapp/src/main/resources/META-INF/plexus/plexus-security.properties b/archiva-webapp/src/main/resources/META-INF/plexus/plexus-security.properties index c47486b91..d53c40833 100644 --- a/archiva-webapp/src/main/resources/META-INF/plexus/plexus-security.properties +++ b/archiva-webapp/src/main/resources/META-INF/plexus/plexus-security.properties @@ -1,3 +1,7 @@ +# +# properties that might be used in plexus-security initialization +# + # # operations # diff --git a/archiva-webapp/src/main/resources/xwork.xml b/archiva-webapp/src/main/resources/xwork.xml index 35c969161..75cc49a8d 100644 --- a/archiva-webapp/src/main/resources/xwork.xml +++ b/archiva-webapp/src/main/resources/xwork.xml @@ -46,6 +46,11 @@ /admin configure + + /admin + registerAdminAccount + input + /admin addRepository @@ -145,6 +150,13 @@ + + /WEB-INF/jsp/admin/registerAdmin.jsp + /WEB-INF/jsp/admin/registerAdmin.jsp + index + + + /WEB-INF/jsp/admin/index.jsp diff --git a/archiva-webapp/src/main/webapp/WEB-INF/jsp/admin/registerAdmin.jsp b/archiva-webapp/src/main/webapp/WEB-INF/jsp/admin/registerAdmin.jsp new file mode 100644 index 000000000..df6ec69eb --- /dev/null +++ b/archiva-webapp/src/main/webapp/WEB-INF/jsp/admin/registerAdmin.jsp @@ -0,0 +1,99 @@ +<%-- + ~ Copyright 2005-2006 The Apache Software Foundation. + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --%> + +<%@ taglib prefix="ww" uri="/webwork" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + + + + Adminsitrator Registration Page + + + + + +
+ +
+ + +
+
+
+ + + +