1 9 package org.ozoneDB.core; 10 11 import java.io.*; 12 import org.ozoneDB.DxLib.*; 13 import org.ozoneDB.util.*; 14 15 16 24 public final class UserManager extends ServerComponent { 25 26 protected final static long serialVersionUID = 2; 28 protected final static byte subSerialVersionUID = 1; 29 30 public final static String GROUP_TABLE = "ozoneDB.userManager.groupTable"; 32 public final static String USER_TABLE = "ozoneDB.userManager.userTable"; 33 34 protected transient Env env; 35 36 39 protected DxMap userTable; 40 41 44 protected DxMap idUserTable; 45 46 49 protected DxMap groupTable; 50 51 54 protected DxMap idGroupTable; 55 56 57 61 protected final static int SYSTEM_USER_ID = -1; 62 63 66 protected final static User garbageCollectorUser = new User("garbageCollector",SYSTEM_USER_ID); 67 68 71 protected User getGarbageCollectorUser() { 72 return garbageCollectorUser; 73 } 74 75 public UserManager( Env _env ) { 76 super( _env ); 77 env = _env; 78 groupTable = new DxHashMap(); 79 userTable = new DxHashMap(); 80 } 81 82 83 public void startup() throws Exception { 84 env.logWriter.newEntry( this, "startup...", LogWriter.INFO ); 85 86 groupTable = (DxMap)env.state.property( GROUP_TABLE, null ); 87 userTable = (DxMap)env.state.property( USER_TABLE, null ); 88 89 boolean isInitialized = true; 90 if (groupTable == null || userTable == null) { 91 env.logWriter.newEntry( this, "No state properties found. Initializing...", LogWriter.INFO ); 92 groupTable = new DxHashMap(); 93 idGroupTable = new DxHashMap(); 94 userTable = new DxHashMap(); 95 isInitialized = false; 96 } 97 98 idUserTable = new DxHashMap(); 100 DxIterator it = userTable.iterator(); 101 User user; 102 while ((user = (User)it.next()) != null) { 103 idUserTable.addForKey( user, user.id() ); 104 } 105 106 idGroupTable = new DxHashMap(); 108 it = groupTable.iterator(); 109 Group group; 110 while ((group = (Group)it.next()) != null) { 111 idGroupTable.addForKey( group, group.id() ); 112 } 113 114 if (isInitialized == false) { 116 String adminName = System.getProperty( "user.name" ); 117 118 env.logWriter.newEntry( this, "admin user: " + adminName, LogWriter.INFO ); 119 120 newUser( adminName, 0 ); 121 newGroup( "admin", 0 ); 122 addUserToGroup( adminName, "admin" ); 123 } 124 } 125 126 127 public void shutdown() throws Exception { 128 env.logWriter.newEntry( this, "shutdown...", LogWriter.INFO ); 129 save(); 130 } 131 132 133 public void save() throws Exception { 134 env.state.setProperty( GROUP_TABLE, groupTable ); 135 env.state.setProperty( USER_TABLE, userTable ); 136 } 137 138 139 public boolean checkPermission( User user, ObjectContainer container, int lockLevel ) { 140 if (lockLevel <= Lock.LEVEL_READ) { 141 return checkReadPermission( user, container ); 142 } else { 143 return checkWritePermission( user, container ); 144 } 145 } 146 147 148 protected boolean checkReadPermission( User reader, ObjectContainer container ) { 149 if (container.permissions().allRead()) { 152 return true; 153 } else if (container.permissions().ownerID == reader.id) { 154 155 return true; 156 } else { 157 158 if (container.permissions().groupRead()) { 159 User owner = userForID( container.permissions().ownerID ); 160 DxIterator it = groupsOfUser( owner ).iterator(); 162 Group group; 163 while ((group = (Group)it.next()) != null) { 164 if (group.containsUser( reader )) { 165 return true; 166 } 167 } 168 } 169 } 170 171 if (reader.getID()==SYSTEM_USER_ID) { 172 return true; 173 } 174 175 return false; 176 } 177 178 179 protected boolean checkWritePermission( User locker, ObjectContainer container ) { 180 if (container.permissions().allLock()) { 183 return true; 184 } else if (container.permissions().ownerID == locker.id) { 185 186 return true; 187 } else { 188 189 if (container.permissions().groupLock()) { 190 User owner = userForID( container.permissions().ownerID ); 191 DxIterator it = groupsOfUser( owner ).iterator(); 193 Group group; 194 while ((group = (Group)it.next()) != null) { 195 if (group.containsUser( locker )) { 196 return true; 197 } 198 } 199 } 200 } 201 202 if (locker.getID()==SYSTEM_USER_ID) { 203 return true; 204 } 205 206 return false; 207 } 208 209 210 public void newGroup( String name, int id ) throws UserManagerException { 211 if (name == null) { 212 throw new UserManagerException( "username is null." ); 213 } 214 Group group = new Group( name, id ); 215 216 if (groupForID( id ) != null) { 217 throw new UserManagerException( "Group id " + id + " already exists." ); 218 } 219 220 if (groupForName( name ) != null) { 221 throw new UserManagerException( "Group name '" + name + "' already exists." ); 222 } 223 224 groupTable.addForKey( group, name ); 225 idGroupTable.addForKey( group, new Integer ( id ) ); 226 227 setChanged(); 228 } 229 230 231 234 public void removeGroup( String name ) throws UserManagerException { 235 if (name == null) { 236 throw new UserManagerException( "username is null." ); 237 } 238 Group group = groupForName( name ); 239 240 if (group == null) { 241 throw new UserManagerException( "Group '" + name + "' does not exist." ); 242 } 243 244 groupTable.removeForKey( group.name ); 245 idGroupTable.removeForKey( new Integer ( group.id ) ); 246 247 setChanged(); 248 } 249 250 251 protected DxBag groupsOfUser( User user ) { 252 DxArrayBag result = new DxArrayBag(); 253 254 DxIterator it = groupTable.iterator(); 255 Group group; 256 while ((group = (Group)it.next()) != null) { 257 if (group.containsUser( user )) { 258 result.add( group ); 259 } 260 } 261 return result; 262 } 263 264 265 public void newUser( String name, int id ) throws UserManagerException { 266 newUser( name, name, id); 267 } 268 269 270 public void newUser( String name, String passwd, int id ) throws UserManagerException { 271 if (name == null) { 272 throw new UserManagerException( "username is null." ); 273 } 274 275 if (passwd == null) { 276 passwd = name; 277 } 278 279 User user = new User( name, passwd, id ); 280 281 if (userForID( id ) != null) { 282 throw new UserManagerException( "User id " + id + " already exists." ); 283 } 284 if (userForName( name ) != null) { 285 throw new UserManagerException( "User name '" + name + "' already exists." ); 286 } 287 288 userTable.addForKey( user, user.name ); 289 idUserTable.addForKey( user, new Integer ( user.id ) ); 290 291 setChanged(); 292 } 293 294 295 public void addUserToGroup( String userName, String groupName ) throws UserManagerException { 296 if (groupName == null) { 297 throw new UserManagerException( "groupname is null." ); 298 } 299 if (userName == null) { 300 throw new UserManagerException( "username is null." ); 301 } 302 Group group = groupForName( groupName ); 303 User user = userForName( userName ); 304 305 if (group == null) { 306 throw new UserManagerException( "Group '" + groupName + "' does not exist." ); 307 } 308 if (user == null) { 309 throw new UserManagerException( "User '" + userName + "' does not exist." ); 310 } 311 312 if (!group.addUser( user )) { 313 throw new UserManagerException( "User '" + userName + "' is in this group already." ); 314 } 315 316 setChanged(); 317 } 318 319 320 public void removeUserFromGroup( String userName, String groupName ) throws UserManagerException { 321 if (groupName == null) { 322 throw new UserManagerException( "groupname is null." ); 323 } 324 if (userName == null) { 325 throw new UserManagerException( "username is null." ); 326 } 327 Group group = groupForName( groupName ); 328 User user = userForName( userName ); 329 330 if (group == null) { 331 throw new UserManagerException( "Group '" + groupName + "' does not exist." ); 332 } 333 if (user == null) { 334 throw new UserManagerException( "User '" + userName + "' does not exist." ); 335 } 336 337 if (!group.containsUser( user )) { 338 throw new UserManagerException( "User '" + userName + "' is not member of '" + groupName + "'." ); 339 } 340 341 group.removeUser( user ); 342 setChanged(); 343 } 344 345 346 public void removeUser( String name ) throws UserManagerException { 347 if (name == null) { 348 throw new UserManagerException( "username is null." ); 349 } 350 User user = (User)userTable.removeForKey( name ); 351 if (user == null) { 352 throw new UserManagerException( "User '" + name + "' does not exist." ); 353 } 354 355 idUserTable.removeForKey( new Integer ( user.id ) ); 356 357 DxIterator it = groupsOfUser( user ).iterator(); 359 Group group; 360 while ((group = (Group)it.next()) != null) { 361 group.removeUser( user ); 362 } 363 364 setChanged(); 365 } 366 367 368 public Group groupForName( String name ) throws UserManagerException { 369 if (name == null) { 370 throw new UserManagerException( "username is null." ); 371 } 372 return (Group)groupTable.elementForKey( name ); 373 } 374 375 376 public Group groupForID( int id ) { 377 return (Group)idGroupTable.elementForKey( new Integer ( id ) ); 378 } 379 380 381 public User userForName( String name ) throws UserManagerException { 382 if (name == null) { 383 throw new UserManagerException( "username is null." ); 384 } 385 return (User)userTable.elementForKey( name ); 386 } 387 388 389 public User userForID( int id ) { 390 return (User)idUserTable.elementForKey( new Integer ( id ) ); 391 } 392 393 394 public DxCollection allGroups() { 395 return groupTable; 396 } 397 398 399 public DxCollection allUsers() { 400 return userTable; 401 } 402 } 403 | Popular Tags |