Cache userPermCache = (Cache) getCacheManager().get(DbCacheManager.USER_PERMS_CACHE, new Integer(-1));
//Simple case: if cache is turned on and the user is already cached,
//we can simply return the cached permissions.
if (userPermCache != null) {
ForumPermissions permissions = (ForumPermissions) userPermCache.get(new Integer(userID));
if (permissions != null) {
return permissions;
}
}
//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();
// check each forum for a specific permission
Iterator allForums = this.forums();
Forum forum;
ForumPermissions forumUserPermissions;
while (allForums.hasNext()) {
forum = (Forum) allForums.next();
forumUserPermissions = getUserPermissions(userID, forum.getID());
finalPermissions = new ForumPermissions(finalPermissions, forumUserPermissions);
}
//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 = null;
if (userPermCache != null) {
anonyPermissions = (ForumPermissions) userPermCache.get(new Integer(-1));
}
//Otherwise, do our own lookup.
if (anonyPermissions == null) {
anonyPermissions = getUserPermissions(-1, -1);
//Add to cache so it will be there next time.
if (userPermCache != null) {
userPermCache.add(new Integer(-1), anonyPermissions);
}
}
//Combine permissions
finalPermissions = new ForumPermissions(finalPermissions, anonyPermissions);
//If they are a valid user, figure out "any user" permissions.
if (isUser) {
ForumPermissions specialUserPermissions = null;
//Check for cache
if (userPermCache != null) {
specialUserPermissions = (ForumPermissions) userPermCache.get(new Integer(0));
}
//Otherwise, do our own lookup.
if (specialUserPermissions == null) {
specialUserPermissions = getUserPermissions(0, -1);
//Add to cache so it will be there next time.
if (userPermCache != null) {
userPermCache.add(new Integer(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 (isUser && userPermCache != null) {
userPermCache.add(new Integer(userID), finalPermissions);