📄 dbpermissionsmanager.java
字号:
* <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(long forumID, long userID) {
UserPermissionsCache userPermsCache = factory.cacheManager.userPermsCache;
// Simple case: if cache is turned on and the user is already cached,
// we can simply return the cached permissions.
ForumPermissions userPerms = userPermsCache.get(forumID, userID);
if (userPerms != null) {
return userPerms;
}
// 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(forumID, userID);
// Combine permissions
finalPermissions = new ForumPermissions(finalPermissions, userPermissions);
}
// Add in anonymous perms.
ForumPermissions anonyPermissions = userPermsCache.get(forumID, -1);
// Otherwise, do our own lookup.
if (anonyPermissions == null) {
anonyPermissions = getUserPermissions(forumID, -1);
// Add to cache so it will be there next time.
userPermsCache.add(forumID, -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
specialUserPermissions = userPermsCache.get(forumID, 0);
// Otherwise, do our own lookup.
if (specialUserPermissions == null) {
specialUserPermissions = getUserPermissions(forumID, 0);
// Add to cache so it will be there next time.
userPermsCache.add(forumID, 0, specialUserPermissions);
}
// Combine permissions
finalPermissions = new ForumPermissions(finalPermissions, specialUserPermissions);
}
// Step 2 -- get Permissions for all groups the user is in.
long [] groups = factory.groupManager.getUserGroups(userID);
for (int i=0; i<groups.length; i++) {
ForumPermissions groupPermissions = getGroupPermissions(forumID, groups[i]);
finalPermissions = new ForumPermissions(finalPermissions, groupPermissions);
}
// Finally, add user to cache so it will be there next time.
if (isUser) {
userPermsCache.add(forumID, userID, finalPermissions);
}
return finalPermissions;
}
/**
* Returns the permissions that a particular user has for the forum.
*/
protected static ForumPermissions getUserPermissions(long forumID, long userID) {
Connection con = null;
Statement stmt = null;
//Initialize a permissions array with no permissions.
boolean [] permissions = new boolean[9];
for (int i=0; i<permissions.length; i++) {
permissions[i] = false;
}
try {
con = ConnectionManager.getConnection();
stmt = con.createStatement();
StringBuffer sql = new StringBuffer(
"SELECT DISTINCT permission FROM jiveUserPerm WHERE "
);
if (forumID == -1) {
sql.append("forumID IS NULL AND ");
}
else {
sql.append("forumID=").append(forumID).append(" AND ");
}
if (userID == -1) {
sql.append("userID IS NULL and userType=");
sql.append(JiveGlobals.ANONYMOUS);
}
else if (userID == 0) {
sql.append("userID IS NULL AND userType=");
sql.append(JiveGlobals.REGISTERED_USERS);
}
else {
sql.append("userID=").append(userID).append(" AND userType=");
sql.append(JiveGlobals.USER);
}
ResultSet rs = stmt.executeQuery(sql.toString());
while(rs.next()) {
int newPerm = rs.getInt(1);
permissions[newPerm] = true;
}
}
catch( SQLException sqle ) {
sqle.printStackTrace();
}
finally {
try { stmt.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 forum.
*/
protected static ForumPermissions getGroupPermissions(long forumID, long groupID) {
Connection con = null;
Statement stmt = null;
//Initialize a permissions array with no permissions.
boolean [] permissions = new boolean[9];
for (int i=0; i<permissions.length; i++) {
permissions[i] = false;
}
try {
con = ConnectionManager.getConnection();
stmt = con.createStatement();
StringBuffer sql = new StringBuffer(
"SELECT DISTINCT permission FROM jiveGroupPerm WHERE "
);
if (forumID == -1) {
sql.append("forumID IS NULL AND ");
}
else {
sql.append("forumID=").append(forumID).append(" AND ");
}
sql.append("groupID=").append(groupID);
ResultSet rs = stmt.executeQuery(sql.toString());
while(rs.next()) {
int newPerm = rs.getInt(1);
permissions[newPerm] = true;
}
}
catch( SQLException sqle ) {
sqle.printStackTrace();
}
finally {
try { stmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
return new ForumPermissions(permissions);
}
private void addUserPermission(long userID, int permissionType) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getConnection();
pstmt = con.prepareStatement(ADD_USER_PERM);
//Set the forum ID
if (forumID == -1) {
pstmt.setNull(1, Types.NUMERIC);
}
else {
pstmt.setLong(1, forumID);
}
//Set the userID and type
if (userID == -1) {
pstmt.setNull(2, Types.NUMERIC);
pstmt.setInt(3, JiveGlobals.ANONYMOUS);
}
else if (userID == 0) {
pstmt.setNull(2, Types.NUMERIC);
pstmt.setInt(3, JiveGlobals.REGISTERED_USERS);
}
else {
pstmt.setLong(2, userID);
pstmt.setInt(3, JiveGlobals.USER);
}
//Set the perm type
pstmt.setInt(4, permissionType);
pstmt.execute();
//Now, expire cache. If forum ID is global, we need to do a global
//expriation
if (forumID == -1) {
if (userID == -1 || userID == 0) {
factory.cacheManager.userPermsCache.clear();
}
else {
factory.cacheManager.userPermsCache.removeUserPerm(userID);
}
}
//Otherwise, it's just per forum.
else {
if (userID == -1 || userID == 0) {
LongCache permsCache = factory.cacheManager.userPermsCache.get(forumID);
//If cache is turned on, the cache shouldn't be null.
if (permsCache != null) {
permsCache.clear();
}
}
else {
factory.cacheManager.userPermsCache.removeUserPerm(userID, forumID);
}
}
}
catch( SQLException sqle ) {
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
private void removeUserPermission(long userID, int permissionType) {
Connection con = null;
Statement stmt = null;
try {
con = ConnectionManager.getConnection();
stmt = con.createStatement();
StringBuffer sql = new StringBuffer("DELETE FROM jiveUserPerm WHERE ");
if (forumID == -1) {
sql.append("forumID IS NULL ");
}
else {
sql.append("forumID=").append(forumID).append(" ");
}
if (userID == -1) {
sql.append("AND userID IS NULL AND userType=");
sql.append(JiveGlobals.ANONYMOUS).append(" ");
}
else if (userID == 0) {
sql.append("AND userID IS NULL AND userType=");
sql.append(JiveGlobals.REGISTERED_USERS).append(" ");
}
else {
sql.append("AND userID=").append(userID).append(" ");
}
sql.append("AND permission=").append(permissionType);
stmt.execute(sql.toString());
//Now, expire cache. If forum ID is global, we need to do a global
//expriation.
UserPermissionsCache cache = factory.cacheManager.userPermsCache;
if (cache.isEnabled()) {
if (forumID == -1) {
if (userID == -1 || userID == 0) {
cache.clear();
}
else {
cache.removeUserPerm(userID);
}
}
//Otherwise, it's just per forum.
else {
if (userID == -1 || userID == 0) {
cache.get(forumID).clear();
}
else {
cache.removeUserPerm(userID, forumID);
}
}
}
}
catch( SQLException sqle ) {
sqle.printStackTrace();
}
finally {
try { stmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
private boolean userHasPermission(long userID, int permissionType) {
boolean hasPermission = false;
Connection con = null;
Statement stmt = null;
try {
con = ConnectionManager.getConnection();
stmt = con.createStatement();
StringBuffer sql = new StringBuffer(100);
sql.append("SELECT userID FROM jiveUserPerm WHERE ");
if (forumID == -1) {
sql.append("forumID IS NULL ");
}
else {
sql.append("forumID=").append(forumID).append(" ");
}
if (userID == -1) {
sql.append("AND userID IS NULL AND userType=");
sql.append(JiveGlobals.ANONYMOUS).append(" ");
}
else if (userID == 0) {
sql.append("AND userID IS NULL and userType=");
sql.append(JiveGlobals.REGISTERED_USERS).append(" ");
}
else {
sql.append("AND userID=").append(userID).append(" ");
}
sql.append("AND permission=").append(permissionType);
ResultSet rs = stmt.executeQuery(sql.toString());
if (rs.next()) {
hasPermission = true;
}
}
catch( SQLException sqle ) {
sqle.printStackTrace();
}
finally {
try { stmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
return hasPermission;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -