dbpermissionsmanager.java

来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 849 行 · 第 1/3 页

JAVA
849
字号
/** * $RCSfile: DbPermissionsManager.java,v $ * $Revision: 1.13 $ * $Date: 2002/06/26 12:02:44 $ * * Copyright (C) 1999-2001 CoolServlets, Inc. All rights reserved. * * This software is the proprietary information of CoolServlets, Inc. * Use is subject to license terms. */package com.jivesoftware.forum.database;import com.jivesoftware.forum.*;import com.jivesoftware.util.*;import java.util.*;import java.sql.*;/** * Database implementation of the PermissionsManager interface. */public class DbPermissionsManager implements PermissionsManager {    /** DATABASE QUERIES **/    private static final String ADD_USER_PERM =        "INSERT INTO jiveUserPerm(categoryID,forumID,userID,userType,permission) VALUES(?,?,?,?,?)";    private static final String ADD_GROUP_PERM =        "INSERT INTO jiveGroupPerm(categoryID,forumID,groupID,permission) VALUES(?,?,?,?)";    private int type;    private long objectID;    private DbForumFactory factory;    private static final ForumPermissions SHOW_CATEGORY_PERM = new ForumPermissions(            false,false,false,false,false,false,false,false,false,false,false,true);    /**     * Constructs a new DbPermissionsManager. A permissions manager applies to     * one of three contexts (which corresponds The forumID indicates what forum     * the permissions management will be for. If the forumID is -1, that means     * system permissions will be being used.     */    public DbPermissionsManager(int type, long objectID) {        this.type = type;        this.objectID = objectID;        this.factory = DbForumFactory.getInstance();    }    // FROM THE PERMISSIONSMANAGER INTERFACE //    public void addUserPermission(User user, int permissionType) {        if (!userHasPermission(user, permissionType)) {            addUserPermission(user.getID(), permissionType);        }    }    public void addAnonymousUserPermission(int permissionType) {        if (!anonymousUserHasPermission(permissionType)) {            addUserPermission(-1, permissionType);        }    }    public void addRegisteredUserPermission(int permissionType) {        if (!registeredUserHasPermission(permissionType)) {            addUserPermission(0, permissionType);        }    }    public void removeUserPermission(User user, int permissionType) {        removeUserPermission(user.getID(), permissionType);    }    public void removeAllUserPermissions() {        Connection con = null;        Statement stmt = null;        try {            con = ConnectionManager.getConnection();            stmt = con.createStatement();            StringBuffer sql = new StringBuffer("DELETE FROM jiveUserPerm WHERE ");            switch (type) {                case JiveGlobals.SYSTEM:                    sql.append("forumID IS NULL AND categoryID IS NULL");                    break;                case JiveGlobals.FORUM_CATEGORY:                    sql.append("categoryID=").append(objectID);                    break;                case JiveGlobals.FORUM:                    sql.append("forumID=").append(objectID);                    break;            }            stmt.execute(sql.toString());            // Remove user permissions from cache since they've changed.            factory.cacheManager.userPermsCache.clear();        }        catch( SQLException sqle ) {            sqle.printStackTrace();        }        finally {            try {  stmt.close(); }            catch (Exception e) { e.printStackTrace(); }            try {  con.close();   }            catch (Exception e) { e.printStackTrace(); }        }    }    public void removeAnonymousUserPermission(int permissionType) {        removeUserPermission(-1, permissionType);    }    public void removeRegisteredUserPermission(int permissionType) {        removeUserPermission(0, permissionType);    }    public boolean userHasPermission(User user, int permissionType) {        return userHasPermission(user.getID(), permissionType);    }    public boolean anonymousUserHasPermission(int permissionType) {        return userHasPermission(-1, permissionType);    }    public boolean registeredUserHasPermission(int permissionType) {        return userHasPermission(0, permissionType);    }    public Iterator usersWithPermission(int permissionType) {        LongList users = new LongList();        Connection con = null;        Statement stmt = null;        try {            con = ConnectionManager.getConnection();            stmt = con.createStatement();            StringBuffer query = new StringBuffer(83);            query.append("SELECT DISTINCT userID FROM jiveUserPerm WHERE ");            switch (type) {                case JiveGlobals.SYSTEM:                    query.append("forumID IS NULL AND categoryID IS NULL");                    break;                case JiveGlobals.FORUM_CATEGORY:                    query.append("categoryID=").append(objectID);                    break;                case JiveGlobals.FORUM:                    query.append("forumID=").append(objectID);                    break;            }            query.append(" AND permission=").append(permissionType);            ResultSet rs = stmt.executeQuery(query.toString());            while (rs.next()) {                users.add(rs.getInt(1));            }        }        catch( SQLException sqle ) {            sqle.printStackTrace();        }        finally {            try {  stmt.close(); }            catch (Exception e) { e.printStackTrace(); }            try {  con.close();   }            catch (Exception e) { e.printStackTrace(); }        }        return new DatabaseObjectIterator(JiveGlobals.USER, users.toArray(),                factory.getUserManager());    }    public int usersWithPermissionCount(int permissionType) {        int userCount = 0;        Connection con = null;        Statement stmt = null;        try {            con = ConnectionManager.getConnection();            stmt = con.createStatement();            StringBuffer query = new StringBuffer(83);            query.append("SELECT count(userID) FROM jiveUserPerm WHERE ");            switch (type) {                case JiveGlobals.SYSTEM:                    query.append("forumID IS NULL AND categoryID IS NULL");                    break;                case JiveGlobals.FORUM_CATEGORY:                    query.append("categoryID=").append(objectID);                    break;                case JiveGlobals.FORUM:                    query.append("forumID=").append(objectID);                    break;            }            query.append(" AND permission=").append(permissionType);            ResultSet rs = stmt.executeQuery(query.toString());            if (rs.next()) {                userCount = rs.getInt(1);            }        }        catch( SQLException sqle ) {            sqle.printStackTrace();        }        finally {            try {  stmt.close(); }            catch (Exception e) { e.printStackTrace(); }            try {  con.close();   }            catch (Exception e) { e.printStackTrace(); }        }        return userCount;    }    public void addGroupPermission(Group group, int permissionType) {        Connection con = null;        PreparedStatement pstmt = null;        try {            con = ConnectionManager.getConnection();            pstmt = con.prepareStatement(ADD_GROUP_PERM);            switch (type) {                case JiveGlobals.SYSTEM:                    pstmt.setLong(1, -1);                    pstmt.setLong(2, -1);                    break;                case JiveGlobals.FORUM_CATEGORY:                    pstmt.setLong(1, objectID);                    pstmt.setLong(2, -1);                    break;                case JiveGlobals.FORUM:                    pstmt.setLong(1, -1);                    pstmt.setLong(2, objectID);                    break;            }            pstmt.setLong(3,group.getID());            pstmt.setInt(4,permissionType);            pstmt.execute();            // Remove user permissions from cache since they've changed.            factory.cacheManager.userPermsCache.clear();        }        catch( SQLException sqle ) {            sqle.printStackTrace();        }        finally {            try {  pstmt.close(); }            catch (Exception e) { e.printStackTrace(); }            try {  con.close();   }            catch (Exception e) { e.printStackTrace(); }        }    }    public void removeGroupPermission(Group group, int permissionType) {        Connection con = null;        Statement stmt = null;        try {            con = ConnectionManager.getConnection();            stmt = con.createStatement();            StringBuffer sql = new StringBuffer("DELETE FROM jiveGroupPerm WHERE ");            switch (type) {                case JiveGlobals.SYSTEM:                    sql.append("categoryID=-1 AND forumID=-1");                    break;                case JiveGlobals.FORUM_CATEGORY:                    sql.append("categoryID=").append(objectID).append(" AND forumID=-1");                    break;                case JiveGlobals.FORUM:                    sql.append("categoryID=-1 AND forumID=").append(objectID);                    break;            }            sql.append(" AND groupID=").append(group.getID());            sql.append(" AND permission=").append(permissionType);            stmt.execute(sql.toString());            // Remove user permissions from cache since they've changed.            factory.cacheManager.userPermsCache.clear();        }        catch( SQLException sqle ) {            sqle.printStackTrace();        }        finally {            try {  stmt.close(); }            catch (Exception e) { e.printStackTrace(); }            try {  con.close();   }            catch (Exception e) { e.printStackTrace(); }        }    }    public void removeAllGroupPermissions() {        Connection con = null;        Statement stmt = null;        try {            con = ConnectionManager.getConnection();            stmt = con.createStatement();            StringBuffer sql = new StringBuffer("DELETE FROM jiveGroupPerm WHERE ");            switch (type) {                case JiveGlobals.SYSTEM:

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?