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