From 9c052b5178564969f1040626376963ae85a9d057 Mon Sep 17 00:00:00 2001 From: ElgarL Date: Wed, 18 Jan 2012 16:39:12 +0000 Subject: Better optimize assembling of a players permissions and allow the * node to populate all registered superperms. --- .../permissions/AnjoPermissionsHandler.java | 132 ++++++++++++--------- 1 file changed, 77 insertions(+), 55 deletions(-) (limited to 'EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java') diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java index 167103796..51c6dded5 100644 --- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java +++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java @@ -5,9 +5,11 @@ package org.anjocaido.groupmanager.permissions; import java.util.ArrayList; +import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Set; import org.anjocaido.groupmanager.GroupManager; import org.anjocaido.groupmanager.data.Group; @@ -97,81 +99,101 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface { */ @Override public List getAllPlayersPermissions(String userName) { - return getAllPlayersPermissions(userName, true); + List perms = new ArrayList(); + + perms.addAll(getAllPlayersPermissions(userName, true)); + + return perms; } + /** * Returns All permissions (including inheritance and sub groups) for the * player. With or without Bukkit child nodes. * * @param userName - * @return List of all players permissions. + * @return Set of all players permissions. */ @Override - public List getAllPlayersPermissions(String userName, Boolean includeChildren) { - - List playerPermArray = new ArrayList(); + public Set getAllPlayersPermissions(String userName, Boolean includeChildren) { - for (String perm : ph.getUser(userName).getPermissionList()) { - if ((!playerPermArray.contains(perm)) && (!playerPermArray.contains("-" + perm))) { - playerPermArray.add(perm); + Set playerPermArray = new HashSet(); - if (includeChildren) { - Map children = GroupManager.BukkitPermissions.getAllChildren(perm, playerPermArray); - - if (children != null) { - for (String child : children.keySet()) { - if (children.get(child)) - if ((!playerPermArray.contains(child)) && (!playerPermArray.contains("-" + child))) { - playerPermArray.add(child); - } - } - } - } - } - } + // Add the players own permissions. + playerPermArray.addAll(populatePerms(ph.getUser(userName).getPermissionList(), includeChildren)); + + // fetch all group permissions for (String group : getGroups(userName)) { + Set groupPermArray = new HashSet(); + if (group.startsWith("g:") && GroupManager.getGlobalGroups().hasGroup(group)) { - for (String perm : GroupManager.getGlobalGroups().getGroupsPermissions(group)) { - if ((!playerPermArray.contains(perm)) && (!playerPermArray.contains("-" + perm))) { - playerPermArray.add(perm); - - if (includeChildren) { - Map children = GroupManager.BukkitPermissions.getAllChildren(perm, playerPermArray); - if (children != null) { - for (String child : children.keySet()) { - if (children.get(child)) - if ((!playerPermArray.contains(child)) && (!playerPermArray.contains("-" + child))) - playerPermArray.add(child); - } - } - } - } - } + // GlobalGroups + groupPermArray = populatePerms(GroupManager.getGlobalGroups().getGroupsPermissions(group), includeChildren); + } else { - for (String perm : ph.getGroup(group).getPermissionList()) { - if ((!playerPermArray.contains(perm)) && (!playerPermArray.contains("-" + perm))) { - playerPermArray.add(perm); - - if (includeChildren) { - Map children = GroupManager.BukkitPermissions.getAllChildren(perm, playerPermArray); - if (children != null) { - for (String child : children.keySet()) { - if (children.get(child)) - if ((!playerPermArray.contains(child)) && (!playerPermArray.contains("-" + child))) { - playerPermArray.add(child); - } - } - } - } - } - } + // World Groups + groupPermArray = populatePerms(ph.getGroup(group).getPermissionList(), includeChildren); } + + // Add all group permissions, unless negated by direct player perms. + for (String perm : groupPermArray) + if ((!playerPermArray.contains(perm)) && (!playerPermArray.contains("-" + perm))) + playerPermArray.add(perm); + } // Collections.sort(playerPermArray, // StringPermissionComparator.getInstance()); return playerPermArray; } + + private Set populatePerms (List perms, boolean includeChildren) { + + Set permArray = new HashSet(); + + for (String perm : perms) { + + // Allow * node to populate ALL perms in Bukkit. + if (perm.equalsIgnoreCase("*")) + permArray.addAll(GroupManager.BukkitPermissions.getAllRegisteredPermissions(includeChildren)); + + boolean negated = false; + if (perm.startsWith("-")) + negated = true; + + if (!permArray.contains(perm)) { + permArray.add(perm); + + if ((negated) && (permArray.contains(perm.substring(1)))) + permArray.remove(perm.substring(1)); + + if (includeChildren) { + + Map children = GroupManager.BukkitPermissions.getAllChildren((negated ? perm.substring(1) : perm), new HashSet()); + + if (children != null) { + if (negated) { + + // Remove children of negated nodes + for (String child : children.keySet()) + if (children.get(child)) + if (permArray.contains(child)) + permArray.remove(child); + + } else { + + // Add child nodes + for (String child : children.keySet()) + if (children.get(child)) + if ((!permArray.contains(child)) && (!permArray.contains("-" + child))) + permArray.add(child); + } + } + } + } + } + + return permArray; + } /** * Verify if player is in such group. It will check it's groups inheritance. -- cgit v1.2.3