1d6499e0a1b17e68baed778325b92321004738db
[utils] / security / src / main / java / org / wamblee / security / authorization / DefaultAuthorizationService.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 java.util.ArrayList;
20 import java.util.List;
21
22 import org.wamblee.persistence.AbstractPersistent;
23 import org.wamblee.usermgt.User;
24 import org.wamblee.usermgt.UserAccessor;
25
26 /**
27  * Default implementation of an authorization service. 
28  * To determine whether access to a resource is allowed, the service consults a number
29  * of authorization rules in a fixed order. The first rule that gives a result GRANTED or 
30  * DENIED determines the result of the evaluation. Rules that return any other result are 
31  * ignoed. If none of the rules match, than access is denied. 
32  *
33  * @author Erik Brakkee
34  */
35 public class DefaultAuthorizationService extends AbstractPersistent implements AuthorizationService {
36     
37     /**
38      * List of ordered authorization rules. 
39      */
40     private List<AuthorizationRule> rules; 
41     
42     /**
43      * User accessor used to obtain the current user. 
44      */
45     private UserAccessor userAccessor; 
46     
47     /**
48      * Name for this instance of the authorization service. 
49      */
50     private String name;
51     
52     /**
53      * Constructs the service. 
54      * @param aAccessor User accessor. 
55      * @param aName Name of this instance of the service. 
56      */
57     public DefaultAuthorizationService(UserAccessor aAccessor, String aName) {
58         rules = new ArrayList<AuthorizationRule>();
59         userAccessor = aAccessor;
60         name = aName; 
61     }
62     
63     /**
64      * Constructs the authorization service.  
65      */
66     public DefaultAuthorizationService() {
67         rules = new ArrayList<AuthorizationRule>();
68         userAccessor = null;
69         name = null; 
70     }
71     
72     /**
73      * Sets the user accessor. 
74      * @param aUserAccessor User accessor. 
75      */
76     public void setUserAccessor(UserAccessor aUserAccessor) { 
77         userAccessor = aUserAccessor; 
78     }
79
80     /* (non-Javadoc)
81      * @see org.wamblee.security.authorization.AuthorizationService#isAllowed(java.lang.Object, org.wamblee.security.authorization.Operation)
82      */
83     public boolean isAllowed(Object aResource, Operation aOperation) {
84         User user = userAccessor.getCurrentUser(); 
85         for (AuthorizationRule rule: rules) { 
86             switch ( rule.isAllowed(aResource, aOperation, user)) {
87             case DENIED: { return false; } 
88             case GRANTED: { return true; }
89             }
90         }
91         return false; 
92     }
93     
94     /* (non-Javadoc)
95      * @see org.wamblee.security.authorization.AuthorizationService#check(T, org.wamblee.security.authorization.Operation)
96      */
97     public <T> T check(T aResource, Operation aOperation) {
98         if ( !isAllowed(aResource, aOperation)) { 
99             throw new AuthorizationException(aResource, aOperation);
100         }
101         return aResource;
102     }
103     
104     protected String getName() { 
105         return name; 
106     }
107     
108     public void setName(String aName) { 
109         name = aName; 
110     }
111     
112     /* (non-Javadoc)
113      * @see org.wamblee.security.authorization.AuthorizationService#getRules()
114      */
115     public AuthorizationRule[] getRules() {
116         return rules.toArray(new AuthorizationRule[0]); 
117     }
118     
119     /* (non-Javadoc)
120      * @see org.wamblee.security.authorization.AuthorizationService#appendRule(org.wamblee.security.authorization.AuthorizationRule)
121      */
122     public void appendRule(AuthorizationRule aRule) {
123         rules.add(aRule); 
124     }
125
126     /* (non-Javadoc)
127      * @see org.wamblee.security.authorization.AuthorizationService#insertRuleAfter(int, org.wamblee.security.authorization.AuthorizationRule)
128      */
129     public void insertRuleAfter(int aIndex, AuthorizationRule aRule) {
130        rules.add(aIndex, aRule);
131     }
132     
133     /* (non-Javadoc)
134      * @see org.wamblee.security.authorization.AuthorizationService#removeRule(int)
135      */
136     public void removeRule(int aIndex) {
137        rules.remove(aIndex);
138     }
139     
140     /**
141      * For OR mapping. 
142      * @return The rules. 
143      */
144     protected List<AuthorizationRule> getMappedRules() {
145         return rules; 
146     }
147     
148     /**
149      * For OR mapping.
150      * @param aRules The rules. 
151      */
152     protected void setMappedRules(List<AuthorizationRule> aRules) { 
153         rules = aRules; 
154     }
155 }