Moved over some of the security stuff from Photos.
[utils] / security / src / test / java / org / wamblee / security / authorization / TestAuthorizationRule.java
1 /*
2  * Copyright 2005 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */ 
16
17 package org.wamblee.security.authorization;
18
19 import static org.wamblee.security.authorization.AuthorizationResult.DENIED;
20 import static org.wamblee.security.authorization.AuthorizationResult.GRANTED;
21
22 import org.wamblee.usermgt.User;
23
24 /**
25  * Test authorization rule that also counts the number of times the rule matches. 
26  */
27 public class TestAuthorizationRule extends UrlAuthorizationRule {
28     
29     /**
30      * Counts the number of matches. 
31      */
32     private int _matches = 0; 
33
34     public TestAuthorizationRule( AuthorizationResult aResult, String aGroup,
35             String aPath, Class<? extends Operation> aOperation) {
36        super(aResult, new GroupUserCondition(aGroup), 
37                new StartsWithPathCondition(aPath), TestResource.class, new IsaOperationCondition(aOperation));
38     }
39     
40     protected TestAuthorizationRule() { 
41         super();
42     }
43     
44     /* (non-Javadoc)
45      * @see org.wamblee.security.authorization.UrlAuthorizationRule#getPath(java.lang.Object)
46      */
47     @Override
48     protected String getResourcePath(Object aResource) {
49         return ((TestResource)aResource).getPath();
50     }
51     
52     /* (non-Javadoc)
53      * @see org.wamblee.security.authorization.UrlAuthorizationRule#isAllowed(java.lang.Object, org.wamblee.security.authorization.Operation, org.wamblee.usermgt.UserAccessor)
54      */
55     @Override
56     public AuthorizationResult isAllowed(Object aResource, Operation anOperation, User aUser) {
57         
58         AuthorizationResult result = super.isAllowed(aResource, anOperation, aUser);
59         if ( result.equals(GRANTED) || result.equals(DENIED)) { 
60             _matches++; 
61         }
62         return result; 
63     }
64     
65     public int getMatchCount() { 
66         return _matches; 
67     }
68     
69     public void reset() { 
70         _matches = 0; 
71     }
72    
73 }