📄 usermanager.java
字号:
// You can redistribute this software and/or modify it under the terms of// the Ozone Core License version 1 published by ozone-db.org.//// The original code and portions created by SMB are// Copyright (C) 1997-2000 by SMB GmbH. All rights reserved.//// $Id: UserManager.java,v 1.22 2000/10/28 16:55:17 daniela Exp $package org.ozoneDB.core;import java.io.*;import org.ozoneDB.DxLib.*;import org.ozoneDB.util.*;/** * The UserManager holds all information about users and user groups. * * * @author <a href="http://www.softwarebuero.de/">SMB</a> * @version $Revision: 1.22 $Date: 2000/10/28 16:55:17 $ * @see User * qsee Group */public final class UserManager { // magic number for streaming protected final static long serialVersionUID = 2; protected final static byte subSerialVersionUID = 1; // property names public final static String GROUP_TABLE = "ozoneDB.userManager.groupTable"; public final static String USER_TABLE = "ozoneDB.userManager.userTable"; protected transient Env env; /** * All currently known users. Maps String into User. */ protected DxMap userTable; /** * All currently known users. Maps IDs into User. */ protected DxMap idUserTable; /** * All currently known groups. Maps String into Group. */ protected DxMap groupTable; /** * All currently known groups. Maps IDs into Group. */ protected DxMap idGroupTable; public UserManager( Env _env ) { env = _env; groupTable = new DxHashMap(); userTable = new DxHashMap(); } public void startup() throws Exception { env.logWriter.newEntry( this, "startup...", LogWriter.INFO ); groupTable = (DxMap)env.state.property( GROUP_TABLE, null ); userTable = (DxMap)env.state.property( USER_TABLE, null ); if (groupTable == null || userTable == null) { env.logWriter.newEntry( this, "No state properties found. Initializing...", LogWriter.INFO ); groupTable = new DxHashMap(); idGroupTable = new DxHashMap(); userTable = new DxHashMap(); } // initialize idUserTable from the content of userTable idUserTable = new DxHashMap(); DxIterator it = userTable.iterator(); User user; while ((user = (User)it.next()) != null) { idUserTable.addForKey( user, user.id() ); } // initialize idGroupTable from the content of groupTable idGroupTable = new DxHashMap(); it = groupTable.iterator(); Group group; while ((group = (Group)it.next()) != null) { idGroupTable.addForKey( group, group.id() ); } } public void shutdown() throws Exception { env.logWriter.newEntry( this, "shutdown...", LogWriter.INFO ); env.state.setProperty( GROUP_TABLE, groupTable ); env.state.setProperty( USER_TABLE, userTable ); } public boolean checkPermission( User user, ObjectContainer container, int lockLevel ) { if (lockLevel <= Lock.LEVEL_READ) { return checkReadPermission( user, container ); } else { return checkWritePermission( user, container ); } } protected boolean checkReadPermission( User reader, ObjectContainer container ) { // allRead can be checked fast and is true in most cases so we // check it first if (container.permissions().allRead()) { return true; } else if (container.permissions().ownerID == reader.id) { return true; } else { if (container.permissions().groupRead()) { User owner = userForID( container.permissions().ownerID ); // if reader is in any group of the owner permission is granted DxIterator it = groupsOfUser( owner ).iterator(); Group group; while ((group = (Group)it.next()) != null) { if (group.containsUser( reader )) { return true; } } } } return false; } protected boolean checkWritePermission( User locker, ObjectContainer container ) { // allRead can be checked fast and is true in most cases so we // check it first if (container.permissions().allLock()) { return true; } else if (container.permissions().ownerID == locker.id) { return true; } else { if (container.permissions().groupLock()) { User owner = userForID( container.permissions().ownerID ); // if reader is in any group of the owner permission is granted DxIterator it = groupsOfUser( owner ).iterator(); Group group; while ((group = (Group)it.next()) != null) { if (group.containsUser( locker )) { return true; } } } } return false; } public void newGroup( String name, int id ) throws UserManagerExc { Group group = new Group( name, id ); if (groupForID( id ) != null) { throw new UserManagerExc( "Group id " + id + " already exists." ); } if (groupForName( name ) != null) { throw new UserManagerExc( "Group name '" + name + "' already exists." ); } groupTable.addForKey( group, name ); idGroupTable.addForKey( group, new Integer( id ) ); } /** * Delete the group for the given name. */ public void removeGroup( String name ) throws UserManagerExc { Group group = groupForName( name ); if (group == null) { throw new UserManagerExc( "Group '" + name + "' does not exist." ); } groupTable.removeForKey( group.name ); idGroupTable.removeForKey( new Integer( group.id ) ); } protected DxBag groupsOfUser( User user ) { DxArrayBag result = new DxArrayBag(); DxIterator it = groupTable.iterator(); Group group; while ((group = (Group)it.next()) != null) { if (group.containsUser( user )) { result.add( group ); } } return result; } public void newUser( String name, int id ) throws UserManagerExc { User user = new User( name, id ); if (userForID( id ) != null) { throw new UserManagerExc( "User id " + id + " already exists." ); } if (userForName( name ) != null) { throw new UserManagerExc( "User name '" + name + "' already exists." ); } userTable.addForKey( user, user.name ); idUserTable.addForKey( user, new Integer( user.id ) ); } public void addUserToGroup( String userName, String groupName ) throws UserManagerExc { Group group = groupForName( groupName ); User user = userForName( userName ); if (group == null) { throw new UserManagerExc( "Group '" + groupName + "' does not exist." ); } if (user == null) { throw new UserManagerExc( "User '" + userName + "' does not exist." ); } if (!group.addUser( user )) { throw new UserManagerExc( "User '" + userName + "' is in this group already." ); } } public void removeUserFromGroup( String userName, String groupName ) throws UserManagerExc { Group group = groupForName( groupName ); User user = userForName( userName ); if (group == null) { throw new UserManagerExc( "Group '" + groupName + "' does not exist." ); } if (user == null) { throw new UserManagerExc( "User '" + userName + "' does not exist." ); } if (!group.containsUser( user )) { throw new UserManagerExc( "User '" + userName + "' is not member of '" + groupName + "'." ); } group.removeUser( user ); } public void removeUser( String name ) throws UserManagerExc { User user = (User)userTable.removeForKey( name ); if (user == null) { throw new UserManagerExc( "User '" + name + "' does not exist." ); } idUserTable.removeForKey( new Integer( user.id ) ); // remove this user from all groups DxIterator it = groupsOfUser( user ).iterator(); Group group; while ((group = (Group)it.next()) != null) { group.removeUser( user ); } } public Group groupForName( String name ) { return (Group)groupTable.elementForKey( name ); } public Group groupForID( int id ) { return (Group)idGroupTable.elementForKey( new Integer( id ) ); } public User userForName( String name ) { return (User)userTable.elementForKey( name ); } public User userForID( int id ) { return (User)idUserTable.elementForKey( new Integer( id ) ); } public DxCollection allGroups() { return groupTable; } public DxCollection allUsers() { return userTable; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -