1 package org.apache.archiva.redback.rbac;
4 * Copyright 2001-2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.codehaus.plexus.util.StringUtils;
22 * RBACObjectAssertions
24 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
27 public class RBACObjectAssertions
29 public static void assertValid( Role role )
30 throws RbacObjectInvalidException
32 assertValid( null, role );
35 public static void assertValid( String scope, Role role )
36 throws RbacObjectInvalidException
40 throw new RbacObjectInvalidException( scope, "Null Role object is invalid." );
43 if ( StringUtils.isEmpty( role.getName() ) )
45 throw new RbacObjectInvalidException( scope, "Role.name must not be empty." );
48 if ( role.getPermissions() != null )
51 for ( Permission perm : role.getPermissions() )
53 assertValid( "Role.permissions[" + i + "]", perm );
59 public static void assertValid( Permission permission )
60 throws RbacObjectInvalidException
62 assertValid( null, permission );
65 public static void assertValid( String scope, Permission permission )
66 throws RbacObjectInvalidException
68 if ( permission == null )
70 throw new RbacObjectInvalidException( scope, "Null Permission object is invalid." );
73 if ( StringUtils.isEmpty( permission.getName() ) )
75 throw new RbacObjectInvalidException( scope, "Permission.name must not be empty." );
78 assertValid( "Permission.operation", permission.getOperation() );
79 assertValid( "Permission.resource", permission.getResource() );
83 public static void assertValid( Operation operation )
84 throws RbacObjectInvalidException
86 assertValid( null, operation );
89 public static void assertValid( String scope, Operation operation )
90 throws RbacObjectInvalidException
92 if ( operation == null )
94 throw new RbacObjectInvalidException( scope, "Null Operation object is invalid." );
97 if ( StringUtils.isEmpty( operation.getName() ) )
99 throw new RbacObjectInvalidException( scope, "Operation.name must not be empty." );
103 public static void assertValid( Resource resource )
104 throws RbacObjectInvalidException
106 assertValid( null, resource );
109 public static void assertValid( String scope, Resource resource )
110 throws RbacObjectInvalidException
112 if ( resource == null )
114 throw new RbacObjectInvalidException( scope, "Null Resource object is invalid." );
117 if ( StringUtils.isEmpty( resource.getIdentifier() ) )
119 throw new RbacObjectInvalidException( scope, "Resource.identifier must not be empty." );
123 public static void assertValid( UserAssignment assignment )
124 throws RbacObjectInvalidException
126 assertValid( null, assignment );
129 public static void assertValid( String scope, UserAssignment assignment )
130 throws RbacObjectInvalidException
132 if ( assignment == null )
134 throw new RbacObjectInvalidException( scope, "Null UserAssigment object is invalid." );
137 if ( StringUtils.isEmpty( assignment.getPrincipal() ) )
139 throw new RbacObjectInvalidException( scope, "UserAssigment.principal cannot be empty." );
142 if ( assignment.getRoleNames() == null )
144 throw new RbacObjectInvalidException( scope, "UserAssignment.roles cannot be null." );
147 /* I don't believe this assertion is valid, a person should be able to be stripped of all roles.
149 if ( assignment.getRoleNames().isEmpty() )
151 throw new RbacObjectInvalidException( scope, "UserAssignment.roles cannot be empty." );
155 for ( String name : assignment.getRoleNames() )
157 if ( StringUtils.isEmpty( name ) )
159 throw new RbacObjectInvalidException( scope, "UserAssignment.rolename[" + i + "] cannot be empty." );