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