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.auth;
018:
019: import java.io.Serializable;
020: import java.util.ArrayList;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Map;
025: import java.util.StringTokenizer;
026:
027: /**
028: * This object represents the current user. Each user must have a unique
029: * identifier (per {@link org.apache.cocoon.auth.SecurityHandler}).
030: *
031: * @version $Id: StandardUser.java 433543 2006-08-22 06:22:54Z crossley $
032: */
033: public class StandardUser implements User, Serializable {
034:
035: /** The unique id of the user. */
036: protected String id;
037:
038: /** The user attributes. */
039: protected final Map attributes = new HashMap();
040:
041: /** Cache the roles info. */
042: protected List roles;
043:
044: /**
045: * Create a new user object.
046: * @param userId The unique identifier for this user.
047: */
048: public StandardUser(final String userId) {
049: this .id = userId;
050: }
051:
052: /**
053: * Create a new user object.
054: * If you use this constructor, you have to ensure that the id of the user
055: * is set accordingly before the user object is used
056: */
057: public StandardUser() {
058: // nothing to do here, we have to ensure that the id is set!
059: }
060:
061: /**
062: * @see org.apache.cocoon.auth.User#getId()
063: */
064: public String getId() {
065: return this .id;
066: }
067:
068: /**
069: * @see org.apache.cocoon.auth.User#setAttribute(java.lang.String, java.lang.Object)
070: */
071: public void setAttribute(final String key, final Object value) {
072: this .attributes.put(key, value);
073: }
074:
075: /**
076: * @see org.apache.cocoon.auth.User#removeAttribute(java.lang.String)
077: */
078: public void removeAttribute(final String key) {
079: this .attributes.remove(key);
080: }
081:
082: /**
083: * @see org.apache.cocoon.auth.User#getAttribute(java.lang.String)
084: */
085: public Object getAttribute(final String key) {
086: return this .attributes.get(key);
087: }
088:
089: /**
090: * @see org.apache.cocoon.auth.User#getAttributeNames()
091: */
092: public Iterator getAttributeNames() {
093: return this .attributes.keySet().iterator();
094: }
095:
096: /**
097: * Check if the user is in a given role. This default implementation
098: * checks the two attributes "roles" and "role". If the incomming role
099: * is found in one of the two attributes, true is returned.
100: * Subclasses should override this method.
101: *
102: * @param role The role to test.
103: * @return Returns true if the user has the role, otherwise false.
104: * @see org.apache.cocoon.auth.User#isUserInRole(java.lang.String)
105: */
106: public boolean isUserInRole(final String role) {
107: if (this .roles == null) {
108: this .roles = new ArrayList();
109: final Object allRoles = this .getAttribute("roles");
110: if (allRoles != null && allRoles instanceof String) {
111: final StringTokenizer st = new StringTokenizer(
112: (String) allRoles, ",");
113: while (st.hasMoreElements()) {
114: this .roles.add(st.nextElement());
115: }
116: }
117: final Object singleRole = this .getAttribute("role");
118: if (singleRole != null && singleRole instanceof String) {
119: this.roles.add(singleRole);
120: }
121: }
122: return this.roles.contains(role);
123: }
124: }
|