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