forumpermissions.java
来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 229 行
JAVA
229 行
/** * $RCSfile: ForumPermissions.java,v $ * $Revision: 1.5 $ * $Date: 2002/04/01 03:54:35 $ * * 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;import com.jivesoftware.util.*;/** * Defines a set of permissions for objects in the forum system that * users can be granted. Forum permissions are used by the protection * proxy objects defined for each major component of the system. */public class ForumPermissions implements Cacheable { /** * Permission to read object. */ public static final int READ_FORUM = 0; /** * Permission to administer the entire sytem. */ public static final int SYSTEM_ADMIN = 1; /** * Permission to administer a particular forum. */ public static final int FORUM_ADMIN = 2; /** * Permission to administer a particular user. */ public static final int USER_ADMIN = 3; /** * Permission to administer a particular group. */ public static final int GROUP_ADMIN = 4; /** * Permission to moderate threads. */ public static final int MODERATE_THREADS = 5; /** * Permission to create a new thread. */ public static final int CREATE_THREAD = 6; /** * Permission to create a new message. */ public static final int CREATE_MESSAGE = 7; /** * Permission to moderate messages. */ public static final int MODERATE_MESSAGES = 8; /** * Permission to create attachments. */ public static final int CREATE_ATTACHMENT = 9; /** * Permission to administer a forum. */ public static final int CATEGORY_ADMIN = 10; /** * Permission to see a category. In general, this permission shouldn't * be used by end-users since it will be automatically toggled for a * category if READ_FORUM permissions are enabled on any forum below * the category. */ public static final int SHOW_CATEGORY = 11; public static final int PERMISSION_COUNT = 12; /** * Holds the actual permission values. */ //private boolean [] values = new boolean[PERMISSION_COUNT]; private int value; private static final ForumPermissions NO_PERMS = new ForumPermissions(false, false, false, false, false, false, false, false, false, false, false, false); private static final ForumPermissions FULL_PERMS = new ForumPermissions(true, true, true, true, true, true, true, true, true, true, true, true); private static final ForumPermissions READ_ONLY = new ForumPermissions(true, false, false, false, false, false, false, false, false, false, false, false); /** * Factory method to create full permissions. */ public static ForumPermissions full() { return FULL_PERMS; } /** * Factory method to create an object with no permissions. */ public static ForumPermissions none() { return NO_PERMS; } /** * Factory method to create an object with read-only permissions. */ public static ForumPermissions readOnly() { return READ_ONLY; } /** * Create a new permissions object with the specified permissions. */ public ForumPermissions(boolean readForum, boolean systemAdmin, boolean forumAdmin, boolean userAdmin, boolean groupAdmin, boolean moderateThreads, boolean createThread, boolean createMessage, boolean moderateMessages, boolean createAttachment, boolean categoryAdmin, boolean showCategory) { value = setBit(value, READ_FORUM, readForum); value = setBit(value, SYSTEM_ADMIN, systemAdmin); value = setBit(value, FORUM_ADMIN, forumAdmin); value = setBit(value, USER_ADMIN, userAdmin); value = setBit(value, GROUP_ADMIN, groupAdmin); value = setBit(value, MODERATE_THREADS, moderateThreads); value = setBit(value, CREATE_THREAD, createThread); value = setBit(value, CREATE_MESSAGE, createMessage); value = setBit(value, MODERATE_MESSAGES, moderateMessages); value = setBit(value, CREATE_ATTACHMENT, createAttachment); value = setBit(value, CATEGORY_ADMIN, categoryAdmin); value = setBit(value, SHOW_CATEGORY, showCategory); } /** * Returns a bitmask for the selected permissionType and value. * * @param permissionType the type of permisssion to get the bitmask for. * @param value true or false. * @return the bitmask associated with the params. */ public static final int setBit(int permissions, int permissionType, boolean value) { int mask = 0x1; mask = mask << permissionType; if (!value) { mask = ~mask; } if (value) { return permissions | mask; } else { return permissions & mask; } } /** * Creates a new ForumPermission object by combining two permissions * objects. The higher permission of each permission type will be used. */ public ForumPermissions(ForumPermissions perm1, ForumPermissions perm2) { value = perm1.value | perm2.value; } /** * Creates a new ForumPermissions object using an int. Each bit in the * number represents the value for a permission type. Use the getBitMask * method in order to easily extract permission data. For example, given * a permission int value, you can see if FORUM_ADMIN permissions are * enabled with: * * <ul><tt>1 == (permissions & getBitMask(FORUM_ADMIN, true)</tt></ul> * * @param permissions a permissions int. */ public ForumPermissions(int permissions) { this.value = permissions; } public String toString() { StringBuffer buf = new StringBuffer(); int copy = value; buf.append(String.valueOf(1 == (copy & 0x1))); for (int i=1; i<PERMISSION_COUNT; i++) { copy = copy >> 1; buf.append(","); buf.append(String.valueOf(1 == (copy & 0x1))); } return buf.toString(); } public int toInt() { return value; } /** * Returns true if the permission of a particular type is allowed. */ public boolean get(int type) { if (type < 0 || type >= PERMISSION_COUNT) { return false; } return 1 == (0x1 & (value >> type)); } //FROM THE CACHEABLE INTERFACE// public int getCachedSize() { // Approximate the size of the object in bytes by calculating the size // of each field. int size = 0; size += CacheSizes.sizeOfObject(); // overhead of object size += CacheSizes.sizeOfInt(); return size; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?