Cache userPermCache = (Cache) factory.getCacheManager().get(DbCacheManager.USER_PERMS_CACHE, new Integer(id));
//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();
//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 = factory.getUserPermissions(userID, id);
//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 = factory.getUserPermissions(-1, id);
//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 = factory.getUserPermissions(0, id);
//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) factory.getProfileManager()).getUserGroups(userID);
for (int i = 0; i < groups.length; i++) {
ForumPermissions groupPermissions = factory.getGroupPermissions(groups[i], id);
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);