1 package org.apache.archiva.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 org.apache.archiva.redback.rbac.Permission;
23 import org.apache.archiva.redback.rbac.RbacManagerException;
24 import org.apache.archiva.redback.rbac.Resource;
25 import org.apache.archiva.redback.rbac.Role;
26 import org.apache.archiva.redback.rbac.RBACManager;
27 import org.apache.archiva.redback.struts2.action.AuditEvent;
28 import org.apache.archiva.redback.struts2.action.AbstractSecurityAction;
29 import org.codehaus.plexus.util.StringUtils;
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.model.SimplePermission;
33 import org.apache.archiva.redback.integration.role.RoleConstants;
34 import org.springframework.context.annotation.Scope;
35 import org.springframework.stereotype.Controller;
37 import javax.inject.Inject;
38 import javax.inject.Named;
39 import java.util.ArrayList;
40 import java.util.Arrays;
41 import java.util.List;
46 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
49 @Controller( "redback-role-create" )
51 public class RoleCreateAction
52 extends AbstractSecurityAction
54 // ------------------------------------------------------------------
55 // Component Requirements
56 // ------------------------------------------------------------------
62 @Named( value = "rBACManager#cached" )
63 private RBACManager manager;
65 // ------------------------------------------------------------------
67 // ------------------------------------------------------------------
69 private String principal;
71 private String roleName;
73 private String description;
75 private List<SimplePermission> permissions;
77 private List<String> childRoles;
79 private SimplePermission addpermission;
81 private String submitMode;
83 protected static final String VALID_ROLENAME_CHARS = "[a-zA-Z_0-9\\-\\s.,]*";
85 // ------------------------------------------------------------------
86 // Action Entry Points - (aka Names)
87 // ------------------------------------------------------------------
91 if ( permissions == null )
93 permissions = new ArrayList<SimplePermission>();
96 if ( childRoles == null )
98 childRoles = new ArrayList<String>();
101 if ( addpermission == null )
103 addpermission = new SimplePermission();
109 public String addpermission()
111 if ( addpermission == null )
113 addActionError( getText( "cannot.add.null.permission" ) );
117 if ( permissions == null )
119 permissions = new ArrayList<SimplePermission>();
122 permissions.add( addpermission );
124 addpermission = new SimplePermission();
129 public String submit()
131 if ( StringUtils.equals( getSubmitMode(), "addPermission" ) )
133 return addpermission();
136 if ( StringUtils.isEmpty( roleName ) )
138 addActionError( getText( "cannot.add.empty.role" ) );
141 if ( !roleName.matches( VALID_ROLENAME_CHARS ) )
143 addActionError( getText( "roleName.invalid.characters" ) );
150 if ( manager.roleExists( roleName ) )
152 _role = manager.getRole( roleName );
156 _role = manager.createRole( roleName );
159 _role.setDescription( description );
160 _role.setChildRoleNames( childRoles );
162 List<Permission> _permissionList = new ArrayList<Permission>();
163 for ( SimplePermission perm : permissions )
166 manager.createPermission( perm.getName(), perm.getOperationName(), perm.getResourceIdentifier() ) );
169 _role.setPermissions( _permissionList );
171 manager.saveRole( _role );
173 addActionMessage( getText( "save.role.success", Arrays.asList( (Object) roleName ) ) );
174 String currentUser = getCurrentUser();
175 AuditEvent event = new AuditEvent( getText( "log.role.create" ) );
176 event.setRole( roleName );
177 event.setCurrentUser( currentUser );
180 catch ( RbacManagerException e )
182 addActionError( getText( "cannot.get.role", Arrays.asList( (Object) roleName, e.getMessage() ) ) );
189 // ------------------------------------------------------------------
190 // Parameter Accessor Methods
191 // ------------------------------------------------------------------
193 public String getPrincipal()
198 public void setPrincipal( String principal )
200 this.principal = principal;
203 public SimplePermission getAddpermission()
205 return addpermission;
208 public void setAddpermission( SimplePermission addpermission )
210 this.addpermission = addpermission;
213 public String getSubmitMode()
218 public void setSubmitMode( String submitMode )
220 this.submitMode = submitMode;
223 public SecureActionBundle initSecureActionBundle()
224 throws SecureActionException
226 SecureActionBundle bundle = new SecureActionBundle();
227 bundle.setRequiresAuthentication( true );
228 bundle.addRequiredAuthorization( RoleConstants.USER_MANAGEMENT_RBAC_ADMIN_OPERATION, Resource.GLOBAL );