1 65 66 67 package org.hsqldb; 68 69 import org.hsqldb.lib.HashMappedList; 70 import org.hsqldb.lib.HsqlArrayList; 71 import org.hsqldb.HsqlNameManager.HsqlName; 72 import org.hsqldb.SchemaManager.Schema; 73 74 80 99 class UserManager implements GrantConstants { 100 101 105 User sysUser = null; 106 107 115 private HashMappedList userList; 116 private GranteeManager granteeManager; 117 118 124 UserManager(Database database) throws HsqlException { 125 126 granteeManager = database.getGranteeManager(); 127 userList = new HashMappedList(); 128 129 createUser(GranteeManager.PUBLIC_ROLE_NAME, null); 130 131 sysUser = createUser(GranteeManager.SYSTEM_AUTHORIZATION_NAME, null); 132 133 sysUser.getGrantee().setAdminDirect(); 137 } 138 139 155 User createUser(String name, String password) throws HsqlException { 156 157 if (name == null) { 158 Trace.doAssert(false, Trace.getMessage(Trace.NULL_NAME)); 159 } 160 161 Grantee g = granteeManager.addGrantee(name); 169 User u = new User(name, password, g); 170 171 if (GranteeManager.SYSTEM_AUTHORIZATION_NAME.equals(name)) { 173 return u; 174 } 175 176 boolean success = userList.add(name, u); 177 178 if (!success) { 179 throw Trace.error(Trace.USER_ALREADY_EXISTS, name); 180 } 181 182 return u; 183 } 184 185 204 void dropUser(String name) throws HsqlException { 205 206 boolean reservedUser = GranteeManager.isReserved(name); 207 208 Trace.check(!reservedUser, Trace.NONMOD_ACCOUNT, name); 209 210 boolean result = granteeManager.removeGrantee(name); 211 212 Trace.check(result, Trace.NO_SUCH_GRANTEE, name); 213 214 User u = (User) userList.remove(name); 215 216 Trace.check(u != null, Trace.USER_NOT_FOUND, name); 217 } 218 219 223 User getUser(String name, String password) throws HsqlException { 224 225 if (name == null) { 226 name = ""; 227 } 228 229 if (password == null) { 230 password = ""; 231 } 232 233 if (name.equals(GranteeManager.PUBLIC_ROLE_NAME)) { 236 throw Trace.error(Trace.ACCESS_IS_DENIED); 237 } 238 239 name = name.toUpperCase(); 240 password = password.toUpperCase(); 241 242 User u = get(name); 243 244 u.checkPassword(password); 245 246 return u; 247 } 248 249 253 HashMappedList getUsers() { 254 return userList; 255 } 256 257 boolean exists(String name) { 258 return userList.get(name) == null ? false 259 : true; 260 } 261 262 266 User get(String name) throws HsqlException { 267 268 User u = (User) userList.get(name); 269 270 if (u == null) { 271 throw Trace.error(Trace.USER_NOT_FOUND, name); 272 } 273 274 return u; 275 } 276 277 299 HsqlArrayList listVisibleUsers(Session session, boolean andPublicUser) { 300 301 HsqlArrayList list; 302 User user; 303 boolean isAdmin; 304 String sessName; 305 String userName; 306 307 list = new HsqlArrayList(); 308 isAdmin = session.isAdmin(); 309 sessName = session.getUsername(); 310 311 if (userList == null || userList.size() == 0) { 312 return list; 313 } 314 315 for (int i = 0; i < userList.size(); i++) { 316 user = (User) userList.get(i); 317 318 if (user == null) { 319 continue; 320 } 321 322 userName = user.getName(); 323 324 if (GranteeManager.PUBLIC_ROLE_NAME.equals(userName)) { 325 if (andPublicUser) { 326 list.add(user); 327 } 328 } else if (isAdmin) { 329 list.add(user); 330 } else if (sessName.equals(userName)) { 331 list.add(user); 332 } 333 } 334 335 return list; 336 } 337 338 static String [] getRightsArray(int rights) { 340 return GranteeManager.getRightsArray(rights); 341 } 342 343 347 void removeDbObject(Object dbobject) { 348 granteeManager.removeDbObject(dbobject); 349 } 350 351 363 User getSysUser() { 364 return sysUser; 365 } 366 367 public synchronized void removeSchemaReference(Schema schema) { 368 369 for (int i = 0; i < userList.size(); i++) { 370 User user = (User) userList.get(i); 371 372 if (user.getInitialSchema() == schema.name) { 373 user.setInitialSchema(null); 374 } 375 } 376 } 377 } 378 | Popular Tags |