001: /*
002: * Copyright 2002,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.catalina.users;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021:
022: import org.apache.catalina.Group;
023: import org.apache.catalina.Role;
024: import org.apache.catalina.UserDatabase;
025: import org.apache.catalina.util.RequestUtil;
026:
027: /**
028: * <p>Concrete implementation of {@link User} for the
029: * {@link MemoryUserDatabase} implementation of {@link UserDatabase}.</p>
030: *
031: * @author Craig R. McClanahan
032: * @version $Revision: 1.4 $ $Date: 2004/02/27 14:58:50 $
033: * @since 4.1
034: */
035:
036: public class MemoryUser extends AbstractUser {
037:
038: // ----------------------------------------------------------- Constructors
039:
040: /**
041: * Package-private constructor used by the factory method in
042: * {@link MemoryUserDatabase}.
043: *
044: * @param database The {@link MemoryUserDatabase} that owns this user
045: * @param username Logon username of the new user
046: * @param password Logon password of the new user
047: * @param fullName Full name of the new user
048: */
049: MemoryUser(MemoryUserDatabase database, String username,
050: String password, String fullName) {
051:
052: super ();
053: this .database = database;
054: setUsername(username);
055: setPassword(password);
056: setFullName(fullName);
057:
058: }
059:
060: // ----------------------------------------------------- Instance Variables
061:
062: /**
063: * The {@link MemoryUserDatabase} that owns this user.
064: */
065: protected MemoryUserDatabase database = null;
066:
067: /**
068: * The set of {@link Group}s that this user is a member of.
069: */
070: protected ArrayList groups = new ArrayList();
071:
072: /**
073: * The set of {@link Role}s associated with this user.
074: */
075: protected ArrayList roles = new ArrayList();
076:
077: // ------------------------------------------------------------- Properties
078:
079: /**
080: * Return the set of {@link Group}s to which this user belongs.
081: */
082: public Iterator getGroups() {
083:
084: synchronized (groups) {
085: return (groups.iterator());
086: }
087:
088: }
089:
090: /**
091: * Return the set of {@link Role}s assigned specifically to this user.
092: */
093: public Iterator getRoles() {
094:
095: synchronized (roles) {
096: return (roles.iterator());
097: }
098:
099: }
100:
101: /**
102: * Return the {@link UserDatabase} within which this User is defined.
103: */
104: public UserDatabase getUserDatabase() {
105:
106: return (this .database);
107:
108: }
109:
110: // --------------------------------------------------------- Public Methods
111:
112: /**
113: * Add a new {@link Group} to those this user belongs to.
114: *
115: * @param group The new group
116: */
117: public void addGroup(Group group) {
118:
119: synchronized (groups) {
120: if (!groups.contains(group)) {
121: groups.add(group);
122: }
123: }
124:
125: }
126:
127: /**
128: * Add a new {@link Role} to those assigned specifically to this user.
129: *
130: * @param role The new role
131: */
132: public void addRole(Role role) {
133:
134: synchronized (roles) {
135: if (!roles.contains(role)) {
136: roles.add(role);
137: }
138: }
139:
140: }
141:
142: /**
143: * Is this user in the specified group?
144: *
145: * @param group The group to check
146: */
147: public boolean isInGroup(Group group) {
148:
149: synchronized (groups) {
150: return (groups.contains(group));
151: }
152:
153: }
154:
155: /**
156: * Is this user specifically assigned the specified {@link Role}? This
157: * method does <strong>NOT</strong> check for roles inherited based on
158: * {@link Group} membership.
159: *
160: * @param role The role to check
161: */
162: public boolean isInRole(Role role) {
163:
164: synchronized (roles) {
165: return (roles.contains(role));
166: }
167:
168: }
169:
170: /**
171: * Remove a {@link Group} from those this user belongs to.
172: *
173: * @param group The old group
174: */
175: public void removeGroup(Group group) {
176:
177: synchronized (groups) {
178: groups.remove(group);
179: }
180:
181: }
182:
183: /**
184: * Remove all {@link Group}s from those this user belongs to.
185: */
186: public void removeGroups() {
187:
188: synchronized (groups) {
189: groups.clear();
190: }
191:
192: }
193:
194: /**
195: * Remove a {@link Role} from those assigned to this user.
196: *
197: * @param role The old role
198: */
199: public void removeRole(Role role) {
200:
201: synchronized (roles) {
202: roles.remove(role);
203: }
204:
205: }
206:
207: /**
208: * Remove all {@link Role}s from those assigned to this user.
209: */
210: public void removeRoles() {
211:
212: synchronized (roles) {
213: roles.clear();
214: }
215:
216: }
217:
218: /**
219: * <p>Return a String representation of this user in XML format.</p>
220: *
221: * <p><strong>IMPLEMENTATION NOTE</strong> - For backwards compatibility,
222: * the reader that processes this entry will accept either
223: * <code>username</code> or </code>name</code> for the username
224: * property.</p>
225: */
226: public String toString() {
227:
228: StringBuffer sb = new StringBuffer("<user username=\"");
229: sb.append(RequestUtil.filter(username));
230: sb.append("\" password=\"");
231: sb.append(RequestUtil.filter(password));
232: sb.append("\"");
233: if (fullName != null) {
234: sb.append(" fullName=\"");
235: sb.append(RequestUtil.filter(fullName));
236: sb.append("\"");
237: }
238: synchronized (groups) {
239: if (groups.size() > 0) {
240: sb.append(" groups=\"");
241: int n = 0;
242: Iterator values = groups.iterator();
243: while (values.hasNext()) {
244: if (n > 0) {
245: sb.append(',');
246: }
247: n++;
248: sb.append(RequestUtil
249: .filter(((Group) values.next())
250: .getGroupname()));
251: }
252: sb.append("\"");
253: }
254: }
255: synchronized (roles) {
256: if (roles.size() > 0) {
257: sb.append(" roles=\"");
258: int n = 0;
259: Iterator values = roles.iterator();
260: while (values.hasNext()) {
261: if (n > 0) {
262: sb.append(',');
263: }
264: n++;
265: sb.append(RequestUtil.filter(((Role) values.next())
266: .getRolename()));
267: }
268: sb.append("\"");
269: }
270: }
271: sb.append("/>");
272: return (sb.toString());
273:
274: }
275:
276: }
|