This project has retired. For details please refer to its Attic page.
RBACObjectAssertions xref
View Javadoc

1   package org.apache.archiva.redback.rbac;
2   
3   /*
4    * Copyright 2001-2006 The Apache Software Foundation.
5    *
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
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
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.
17   */
18  
19  
20  import org.apache.commons.lang.StringUtils;
21  
22  /**
23   * RBACObjectAssertions 
24   *
25   * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
26   *
27   */
28  public class RBACObjectAssertions
29  {
30      public static void assertValid( Role role )
31          throws RbacObjectInvalidException
32      {
33          assertValid( null, role );
34      }
35  
36      public static void assertValid( String scope, Role role )
37          throws RbacObjectInvalidException
38      {
39          if ( role == null )
40          {
41              throw new RbacObjectInvalidException( scope, "Null Role object is invalid." );
42          }
43  
44          if ( StringUtils.isEmpty( role.getName() ) )
45          {
46              throw new RbacObjectInvalidException( scope, "Role.name must not be empty." );
47          }
48  
49          if ( role.getPermissions() != null )
50          {
51              int i = 0;
52              for ( Permission perm : role.getPermissions() )
53              {
54                  assertValid( "Role.permissions[" + i + "]", perm );
55                  i++;
56              }
57          }
58      }
59  
60      public static void assertValid( Permission permission )
61          throws RbacObjectInvalidException
62      {
63          assertValid( null, permission );
64      }
65  
66      public static void assertValid( String scope, Permission permission )
67          throws RbacObjectInvalidException
68      {
69          if ( permission == null )
70          {
71              throw new RbacObjectInvalidException( scope, "Null Permission object is invalid." );
72          }
73  
74          if ( StringUtils.isEmpty( permission.getName() ) )
75          {
76              throw new RbacObjectInvalidException( scope, "Permission.name must not be empty." );
77          }
78  
79          assertValid( "Permission.operation", permission.getOperation() );
80          assertValid( "Permission.resource", permission.getResource() );
81  
82      }
83  
84      public static void assertValid( Operation operation )
85          throws RbacObjectInvalidException
86      {
87          assertValid( null, operation );
88      }
89  
90      public static void assertValid( String scope, Operation operation )
91          throws RbacObjectInvalidException
92      {
93          if ( operation == null )
94          {
95              throw new RbacObjectInvalidException( scope, "Null Operation object is invalid." );
96          }
97  
98          if ( StringUtils.isEmpty( operation.getName() ) )
99          {
100             throw new RbacObjectInvalidException( scope, "Operation.name must not be empty." );
101         }
102     }
103 
104     public static void assertValid( Resource resource )
105         throws RbacObjectInvalidException
106     {
107         assertValid( null, resource );
108     }
109 
110     public static void assertValid( String scope, Resource resource )
111         throws RbacObjectInvalidException
112     {
113         if ( resource == null )
114         {
115             throw new RbacObjectInvalidException( scope, "Null Resource object is invalid." );
116         }
117 
118         if ( StringUtils.isEmpty( resource.getIdentifier() ) )
119         {
120             throw new RbacObjectInvalidException( scope, "Resource.identifier must not be empty." );
121         }
122     }
123 
124     public static void assertValid( UserAssignment assignment )
125         throws RbacObjectInvalidException
126     {
127         assertValid( null, assignment );
128     }
129 
130     public static void assertValid( String scope, UserAssignment assignment )
131         throws RbacObjectInvalidException
132     {
133         if ( assignment == null )
134         {
135             throw new RbacObjectInvalidException( scope, "Null UserAssigment object is invalid." );
136         }
137 
138         if ( StringUtils.isEmpty( assignment.getPrincipal() ) )
139         {
140             throw new RbacObjectInvalidException( scope, "UserAssigment.principal cannot be empty." );
141         }
142 
143         if ( assignment.getRoleNames() == null )
144         {
145             throw new RbacObjectInvalidException( scope, "UserAssignment.roles cannot be null." );
146         }
147 
148         /*  I don't believe this assertion is valid, a person should be able to be stripped of all roles.
149            -- jesse
150         if ( assignment.getRoleNames().isEmpty() )
151         {
152             throw new RbacObjectInvalidException( scope, "UserAssignment.roles cannot be empty." );
153         }
154           */
155         int i = 0;
156         for ( String name : assignment.getRoleNames() )
157         {
158             if ( StringUtils.isEmpty( name ) )
159             {
160                 throw new RbacObjectInvalidException( scope, "UserAssignment.rolename[" + i + "] cannot be empty." );
161             }
162             i++;
163         }
164     }
165 
166 }