e52ced7ec1f370529eac254daed9768860dbf061
[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 package org.wamblee.security.authorization;
17
18 import org.wamblee.persistence.AbstractPersistent;
19
20 import org.wamblee.usermgt.User;
21 import org.wamblee.usermgt.UserAccessor;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26
27 /**
28  * Default implementation of an authorization service.  To determine
29  * whether access to a resource is allowed, the service consults a number of
30  * authorization rules in a fixed order. The first rule that gives a result
31  * GRANTED or  DENIED determines the result of the evaluation. Rules that
32  * return any other result are  ignoed. If none of the rules match, than
33  * access is denied.
34  *
35  * @author Erik Brakkee
36  */
37 public class DefaultAuthorizationService extends AbstractPersistent
38     implements AuthorizationService {
39     /**
40      * List of ordered authorization rules.
41      */
42     private List<AuthorizationRule> rules;
43
44     /**
45      * User accessor used to obtain the current user.
46      */
47     private UserAccessor userAccessor;
48
49     /**
50      * Name for this instance of the authorization service.
51      */
52     private String name;
53
54 /**
55      * Constructs the service. 
56      * @param aAccessor User accessor. 
57      * @param aName Name of this instance of the service. 
58      */
59     public DefaultAuthorizationService(UserAccessor aAccessor, String aName) {
60         rules            = new ArrayList<AuthorizationRule>();
61         userAccessor     = aAccessor;
62         name             = aName;
63     }
64
65 /**
66      * Constructs the authorization service.  
67      */
68     public DefaultAuthorizationService() {
69         rules            = new ArrayList<AuthorizationRule>();
70         userAccessor     = null;
71         name             = null;
72     }
73
74     /**
75      * Sets the user accessor.
76      *
77      * @param aUserAccessor User accessor.
78      */
79     public void setUserAccessor(UserAccessor aUserAccessor) {
80         userAccessor = aUserAccessor;
81     }
82
83     /* (non-Javadoc)
84      * @see org.wamblee.security.authorization.AuthorizationService#isAllowed(java.lang.Object, org.wamblee.security.authorization.Operation)
85      */
86     /**
87      * DOCUMENT ME!
88      *
89      * @param aResource DOCUMENT ME!
90      * @param aOperation DOCUMENT ME!
91      *
92      * @return DOCUMENT ME!
93      */
94     public boolean isAllowed(Object aResource, Operation aOperation) {
95         User user = userAccessor.getCurrentUser();
96
97         for (AuthorizationRule rule : rules) {
98             switch (rule.isAllowed(aResource, aOperation, user)) {
99                 case DENIED:
100                     return false;
101
102                 case GRANTED:
103                     return true;
104             }
105         }
106
107         return false;
108     }
109
110     /* (non-Javadoc)
111      * @see org.wamblee.security.authorization.AuthorizationService#check(T, org.wamblee.security.authorization.Operation)
112      */
113     /**
114      * DOCUMENT ME!
115      *
116      * @param <T> DOCUMENT ME!
117      * @param aResource DOCUMENT ME!
118      * @param aOperation DOCUMENT ME!
119      *
120      * @return DOCUMENT ME!
121      *
122      * @throws AuthorizationException DOCUMENT ME!
123      */
124     public <T> T check(T aResource, Operation aOperation) {
125         if (!isAllowed(aResource, aOperation)) {
126             throw new AuthorizationException(aResource, aOperation);
127         }
128
129         return aResource;
130     }
131
132     /**
133      * DOCUMENT ME!
134      *
135      * @return DOCUMENT ME!
136      */
137     protected String getName() {
138         return name;
139     }
140
141     /**
142      * DOCUMENT ME!
143      *
144      * @param aName DOCUMENT ME!
145      */
146     public void setName(String aName) {
147         name = aName;
148     }
149
150     /* (non-Javadoc)
151      * @see org.wamblee.security.authorization.AuthorizationService#getRules()
152      */
153     /**
154      * DOCUMENT ME!
155      *
156      * @return DOCUMENT ME!
157      */
158     public AuthorizationRule[] getRules() {
159         return rules.toArray(new AuthorizationRule[0]);
160     }
161
162     /* (non-Javadoc)
163      * @see org.wamblee.security.authorization.AuthorizationService#appendRule(org.wamblee.security.authorization.AuthorizationRule)
164      */
165     /**
166      * DOCUMENT ME!
167      *
168      * @param aRule DOCUMENT ME!
169      */
170     public void appendRule(AuthorizationRule aRule) {
171         rules.add(aRule);
172     }
173
174     /* (non-Javadoc)
175      * @see org.wamblee.security.authorization.AuthorizationService#insertRuleAfter(int, org.wamblee.security.authorization.AuthorizationRule)
176      */
177     /**
178      * DOCUMENT ME!
179      *
180      * @param aIndex DOCUMENT ME!
181      * @param aRule DOCUMENT ME!
182      */
183     public void insertRuleAfter(int aIndex, AuthorizationRule aRule) {
184         rules.add(aIndex, aRule);
185     }
186
187     /* (non-Javadoc)
188      * @see org.wamblee.security.authorization.AuthorizationService#removeRule(int)
189      */
190     /**
191      * DOCUMENT ME!
192      *
193      * @param aIndex DOCUMENT ME!
194      */
195     public void removeRule(int aIndex) {
196         rules.remove(aIndex);
197     }
198
199     /**
200      * For OR mapping.
201      *
202      * @return The rules.
203      */
204     protected List<AuthorizationRule> getMappedRules() {
205         return rules;
206     }
207
208     /**
209      * For OR mapping.
210      *
211      * @param aRules The rules.
212      */
213     protected void setMappedRules(List<AuthorizationRule> aRules) {
214         rules = aRules;
215     }
216 }