⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usermanager.java

📁 Java的面向对象数据库系统的源代码
💻 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-@year@ by SMB GmbH. All rights reserved.//// $Id: UserManager.java,v 1.2 2002/06/08 00:49:38 mediumnet Exp $package org.ozoneDB.core;import java.io.*;import org.ozoneDB.DxLib.*;import org.ozoneDB.util.*;/** * The UserManager holds all information about users and groups. * * @author <a href="http://www.softwarebuero.de/">SMB</a> * @version $Revision: 1.2 $Date: 2002/06/08 00:49:38 $ * @see User * @see Group */public final class UserManager extends ServerComponent {    // 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;    /**    	This is the userID of the system. The system has all rights and is comparable to root in    	UNIX systems.    */    protected final static int SYSTEM_USER_ID = -1;    /**    	The User object of the GarbageCollector.    */    protected final static User garbageCollectorUser    =	new User("garbageCollector",SYSTEM_USER_ID);	/**		Returns the User object of the GarbageCollector.	*/    protected User getGarbageCollectorUser() {    	return garbageCollectorUser;    }    public UserManager( Env _env ) {        super( _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 );        boolean isInitialized = true;        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();            isInitialized = false;        }        // 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() );        }        // add admin user and group        if (isInitialized == false) {            String adminName = System.getProperty( "user.name" );            env.logWriter.newEntry( this, "admin user: " + adminName, LogWriter.INFO );            newUser( adminName, 0 );            newGroup( "admin", 0 );            addUserToGroup( adminName, "admin" );        }    }    public void shutdown() throws Exception {        env.logWriter.newEntry( this, "shutdown...", LogWriter.INFO );        save();    }    public void save() throws Exception {        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;                    }                }            }        }        if (reader.getID()==SYSTEM_USER_ID) {            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;                    }                }            }        }        if (locker.getID()==SYSTEM_USER_ID) {            return true;        }        return false;    }    public void newGroup( String name, int id ) throws UserManagerException {        if (name == null) {            throw new UserManagerException( "username is null." );        }        Group group = new Group( name, id );        if (groupForID( id ) != null) {            throw new UserManagerException( "Group id " + id + " already exists." );        }        if (groupForName( name ) != null) {            throw new UserManagerException( "Group name '" + name + "' already exists." );        }        groupTable.addForKey( group, name );        idGroupTable.addForKey( group, new Integer( id ) );        setChanged();    }    /**     * Delete the group for the given name.     */    public void removeGroup( String name ) throws UserManagerException {        if (name == null) {            throw new UserManagerException( "username is null." );        }        Group group = groupForName( name );        if (group == null) {            throw new UserManagerException( "Group '" + name + "' does not exist." );        }        groupTable.removeForKey( group.name );        idGroupTable.removeForKey( new Integer( group.id ) );        setChanged();    }    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 UserManagerException {    	newUser( name, name, id);    }	public void newUser( String name, String passwd, int id ) throws UserManagerException {		if (name == null) {			throw new UserManagerException( "username is null." );		}		if (passwd == null) {			passwd = name;		}		User user = new User( name, passwd, id );		if (userForID( id ) != null) {			throw new UserManagerException( "User id " + id + " already exists." );		}		if (userForName( name ) != null) {			throw new UserManagerException( "User name '" + name + "' already exists." );		}		userTable.addForKey( user, user.name );		idUserTable.addForKey( user, new Integer( user.id ) );		setChanged();	}    public void addUserToGroup( String userName, String groupName ) throws UserManagerException {        if (groupName == null) {            throw new UserManagerException( "groupname is null." );        }        if (userName == null) {            throw new UserManagerException( "username is null." );        }        Group group = groupForName( groupName );        User user = userForName( userName );        if (group == null) {            throw new UserManagerException( "Group '" + groupName + "' does not exist." );        }        if (user == null) {            throw new UserManagerException( "User '" + userName + "' does not exist." );        }        if (!group.addUser( user )) {            throw new UserManagerException( "User '" + userName + "' is in this group already." );        }        setChanged();    }    public void removeUserFromGroup( String userName, String groupName ) throws UserManagerException {        if (groupName == null) {            throw new UserManagerException( "groupname is null." );        }        if (userName == null) {            throw new UserManagerException( "username is null." );        }        Group group = groupForName( groupName );        User user = userForName( userName );        if (group == null) {            throw new UserManagerException( "Group '" + groupName + "' does not exist." );        }        if (user == null) {            throw new UserManagerException( "User '" + userName + "' does not exist." );        }        if (!group.containsUser( user )) {            throw new UserManagerException( "User '" + userName + "' is not member of '" + groupName + "'." );        }        group.removeUser( user );        setChanged();    }    public void removeUser( String name ) throws UserManagerException {        if (name == null) {            throw new UserManagerException( "username is null." );        }        User user = (User)userTable.removeForKey( name );        if (user == null) {            throw new UserManagerException( "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 );        }        setChanged();    }    public Group groupForName( String name ) throws UserManagerException {        if (name == null) {            throw new UserManagerException( "username is null." );        }        return (Group)groupTable.elementForKey( name );    }    public Group groupForID( int id ) {        return (Group)idGroupTable.elementForKey( new Integer( id ) );    }    public User userForName( String name ) throws UserManagerException {        if (name == null) {            throw new UserManagerException( "username is null." );        }        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 + -