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.Operation;
23 import org.apache.archiva.redback.rbac.Permission;
24 import org.apache.archiva.redback.rbac.RBACManager;
25 import org.apache.archiva.redback.rbac.Resource;
26 import org.apache.archiva.redback.rbac.RbacManagerException;
27 import org.apache.archiva.redback.struts2.action.RedbackActionSupport;
28 import org.codehaus.plexus.util.StringUtils;
29 import org.apache.archiva.redback.integration.interceptor.SecureActionBundle;
30 import org.apache.archiva.redback.integration.interceptor.SecureActionException;
31 import org.apache.archiva.redback.integration.role.RoleConstants;
32 import org.apache.archiva.redback.integration.util.PermissionSorter;
33 import org.springframework.context.annotation.Scope;
34 import org.springframework.stereotype.Controller;
36 import javax.inject.Inject;
37 import javax.inject.Named;
38 import java.util.Arrays;
39 import java.util.Collections;
40 import java.util.List;
45 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
48 @Controller( "redback-permissions" )
50 public class PermissionsAction
51 extends RedbackActionSupport
53 private static final String LIST = "list";
55 // ------------------------------------------------------------------
56 // Plexus Component Requirements
57 // ------------------------------------------------------------------
63 @Named( value = "rBACManager#cached" )
64 private RBACManager manager;
66 // ------------------------------------------------------------------
68 // ------------------------------------------------------------------
72 private String description;
74 private String operationName;
76 private String operationDescription;
78 private String resourceIdentifier;
80 private List<Permission> allPermissions;
82 // ------------------------------------------------------------------
83 // Action Entry Points - (aka Names)
84 // ------------------------------------------------------------------
90 allPermissions = manager.getAllPermissions();
92 if ( allPermissions == null )
94 allPermissions = Collections.emptyList();
97 Collections.sort( allPermissions, new PermissionSorter() );
99 catch ( RbacManagerException e )
101 addActionError( getText( "cannot.list.all.permissions", Arrays.asList( (Object) e.getMessage() ) ) );
102 log.error( "System error:", e );
103 allPermissions = Collections.emptyList();
109 public String input()
113 addActionError( getText( "cannot.edit.null.permission" ) );
117 if ( StringUtils.isEmpty( name ) )
119 addActionError( getText( "cannot.edit.empty.permission" ) );
123 if ( !manager.permissionExists( name ) )
125 // Means that the permission name doesn't exist.
126 // We should exit early and not attempt to look up the permission information.
132 Permission permission = manager.getPermission( name );
133 if ( permission == null )
135 addActionError( getText( "cannot.operate.null.permission" ) );
139 description = permission.getDescription();
140 Operation operation = permission.getOperation();
141 if ( operation != null )
143 operationName = operation.getName();
144 operationDescription = operation.getDescription();
147 Resource resource = permission.getResource();
148 if ( resource != null )
150 resourceIdentifier = resource.getIdentifier();
153 catch ( RbacManagerException e )
155 addActionError( getText( "cannot.get.permission", Arrays.asList( (Object) name, e.getMessage() ) ) );
162 public String submit()
166 addActionError( getText( "cannot.edit.null.permission" ) );
170 if ( StringUtils.isEmpty( name ) )
172 addActionError( getText( "cannot.edit.empty.permission" ) );
178 Permission permission;
179 if ( manager.permissionExists( name ) )
181 permission = manager.getPermission( name );
185 permission = manager.createPermission( name );
188 permission.setDescription( description );
190 Operation operation = manager.createOperation( operationName );
191 if ( StringUtils.isNotEmpty( operationDescription ) )
193 operation.setDescription( operationDescription );
195 permission.setOperation( manager.saveOperation( operation ) );
197 Resource resource = manager.createResource( resourceIdentifier );
198 permission.setResource( manager.saveResource( resource ) );
200 manager.savePermission( permission );
202 addActionMessage( getText( "save.permission.success", Arrays.asList( (Object) name ) ) );
204 catch ( RbacManagerException e )
206 addActionError( getText( "cannot.get.permission", Arrays.asList( (Object) name, e.getMessage() ) ) );
213 // ------------------------------------------------------------------
214 // Parameter Accessor Methods
215 // ------------------------------------------------------------------
217 public String getDescription()
222 public void setDescription( String description )
224 this.description = description;
227 public String getName()
232 public void setName( String name )
237 public String getOperationDescription()
239 return operationDescription;
242 public void setOperationDescription( String operationDescription )
244 this.operationDescription = operationDescription;
247 public String getOperationName()
249 return operationName;
252 public void setOperationName( String operationName )
254 this.operationName = operationName;
257 public String getResourceIdentifier()
259 return resourceIdentifier;
262 public void setResourceIdentifier( String resourceIdentifier )
264 this.resourceIdentifier = resourceIdentifier;
267 public List<Permission> getAllPermissions()
269 return allPermissions;
272 public void setAllPermissions( List<Permission> allPermissions )
274 this.allPermissions = allPermissions;
277 public SecureActionBundle initSecureActionBundle()
278 throws SecureActionException
280 SecureActionBundle bundle = new SecureActionBundle();
281 bundle.setRequiresAuthentication( true );
282 bundle.addRequiredAuthorization( RoleConstants.USER_MANAGEMENT_RBAC_ADMIN_OPERATION, Resource.GLOBAL );