1 /* 2 * The Apache Software License, Version 1.1 3 * 4 * Copyright (c) 1999 The Apache Software Foundation. All rights 5 * reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * 19 * 3. The end-user documentation included with the redistribution, if 20 * any, must include the following acknowlegement: 21 * "This product includes software developed by the 22 * Apache Software Foundation (http://www.apache.org/)." 23 * Alternately, this acknowlegement may appear in the software itself, 24 * if and wherever such third-party acknowlegements normally appear. 25 * 26 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software 27 * Foundation" must not be used to endorse or promote products derived 28 * from this software without prior written permission. For written 29 * permission, please contact apache@apache.org. 30 * 31 * 5. Products derived from this software may not be called "Apache" 32 * nor may "Apache" appear in their names without prior written 33 * permission of the Apache Group. 34 * 35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 46 * SUCH DAMAGE. 47 * ==================================================================== 48 * 49 * This software consists of voluntary contributions made by many 50 * individuals on behalf of the Apache Software Foundation. For more 51 * information on the Apache Software Foundation, please see 52 * <http://www.apache.org/>. 53 * 54 * [Additional notices, if required by prior licensing conditions] 55 * 56 */ 57 58 59 package org.apache.tomcat.security.file; 60 61 62 import java.util.Enumeration; 63 import java.util.Hashtable; 64 65 66 /** 67 * In-memory representation of a defined group of users, which may be granted 68 * specific roles indirectly by virtue of their membership in a group. This 69 * class exhibits the following JavaBeans properties: 70 * <ul> 71 * <li><b>name</b> - Username that uniquely (within a particular security 72 * domain) identifies this user. 73 * <li><b>roles</b> - The set of role names explicitly assigned to this user. 74 * <li><b>users</b> - The set of users who are members of this group. 75 * </ul> 76 * 77 * @author Craig R. McClanahan 78 * @version $Revision: 1.2 $ $Date: 2000/02/26 02:32:15 $ 79 */ 80 81 final class FileRealmGroup { 82 83 84 /** 85 * The database containing this group. 86 */ 87 private FileRealmDatabase database = null; 88 89 90 /** 91 * The group name assigned to this group. 92 */ 93 private String name = null; 94 95 96 /** 97 * The set of roles assigned explicitly to this group, keyed by role name. 98 * The values are arbitrary. 99 */ 100 private Hashtable roles = new Hashtable(); 101 102 103 /** 104 * The set of users who are members of this group, keyed by username. 105 */ 106 private Hashtable users = new Hashtable(); 107 108 109 /** 110 * [Package Private] Create a new group with the specified group name. 111 * It is assumed that the creating entity has ensured that this 112 * group name is unique within this security realm. 113 * 114 * @param database The FileRealmDatabase containing the new group 115 * @param name The group name assigned to the new group 116 */ 117 FileRealmGroup(FileRealmDatabase database, String name) { 118 119 super(); 120 this.database = database; 121 this.name = name; 122 database.addGroup(this); 123 124 } 125 126 127 /** 128 * Add the explicit assignment of the specified role to this group. 129 * 130 * @param role The role being assigned to this group 131 */ 132 public void addRole(String role) { 133 134 database.addRole(role); 135 roles.put(role, role); 136 137 } 138 139 140 /** 141 * [Package Private] Add the specified user to the group members of 142 * this group. 143 * 144 * @param user User to be added 145 */ 146 void addUser(FileRealmUser user) { 147 148 users.put(user.getName(), user); 149 150 } 151 152 153 /** 154 * Remove this group from the database to which it belongs. 155 */ 156 public void destroy() { 157 158 Enumeration users = database.getUsers(); 159 while (users.hasMoreElements()) { 160 FileRealmUser user = (FileRealmUser) users.nextElement(); 161 user.remove(this); 162 } 163 database.remove(this); 164 165 } 166 167 168 /** 169 * Return the group name of this group. 170 */ 171 public String getName() { 172 173 return (name); 174 175 } 176 177 178 /** 179 * Return an enumeration of the roles explicitly assigned to this group. 180 * If there are no assigned roles, an empty enumeration is returned. 181 */ 182 public Enumeration getRoles() { 183 184 return (roles.elements()); 185 186 } 187 188 189 /** 190 * Return an enumeration of the users who are members of this group. 191 * Each element is an instance of FileRealmUser. 192 */ 193 public Enumeration getUsers() { 194 195 return (users.elements()); 196 197 } 198 199 200 /** 201 * Has this user been assigned the specified role, either directly or 202 * indirectly by virtue of group membership? 203 * 204 * @param role The role to be tested 205 */ 206 public boolean hasRole(String role) { 207 208 return (roles.get(role) != null); 209 210 } 211 212 213 /** 214 * Remove the specified explicitly assigned role from this group. 215 * 216 * @param role Role to be removed 217 */ 218 public void remove(String role) { 219 220 roles.remove(role); 221 222 } 223 224 225 /** 226 * [Package Private] Remove the specified user from membership in 227 * this group. 228 * 229 * @param user The user to be removed 230 */ 231 void remove(FileRealmUser user) { 232 233 users.remove(user.getName()); 234 235 } 236 237 238 }