KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > tools > service > UserRightsService


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
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.apache.cocoon.portal.tools.service;
17
18 import java.io.IOException JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Properties JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24
25 import org.apache.avalon.framework.CascadingRuntimeException;
26 import org.apache.cocoon.matching.helpers.WildcardHelper;
27 import org.apache.cocoon.portal.profile.PortalUser;
28 import org.apache.cocoon.portal.tools.helper.MultipleRoleMatcher;
29 import org.apache.cocoon.portal.tools.helper.RoleMatcher;
30 import org.apache.cocoon.portal.tools.helper.SingleRoleMatcher;
31 import org.apache.excalibur.source.Source;
32
33 /**
34  * Service, that provides access to the user rights configuration.
35  *
36  * @version CVS $Id: UserRightsService.java 156704 2005-03-09 22:57:22Z antonio $
37  */

38 public class UserRightsService {
39     
40     /**
41      * The properties' location.
42      */

43     private Source location;
44
45     /**
46      * The properties.
47      */

48     private Properties JavaDoc properties;
49
50     /**
51      * Signals when the properties have been loaded last.
52      */

53     private long lastModified = -1;
54
55     /**
56      * Signals whether to reload the properties.
57      */

58     private boolean reload = false;
59
60     /**
61      * Holds the userrights.
62      */

63     private Map JavaDoc userrights;
64
65     /**
66      * @return The location
67      */

68     
69     public Source getLocation() {
70         return this.location;
71     }
72
73     /**
74      * @param location The location to set
75      */

76
77     public void setLocation(Source location) {
78         this.location = location;
79     }
80
81     /**
82      * @return The reload
83      */

84     public boolean getReload() {
85         return this.reload;
86     }
87
88     /**
89      * @param reload The reload to set
90      */

91     public void setReload(boolean reload) {
92         this.reload = reload;
93     }
94
95     /**
96      * Initialize the bean.
97      */

98     public void initialize() {
99         boolean load;
100
101         // Check if called for the first time
102
if (this.properties == null) {
103             load = true;
104         } else {
105             // Check if reload is required
106
load = this.reload;
107         }
108
109         try {
110             if (load) {
111                 // Check file timestamp
112
long lastModified = this.location.getLastModified();
113                 if (this.lastModified >= lastModified) {
114                     load = false;
115                 }
116
117                 if (load) {
118                     this.lastModified = lastModified;
119                     this.properties = new Properties JavaDoc();
120                     this.properties.load(this.location.getInputStream());
121                     this.parseProperties();
122                 }
123             }
124         } catch (IOException JavaDoc e) {
125             throw new CascadingRuntimeException(e.getMessage(), e);
126         }
127     }
128
129     /**
130      * @return Whether the current user is allowed to call the given url.
131      */

132     public boolean userIsAllowed(String JavaDoc url, PortalUser user) {
133         this.initialize();
134
135         boolean isAllowed = true;
136
137         // Iterate all userrights
138
Iterator JavaDoc iterator = this.userrights.entrySet().iterator();
139         Map.Entry JavaDoc entry;
140         int[] pattern;
141         RoleMatcher[] matcher;
142         while (iterator.hasNext() && isAllowed) {
143             entry = (Map.Entry JavaDoc)iterator.next();
144             pattern = (int[])entry.getKey();
145
146             // If userright matches try to find a matching role
147
if (WildcardHelper.match(new HashMap JavaDoc(), url, pattern)) {
148                 matcher = (RoleMatcher[])entry.getValue();
149
150                 isAllowed = false;
151
152                 int length = matcher.length;
153                 for (int i = 0; i < length; i++) {
154                     if (matcher[i].matches(user)) {
155                         isAllowed = true;
156                     }
157                 }
158             }
159         }
160
161         return isAllowed;
162     }
163
164     public boolean userFunctionIsAllowed(String JavaDoc id, PortalUser user) {
165         this.initialize();
166
167         boolean isAllowed = true;
168
169         // Iterate all userrights
170
Iterator JavaDoc iterator = this.userrights.entrySet().iterator();
171         Map.Entry JavaDoc entry;
172         int[] pattern;
173         RoleMatcher[] matcher;
174         while (iterator.hasNext() && isAllowed) {
175             entry = (Map.Entry JavaDoc)iterator.next();
176             pattern = (int[])entry.getKey();
177
178             // If userright matches try to find a matching role
179
if (WildcardHelper.match(new HashMap JavaDoc(), id, pattern)) {
180                 matcher = (RoleMatcher[])entry.getValue();
181
182                 isAllowed = false;
183
184                 int length = matcher.length;
185                 for (int i = 0; i < length; i++) {
186                     if (matcher[i].matches(user)) {
187                         isAllowed = true;
188                     }
189                 }
190             }
191         }
192
193         return isAllowed;
194     }
195
196     /**
197      * Parse the properties.
198      */

199     private void parseProperties() {
200         Map JavaDoc userrights = new HashMap JavaDoc();
201
202         Iterator JavaDoc iterator = this.properties.entrySet().iterator();
203         Map.Entry JavaDoc entry;
204         while (iterator.hasNext()) {
205             entry = (Map.Entry JavaDoc)iterator.next();
206             userrights.put(
207                 WildcardHelper.compilePattern((String JavaDoc)entry.getKey()),
208                 this.buildRoles((String JavaDoc)entry.getValue()));
209         }
210
211         this.userrights = userrights;
212     }
213
214     /**
215      * @return A list representing the given roles.
216      */

217     private RoleMatcher[] buildRoles(String JavaDoc roles) {
218         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(roles, ",", false);
219
220         RoleMatcher[] result = new RoleMatcher[tokenizer.countTokens()];
221
222         String JavaDoc token;
223         int i = 0;
224         while (tokenizer.hasMoreTokens()) {
225             token = tokenizer.nextToken();
226             if (token.indexOf(MultipleRoleMatcher.ROLE_SEPARATOR) == -1) {
227                 result[i] = new SingleRoleMatcher(token);
228             } else {
229                 result[i] = new MultipleRoleMatcher(token);
230             }
231             i++;
232         }
233
234         return result;
235     }
236 }
237
Popular Tags