2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
19
package org.apache.catalina.users;
22
import java.util.ArrayList;
23
import java.util.Iterator;
25
import org.apache.catalina.Group;
26
import org.apache.catalina.Role;
27
import org.apache.catalina.UserDatabase;
28
import org.apache.catalina.util.RequestUtil;
31
* <p>Concrete implementation of {@link org.apache.catalina.User} for the
32
* {@link MemoryUserDatabase} implementation of {@link UserDatabase}.</p>
34
* @author Craig R. McClanahan
35
* @version $Id: MemoryUser.java 939353 2010-04-29 15:50:43Z kkolinko $
39
public class MemoryUser extends AbstractUser {
42
// ----------------------------------------------------------- Constructors
46
* Package-private constructor used by the factory method in
47
* {@link MemoryUserDatabase}.
49
* @param database The {@link MemoryUserDatabase} that owns this user
50
* @param username Logon username of the new user
51
* @param password Logon password of the new user
52
* @param fullName Full name of the new user
54
MemoryUser(MemoryUserDatabase database, String username,
55
String password, String fullName) {
58
this.database = database;
59
setUsername(username);
60
setPassword(password);
61
setFullName(fullName);
66
// ----------------------------------------------------- Instance Variables
70
* The {@link MemoryUserDatabase} that owns this user.
72
protected MemoryUserDatabase database = null;
76
* The set of {@link Group}s that this user is a member of.
78
protected ArrayList groups = new ArrayList();
82
* The set of {@link Role}s associated with this user.
84
protected ArrayList roles = new ArrayList();
87
// ------------------------------------------------------------- Properties
91
* Return the set of {@link Group}s to which this user belongs.
93
public Iterator getGroups() {
95
synchronized (groups) {
96
return (groups.iterator());
103
* Return the set of {@link Role}s assigned specifically to this user.
105
public Iterator getRoles() {
107
synchronized (roles) {
108
return (roles.iterator());
115
* Return the {@link UserDatabase} within which this User is defined.
117
public UserDatabase getUserDatabase() {
119
return (this.database);
124
// --------------------------------------------------------- Public Methods
128
* Add a new {@link Group} to those this user belongs to.
130
* @param group The new group
132
public void addGroup(Group group) {
134
synchronized (groups) {
135
if (!groups.contains(group)) {
144
* Add a new {@link Role} to those assigned specifically to this user.
146
* @param role The new role
148
public void addRole(Role role) {
150
synchronized (roles) {
151
if (!roles.contains(role)) {
160
* Is this user in the specified group?
162
* @param group The group to check
164
public boolean isInGroup(Group group) {
166
synchronized (groups) {
167
return (groups.contains(group));
174
* Is this user specifically assigned the specified {@link Role}? This
175
* method does <strong>NOT</strong> check for roles inherited based on
176
* {@link Group} membership.
178
* @param role The role to check
180
public boolean isInRole(Role role) {
182
synchronized (roles) {
183
return (roles.contains(role));
190
* Remove a {@link Group} from those this user belongs to.
192
* @param group The old group
194
public void removeGroup(Group group) {
196
synchronized (groups) {
197
groups.remove(group);
204
* Remove all {@link Group}s from those this user belongs to.
206
public void removeGroups() {
208
synchronized (groups) {
216
* Remove a {@link Role} from those assigned to this user.
218
* @param role The old role
220
public void removeRole(Role role) {
222
synchronized (roles) {
230
* Remove all {@link Role}s from those assigned to this user.
232
public void removeRoles() {
234
synchronized (roles) {
242
* <p>Return a String representation of this user in XML format.</p>
244
* <p><strong>IMPLEMENTATION NOTE</strong> - For backwards compatibility,
245
* the reader that processes this entry will accept either
246
* <code>username</code> or </code>name</code> for the username
249
public String toString() {
251
StringBuffer sb = new StringBuffer("<user username=\"");
252
sb.append(RequestUtil.filter(username));
253
sb.append("\" password=\"");
254
sb.append(RequestUtil.filter(password));
256
if (fullName != null) {
257
sb.append(" fullName=\"");
258
sb.append(RequestUtil.filter(fullName));
261
synchronized (groups) {
262
if (groups.size() > 0) {
263
sb.append(" groups=\"");
265
Iterator values = groups.iterator();
266
while (values.hasNext()) {
271
sb.append(RequestUtil.filter(((Group) values.next()).getGroupname()));
276
synchronized (roles) {
277
if (roles.size() > 0) {
278
sb.append(" roles=\"");
280
Iterator values = roles.iterator();
281
while (values.hasNext()) {
286
sb.append(RequestUtil.filter(((Role) values.next()).getRolename()));
292
return (sb.toString());