dbpermissionsmanager.java

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

JAVA
849 行
字号
                    pstmt.setLong(1, userID);                    pstmt.setInt(2, userType);                }                else {                    pstmt.setInt(1, userType);                }            }            else {                pstmt.setLong(1, objectID);                if (userType == JiveGlobals.USER) {                    pstmt.setLong(2, userID);                    pstmt.setInt(3, userType);                }                else {                    pstmt.setInt(2, userType);                }            }            ResultSet rs = pstmt.executeQuery();            while(rs.next()) {                permissions = ForumPermissions.setBit(permissions, rs.getInt(1), true);            }        }        catch( SQLException sqle ) {            sqle.printStackTrace();        }        finally {            try {  pstmt.close(); }            catch (Exception e) { e.printStackTrace(); }            try {  con.close();   }            catch (Exception e) { e.printStackTrace(); }        }        return new ForumPermissions(permissions);    }    /**     * Returns the permissions that a particular group has for the system,     * category or forum. Valid types are JiveGlobals.SYSTEM,     * JiveGlobals.FORUM_CATEGORY and JiveGlobals.FORUM.     *     * @param type the type of the permission context.     * @param objectID the value corresponding to the type.     * @param groupID the group to look up the permission on.     */    protected static ForumPermissions getGroupPermissions(int type, long objectID,            long groupID)    {        Cache permsCache = DbForumFactory.getInstance().cacheManager.userPermsCache;        // Simple case: if cache is turned on and the perm is already cached,        // we can simply return the cached permissions.        StringBuffer key = new StringBuffer();        key.append("GROUP,").append(type).append(",").append(objectID).append(",").append(groupID);        ForumPermissions groupPerms = (ForumPermissions)permsCache.get(key.toString());        if (groupPerms != null) {            return groupPerms;        }        Connection con = null;        PreparedStatement pstmt = null;        // Initialize a permissions array with no permissions.        int permissions = 0x0;        try {            con = ConnectionManager.getConnection();            StringBuffer sql = new StringBuffer(                "SELECT permission FROM jiveGroupPerm WHERE "            );            switch (type) {                case JiveGlobals.SYSTEM:                    sql.append("forumID=-1 AND categoryID=-1 AND ");                    break;                case JiveGlobals.FORUM_CATEGORY:                    sql.append("categoryID=? AND forumID=-1 AND ");                    break;                case JiveGlobals.FORUM:                    sql.append("categoryID=-1 AND forumID=? AND ");                    break;            }            sql.append("groupID=?");            pstmt = con.prepareStatement(sql.toString());            if (type == JiveGlobals.SYSTEM) {                pstmt.setLong(1, groupID);            }            else {                pstmt.setLong(1, objectID);                pstmt.setLong(2, groupID);            }            ResultSet rs = pstmt.executeQuery();            while(rs.next()) {                permissions = ForumPermissions.setBit(permissions, rs.getInt(1), true);            }            groupPerms = new ForumPermissions(permissions);        }        catch( SQLException sqle ) {            sqle.printStackTrace();        }        finally {            try {  pstmt.close(); }            catch (Exception e) { e.printStackTrace(); }            try {  con.close();   }            catch (Exception e) { e.printStackTrace(); }        }        // Add the permission to cache.        permsCache.put(key.toString(), groupPerms);        return groupPerms;    }    private void addUserPermission(long userID, int permissionType) {        Connection con = null;        PreparedStatement pstmt = null;        try {            con = ConnectionManager.getConnection();            pstmt = con.prepareStatement(ADD_USER_PERM);            // Set the categoryID and forumID            switch (type) {                case JiveGlobals.SYSTEM:                    pstmt.setNull(1, Types.NUMERIC);                    pstmt.setNull(2, Types.NUMERIC);                    break;                case JiveGlobals.FORUM_CATEGORY:                    pstmt.setLong(1, objectID);                    pstmt.setNull(2, Types.NUMERIC);                    break;                case JiveGlobals.FORUM:                    pstmt.setNull(1, Types.NUMERIC);                    pstmt.setLong(2, objectID);                    break;            }            // Set the userID and type            if (userID == -1) {                pstmt.setNull(3, Types.NUMERIC);                pstmt.setInt(4, JiveGlobals.ANONYMOUS);            }            else if (userID == 0) {                pstmt.setNull(3, Types.NUMERIC);                pstmt.setInt(4, JiveGlobals.REGISTERED_USERS);            }            else {                pstmt.setLong(3, userID);                pstmt.setInt(4, JiveGlobals.USER);            }            // Set the perm type            pstmt.setInt(5, permissionType);            pstmt.execute();            // Now, expire cache.            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(); }        }    }    private void removeUserPermission(long userID, int permissionType) {        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("categoryID IS NULL AND forumID IS NULL ");                    break;                case JiveGlobals.FORUM_CATEGORY:                    sql.append("categoryID=").append(objectID).append(                            " AND forumID IS NULL ");                    break;                case JiveGlobals.FORUM:                    sql.append("categoryID IS NULL AND forumID=").append(                            objectID).append(" ");                    break;            }            if (userID == -1) {                sql.append("AND userID IS NULL AND userType=");                sql.append(JiveGlobals.ANONYMOUS).append(" ");            }            else if (userID == 0) {                sql.append("AND userID IS NULL AND userType=");                sql.append(JiveGlobals.REGISTERED_USERS).append(" ");            }            else {                sql.append("AND userID=").append(userID).append(" ");            }            sql.append("AND permission=").append(permissionType);            stmt.execute(sql.toString());            // Now, expire cache.            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(); }        }    }    private boolean userHasPermission(long userID, int permissionType) {        boolean hasPermission = false;        Connection con = null;        PreparedStatement pstmt = null;        try {            con = ConnectionManager.getConnection();            StringBuffer sql = new StringBuffer(                "SELECT userID FROM jiveUserPerm WHERE "            );            switch (type) {                case JiveGlobals.SYSTEM:                    sql.append("categoryID IS NULL AND forumID IS NULL AND ");                    break;                case JiveGlobals.FORUM_CATEGORY:                    sql.append("categoryID=? AND forumID IS NULL AND ");                    break;                case JiveGlobals.FORUM:                    sql.append("categoryID IS NULL AND forumID=? AND ");                    break;            }            if (userID == -1 || userID == 0) {                sql.append("userID IS NULL and userType=? ");            }            else {                sql.append("userID=? AND userType=? ");            }            sql.append("AND permission=?");            pstmt = con.prepareStatement(sql.toString());            // Determine the user type.            int userType = JiveGlobals.USER;            if (userID == -1) {                userType = JiveGlobals.ANONYMOUS;            }            else if (userID == 0) {                userType = JiveGlobals.REGISTERED_USERS;            }            // Now, set variables in pstmt.            if (type == JiveGlobals.SYSTEM) {                if (userType == JiveGlobals.USER) {                    pstmt.setLong(1, userID);                    pstmt.setInt(2, userType);                    pstmt.setInt(3, permissionType);                }                else {                    pstmt.setInt(1, userType);                    pstmt.setInt(2, permissionType);                }            }            else {                pstmt.setLong(1, objectID);                if (userType == JiveGlobals.USER) {                    pstmt.setLong(2, userID);                    pstmt.setInt(3, userType);                    pstmt.setInt(4, permissionType);                }                else {                    pstmt.setInt(2, userType);                    pstmt.setInt(3, permissionType);                }            }            ResultSet rs = pstmt.executeQuery();            if (rs.next()) {                hasPermission = true;            }        }        catch( SQLException sqle ) {            sqle.printStackTrace();        }        finally {            try {  pstmt.close(); }            catch (Exception e) { e.printStackTrace(); }            try {  con.close();   }            catch (Exception e) { e.printStackTrace(); }        }        return hasPermission;    }}

⌨️ 快捷键说明

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