dbpermissionsmanager.java

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

JAVA
849
字号
                    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;            }            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 Iterator groupsWithPermission(int permissionType) {        LongList groups = new LongList();        Connection con = null;        PreparedStatement pstmt = null;        try {            con = ConnectionManager.getConnection();            StringBuffer sql = new StringBuffer(                    "SELECT groupID FROM jiveGroupPerm WHERE ");            switch (type) {                case JiveGlobals.SYSTEM:                    sql.append("categoryID=-1 AND forumID=-1");                    break;                case JiveGlobals.FORUM_CATEGORY:                    sql.append("categoryID=? AND forumID=-1");                    break;                case JiveGlobals.FORUM:                    sql.append("categoryID=-1 AND forumID=?");                    break;            }            sql.append(" AND permission=?");            pstmt = con.prepareStatement(sql.toString());            if (type == JiveGlobals.SYSTEM) {                pstmt.setInt(1,  permissionType);            }            else {                pstmt.setLong(1, objectID);                pstmt.setInt(2, permissionType);            }            ResultSet rs = pstmt.executeQuery();            while (rs.next()) {                groups.add(rs.getLong(1));            }        }        catch( SQLException sqle ) {            sqle.printStackTrace();        }        finally {            try {  pstmt.close(); }            catch (Exception e) { e.printStackTrace(); }            try {  con.close();   }            catch (Exception e) { e.printStackTrace(); }        }        return new DatabaseObjectIterator(JiveGlobals.GROUP, groups.toArray(),                factory.getGroupManager());    }    public int groupsWithPermissionCount(int permissionType) {        int groupCount = 0;        Connection con = null;        PreparedStatement pstmt = null;        try {            con = ConnectionManager.getConnection();            StringBuffer sql = new StringBuffer(                    "SELECT count(groupID) FROM jiveGroupPerm WHERE ");            switch (type) {                case JiveGlobals.SYSTEM:                    sql.append("categoryID=-1 AND forumID=-1");                    break;                case JiveGlobals.FORUM_CATEGORY:                    sql.append("categoryID=? AND forumID=-1");                    break;                case JiveGlobals.FORUM:                    sql.append("categoryID=-1 AND forumID=?");                    break;            }            sql.append(" AND permission=?");            pstmt = con.prepareStatement(sql.toString());            if (type == JiveGlobals.SYSTEM) {                pstmt.setInt(1, permissionType);            }            else {                pstmt.setLong(1, objectID);                pstmt.setInt(2, permissionType);            }            ResultSet rs = pstmt.executeQuery();            if (rs.next()) {                groupCount = rs.getInt(1);            }        }        catch( SQLException sqle ) {            sqle.printStackTrace();        }        finally {            try {  pstmt.close(); }            catch (Exception e) { e.printStackTrace(); }            try {  con.close();   }            catch (Exception e) { e.printStackTrace(); }        }        return groupCount;    }    // OTHER METHODS //    /**     * Returns the ForumPermissions object that corresponds to the combined     * permissions that a user has for a particular forum (or their global     * permissions if the forumID is -1). This consists of:     * <ul>     *      <li> Anonymous and "all registered users" permissions.     *      <li> Global permissions.     *      <li> Permissions for groups that the user belongs to.     *      <li> Specific permissions granted to the user.     * </ul>     */    protected ForumPermissions getFinalUserPerms(int type, long objectID, long userID) {        Cache userPermsCache = factory.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(type).append(",").append(objectID).append(",").append(userID);        ForumPermissions userPerms = (ForumPermissions)userPermsCache.get(                key.toString());        if (userPerms != null) {            return userPerms;        }        // Not so simple case: cache is not turned off or the permissions        // have not been cached yet.        ForumPermissions finalPermissions = getUserPermissions(type, objectID, userID);        boolean isUser = !(userID==-1 || userID==0);        // If they are a user, add in "anonymous" permissions.        if (isUser) {            // Add in anonymous perms.            ForumPermissions anonyPermissions = (ForumPermissions)userPermsCache.get(                    type + "," + objectID + ",-1");            // Otherwise, do our own lookup.            if (anonyPermissions == null) {                anonyPermissions = getFinalUserPerms(type, objectID, -1);            }            // Combine permissions            finalPermissions = new ForumPermissions(finalPermissions, anonyPermissions);        }        // If they are a user, add in "any user" permissions.        if (isUser) {            ForumPermissions specialUserPermissions = null;            // Check for cache            specialUserPermissions = (ForumPermissions)userPermsCache.get(                    type + "," + objectID + ",0");            // Otherwise, do our own lookup.            if (specialUserPermissions == null) {                specialUserPermissions = getFinalUserPerms(type, objectID, 0);            }            // Combine permissions            finalPermissions = new ForumPermissions(finalPermissions,                    specialUserPermissions);        }        // Step 2 -- get Permissions for all groups the user is in.        if (isUser) {            try {                User user = factory.userManager.getUser(userID);                Iterator groups = factory.groupManager.userGroups(user);                while (groups.hasNext()) {                    long groupID = ((Group)groups.next()).getID();                    ForumPermissions groupPermissions = getGroupPermissions(                            type, objectID, groupID);                    finalPermissions = new ForumPermissions(finalPermissions,                            groupPermissions);                }            } catch (UserNotFoundException unfe) { }        }        // If this is a category, we need to do some special permission handling.        // We need to find a value for the SHOW_CATEGORY permission.        // This permission is set if there are any sub-categories or forums        // that the READ_FORUM permission is set for, or if there are any        // SUB_FORUMS that the SHOW_CATEGORY permission is set for.        if (type == JiveGlobals.FORUM_CATEGORY) {            // Get a handle on this category.            DbForumCategory category = null;            try {                category = factory.cacheManager.getForumCategory(objectID);            }            catch (ForumCategoryNotFoundException e) {                e.printStackTrace();            }            // First, check all of our forums.            for (Iterator i=category.forums(); i.hasNext(); ) {                Forum forum = (Forum)i.next();                ForumPermissions forumPerms = getFinalUserPerms(                        JiveGlobals.FORUM,  forum.getID(), userID);                if (forumPerms.get(ForumPermissions.READ_FORUM) ||                    forumPerms.get(ForumPermissions.FORUM_ADMIN) ||                    forumPerms.get(ForumPermissions.MODERATE_THREADS) ||                    forumPerms.get(ForumPermissions.MODERATE_MESSAGES))                {                    finalPermissions = new ForumPermissions(finalPermissions,                        SHOW_CATEGORY_PERM);                    break;                }            }            // Now, check child categories.            for (Iterator i=category.categories(); i.hasNext(); ) {                ForumCategory subCategory = (ForumCategory)i.next();                // Get permissions on category.                ForumPermissions catPerms = getFinalUserPerms(                        JiveGlobals.FORUM_CATEGORY, subCategory.getID(), userID);                if (catPerms.get(ForumPermissions.SHOW_CATEGORY)) {                    finalPermissions = new ForumPermissions(finalPermissions,                            SHOW_CATEGORY_PERM);                    break;                }            }        }        // Finally, add user to cache so it will be there next time.        userPermsCache.put(key.toString(), finalPermissions);        return finalPermissions;    }    /**     * Returns the permissions that a particular user has for the forum.     */    protected static ForumPermissions getUserPermissions(int type, long objectID,            long userID)    {        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 jiveUserPerm WHERE "            );            switch (type) {                case JiveGlobals.SYSTEM:                    sql.append("forumID IS NULL AND categoryID 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=?");            }            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) {

⌨️ 快捷键说明

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