d455ade2b584f6a631694d6ad37ee517afc44b7e
[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 public class DefaultAuthorizationService extends AbstractPersistent implements AuthorizationService {
34     
35     /**
36      * List of ordered authorization rules. 
37      */
38     private List<AuthorizationRule> _rules; 
39     
40     /**
41      * User accessor used to obtain the current user. 
42      */
43     private UserAccessor _userAccessor; 
44     
45     /**
46      * Name for this instance of the authorization service. 
47      */
48     private String _name;
49     
50     /**
51      * Constructs the service. 
52      * @param aAccessor User accessor. 
53      * @param aName Name of this instance of the service. 
54      */
55     public DefaultAuthorizationService(UserAccessor aAccessor, String aName) {
56         _rules = new ArrayList<AuthorizationRule>();
57         _userAccessor = aAccessor;
58         _name = aName; 
59     }
60     
61     /**
62      * Constructs the authorization service.  
63      */
64     public DefaultAuthorizationService() {
65         _rules = new ArrayList<AuthorizationRule>();
66         _userAccessor = null;
67         _name = null; 
68     }
69     
70     /**
71      * Sets the user accessor. 
72      * @param aUserAccessor User accessor. 
73      */
74     public void setUserAccessor(UserAccessor aUserAccessor) { 
75         _userAccessor = aUserAccessor; 
76     }
77
78     /* (non-Javadoc)
79      * @see org.wamblee.security.authorization.AuthorizationService#isAllowed(java.lang.Object, org.wamblee.security.authorization.Operation)
80      */
81     public boolean isAllowed(Object aResource, Operation aOperation) {
82         User user = _userAccessor.getCurrentUser(); 
83         for (AuthorizationRule rule: _rules) { 
84             switch ( rule.isAllowed(aResource, aOperation, user)) {
85             case DENIED: { return false; } 
86             case GRANTED: { return true; }
87             }
88         }
89         return false; 
90     }
91     
92     /* (non-Javadoc)
93      * @see org.wamblee.security.authorization.AuthorizationService#check(T, org.wamblee.security.authorization.Operation)
94      */
95     public <T> T check(T aResource, Operation aOperation) {
96         if ( !isAllowed(aResource, aOperation)) { 
97             throw new AuthorizationException(aResource, aOperation);
98         }
99         return aResource;
100     }
101     
102     protected String getName() { 
103         return _name; 
104     }
105     
106     public void setName(String aName) { 
107         _name = aName; 
108     }
109     
110     /* (non-Javadoc)
111      * @see org.wamblee.security.authorization.AuthorizationService#getRules()
112      */
113     public AuthorizationRule[] getRules() {
114         return _rules.toArray(new AuthorizationRule[0]); 
115     }
116     
117     /* (non-Javadoc)
118      * @see org.wamblee.security.authorization.AuthorizationService#appendRule(org.wamblee.security.authorization.AuthorizationRule)
119      */
120     public void appendRule(AuthorizationRule aRule) {
121         _rules.add(aRule); 
122     }
123
124     /* (non-Javadoc)
125      * @see org.wamblee.security.authorization.AuthorizationService#insertRuleAfter(int, org.wamblee.security.authorization.AuthorizationRule)
126      */
127     public void insertRuleAfter(int aIndex, AuthorizationRule aRule) {
128        _rules.add(aIndex, aRule);
129     }
130     
131     /* (non-Javadoc)
132      * @see org.wamblee.security.authorization.AuthorizationService#removeRule(int)
133      */
134     public void removeRule(int aIndex) {
135        _rules.remove(aIndex);
136     }
137     
138     /**
139      * For OR mapping. 
140      * @return The rules. 
141      */
142     protected List<AuthorizationRule> getMappedRules() {
143         return _rules; 
144     }
145     
146     /**
147      * For OR mapping.
148      * @param aRules The rules. 
149      */
150     protected void setMappedRules(List<AuthorizationRule> aRules) { 
151         _rules = aRules; 
152     }
153 }