001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.portal.tools.service;
018:
019: import java.io.IOException;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.Map;
023: import java.util.Properties;
024: import java.util.StringTokenizer;
025:
026: import org.apache.avalon.framework.CascadingRuntimeException;
027: import org.apache.cocoon.portal.profile.PortalUser;
028: import org.apache.cocoon.portal.tools.helper.MultipleRoleMatcher;
029: import org.apache.cocoon.portal.tools.helper.RoleMatcher;
030: import org.apache.cocoon.portal.tools.helper.SingleRoleMatcher;
031: import org.apache.cocoon.util.WildcardMatcherHelper;
032: import org.apache.excalibur.source.Source;
033:
034: /**
035: * Service, that provides access to the user rights configuration.
036: *
037: * @version CVS $Id: UserRightsService.java 156704 2005-03-09 22:57:22Z antonio $
038: */
039: public class UserRightsService {
040:
041: /**
042: * The properties' location.
043: */
044: private Source location;
045:
046: /**
047: * The properties.
048: */
049: private Properties properties;
050:
051: /**
052: * Signals when the properties have been loaded last.
053: */
054: private long lastModified = -1;
055:
056: /**
057: * Signals whether to reload the properties.
058: */
059: private boolean reload = false;
060:
061: /**
062: * Holds the userrights.
063: */
064: private Map userrights;
065:
066: /**
067: * @return The location
068: */
069:
070: public Source getLocation() {
071: return this .location;
072: }
073:
074: /**
075: * @param location The location to set
076: */
077:
078: public void setLocation(Source location) {
079: this .location = location;
080: }
081:
082: /**
083: * @return The reload
084: */
085: public boolean getReload() {
086: return this .reload;
087: }
088:
089: /**
090: * @param reload The reload to set
091: */
092: public void setReload(boolean reload) {
093: this .reload = reload;
094: }
095:
096: /**
097: * Initialize the bean.
098: */
099: public void initialize() {
100: boolean load;
101:
102: // Check if called for the first time
103: if (this .properties == null) {
104: load = true;
105: } else {
106: // Check if reload is required
107: load = this .reload;
108: }
109:
110: try {
111: if (load) {
112: // Check file timestamp
113: long lastModified = this .location.getLastModified();
114: if (this .lastModified >= lastModified) {
115: load = false;
116: }
117:
118: if (load) {
119: this .lastModified = lastModified;
120: this .properties = new Properties();
121: this .properties
122: .load(this .location.getInputStream());
123: this .parseProperties();
124: }
125: }
126: } catch (IOException e) {
127: throw new CascadingRuntimeException(e.getMessage(), e);
128: }
129: }
130:
131: /**
132: * @return Whether the current user is allowed to call the given url.
133: */
134: public boolean userIsAllowed(String url, PortalUser user) {
135: this .initialize();
136:
137: boolean isAllowed = true;
138:
139: // Iterate all userrights
140: final Iterator iterator = this .userrights.entrySet().iterator();
141: while (iterator.hasNext() && isAllowed) {
142: final Map.Entry entry = (Map.Entry) iterator.next();
143: final String pattern = (String) entry.getKey();
144:
145: // If userright matches try to find a matching role
146: if (WildcardMatcherHelper.match(pattern, url) != null) {
147: final RoleMatcher[] matcher = (RoleMatcher[]) entry
148: .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 id, PortalUser user) {
165: this .initialize();
166:
167: boolean isAllowed = true;
168:
169: // Iterate all userrights
170: final Iterator iterator = this .userrights.entrySet().iterator();
171: while (iterator.hasNext() && isAllowed) {
172: final Map.Entry entry = (Map.Entry) iterator.next();
173: final String pattern = (String) entry.getKey();
174:
175: // If userright matches try to find a matching role
176: if (WildcardMatcherHelper.match(pattern, id) != null) {
177: final RoleMatcher[] matcher = (RoleMatcher[]) entry
178: .getValue();
179:
180: isAllowed = false;
181:
182: int length = matcher.length;
183: for (int i = 0; i < length; i++) {
184: if (matcher[i].matches(user)) {
185: isAllowed = true;
186: }
187: }
188: }
189: }
190:
191: return isAllowed;
192: }
193:
194: /**
195: * Parse the properties.
196: */
197: private void parseProperties() {
198: final Map ur = new HashMap();
199:
200: final Iterator iterator = this .properties.entrySet().iterator();
201: while (iterator.hasNext()) {
202: final Map.Entry entry = (Map.Entry) iterator.next();
203: ur.put((String) entry.getKey(), this
204: .buildRoles((String) entry.getValue()));
205: }
206:
207: this .userrights = ur;
208: }
209:
210: /**
211: * @return A list representing the given roles.
212: */
213: private RoleMatcher[] buildRoles(String roles) {
214: StringTokenizer tokenizer = new StringTokenizer(roles, ",",
215: false);
216:
217: RoleMatcher[] result = new RoleMatcher[tokenizer.countTokens()];
218:
219: String token;
220: int i = 0;
221: while (tokenizer.hasMoreTokens()) {
222: token = tokenizer.nextToken();
223: if (token.indexOf(MultipleRoleMatcher.ROLE_SEPARATOR) == -1) {
224: result[i] = new SingleRoleMatcher(token);
225: } else {
226: result[i] = new MultipleRoleMatcher(token);
227: }
228: i++;
229: }
230:
231: return result;
232: }
233: }
|