📄 dbforumfactory.java
字号:
return (ForumPermissions)userPermissionsCache.get(userID);
}
//Not so simple case: cache is not turned on or the user permissions
//have not been cached yet.
boolean isAnonymous = (userID == -1);
boolean isUser = !isAnonymous;
ForumPermissions finalPermissions = ForumPermissions.none();
//Step 1 - Get permissions for the User. This includes anonymous
//perms, "special user" perms, and the specific perms for the user.
if (isUser) {
ForumPermissions userPermissions = getUserPermissions(userID, -1);
//Combine permissions
finalPermissions = new ForumPermissions(finalPermissions, userPermissions);
}
//Add in anonymous perms.
ForumPermissions anonyPermissions;
if (cacheEnabled && userPermissionsCache.containsKey(-1)) {
anonyPermissions = (ForumPermissions)userPermissionsCache.get(-1);
}
//Otherwise, do our own lookup.
else {
anonyPermissions = getUserPermissions(-1, -1);
}
//Combine permissions
finalPermissions = new ForumPermissions(finalPermissions, anonyPermissions);
//If they are a valid user, figure out "any user" permissions.
if (isUser) {
ForumPermissions specialUserPermissions;
//Check for cache
if (cacheEnabled && userPermissionsCache.containsKey(0)) {
specialUserPermissions = (ForumPermissions)userPermissionsCache.get(0);
}
//Otherwise, do our own lookup.
else {
specialUserPermissions = getUserPermissions(0, -1);
//Add to cache so it will be there next time.
if (cacheEnabled) {
userPermissionsCache.add(0, specialUserPermissions);
}
}
//Combine permissions
finalPermissions = new ForumPermissions(finalPermissions, specialUserPermissions);
}
//Step 2 -- get Permissions for all groups the user is in.
int [] groups = ((DbProfileManager)getProfileManager()).getUserGroups(userID);
for (int i=0; i<groups.length; i++) {
ForumPermissions groupPermissions = getGroupPermissions(groups[i], -1);
finalPermissions = new ForumPermissions(finalPermissions, groupPermissions);
}
//Finally, add user to cache so it will be there next time.
if (cacheEnabled) {
userPermissionsCache.add(userID, finalPermissions);
}
return finalPermissions;
}
public boolean hasPermission(int type) {
return true;
}
//-- METHODS BELOW ARE NOT PART OF THE FORUMFACTORY INTERFACE --//
/**
* Returns true if caching is enabled.
*/
public boolean isCacheEnabled() {
return cacheEnabled;
}
/**
* Sets whether cache is enabled.
*/
public void setCacheEnabled(boolean enabled) {
cacheEnabled = enabled;
//If disabling cache, clear out all caches since we'll no
//long need them.
if (enabled == false) {
forumCache.clear();
threadCache.clear();
messageCache.clear();
userPermissionsCache.clear();
//Now, iterate through all forums and clear their caches.
Iterator iter = forums();
while (iter.hasNext()) {
DbForum forum = (DbForum)iter.next();
forum.userPermissionsCache.clear();
}
}
}
/**
* Returns a thread specified by its id. Will return null
* if the thread is not in the forum. If cache is turned
* on, it will use it.
*/
public DbForumThread getThread(int threadID, DbForum forum) throws
ForumThreadNotFoundException
{
//If cache is not enabled, do a new lookup of object
if (!cacheEnabled) {
return new DbForumThread(threadID, forum, this);
}
//Cache is enabled.
if(!threadCache.containsKey(threadID)) {
DbForumThread thread = new DbForumThread(threadID, forum, this);
threadCache.add(threadID, thread);
return thread;
}
else {
return (DbForumThread)threadCache.get(threadID);
}
}
/**
* Returns a message from the thread based on its id. If cache is turned
* on, it will use it.
*
* @param messageID the ID of the message to get from the thread.
*/
protected DbForumMessage getMessage(int messageID)
throws ForumMessageNotFoundException
{
//If cache is not enabled, do a new lookup of object
if (!cacheEnabled) {
return new DbForumMessage(messageID, this);
}
//Cache is enabled.
if(!messageCache.containsKey(messageID)) {
DbForumMessage message = new DbForumMessage(messageID, this);
messageCache.add(messageID, message);
return message;
}
else {
return (DbForumMessage)messageCache.get(messageID);
}
}
/**
* Logs events in the system. Very beginnings here....
*/
protected void log(String message, Exception e) {
System.err.println("Log event : " + message);
e.printStackTrace();
}
/**
* Returns the permissions that a particular user has for the forum.
*/
protected ForumPermissions getUserPermissions(int userID, int forumID) {
Connection con = null;
PreparedStatement pstmt = null;
//Initialize a permissions array with no permissions.
boolean [] permissions = new boolean[8];
for (int i=0; i<permissions.length; i++) {
permissions[i] = false;
}
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(GET_USER_PERMS);
pstmt.setInt(1, forumID);
pstmt.setInt(2, userID);
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
int newPerm = rs.getInt("permission");
permissions[newPerm] = true;
}
}
catch( SQLException sqle ) {
System.err.println("Error in DbForum.java:" + 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.
*/
protected ForumPermissions getGroupPermissions(int groupID, int forumID) {
Connection con = null;
PreparedStatement pstmt = null;
//Initialize a permissions array with no permissions.
boolean [] permissions = new boolean[8];
for (int i=0; i<permissions.length; i++) {
permissions[i] = false;
}
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(GET_GROUP_PERMS);
pstmt.setInt(1, forumID);
pstmt.setInt(2, groupID);
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
int newPerm = rs.getInt("permission");
permissions[newPerm] = 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);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -