From 00057eaca40e908bcfd9b706eb4ae51b5d9cf2aa Mon Sep 17 00:00:00 2001 From: ElgarL Date: Sat, 28 Jan 2012 11:47:39 +0000 Subject: Fixed a bug when pushing superperms in the wrong order. --- .../permissions/BukkitPermissions.java | 142 +++++++++------------ 1 file changed, 60 insertions(+), 82 deletions(-) (limited to 'EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/BukkitPermissions.java') diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/BukkitPermissions.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/BukkitPermissions.java index 3ab2dbbeb..7c063e22d 100644 --- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/BukkitPermissions.java +++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/BukkitPermissions.java @@ -20,15 +20,15 @@ import java.lang.reflect.Field; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; +import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; +import java.util.ListIterator; import java.util.Map; import java.util.Set; import org.anjocaido.groupmanager.GroupManager; -//import org.anjocaido.groupmanager.data.User; import org.anjocaido.groupmanager.dataholder.OverloadedWorldHolder; -//import org.anjocaido.groupmanager.utils.PermissionCheckResult; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -46,7 +46,6 @@ import org.bukkit.event.server.PluginEnableEvent; import org.bukkit.permissions.Permission; import org.bukkit.permissions.PermissionAttachment; import org.bukkit.permissions.PermissionAttachmentInfo; -//import org.bukkit.permissions.PermissionDefault; import org.bukkit.plugin.PluginManager; @@ -54,12 +53,12 @@ import org.bukkit.plugin.PluginManager; * * BukkitPermissions overrides to force GM reponses to Superperms * - * @author ElgarL, originally based upon PermissionsEX implementation + * @author ElgarL */ public class BukkitPermissions { protected Map attachments = new HashMap(); - protected LinkedList registeredPermissions = new LinkedList(); + protected LinkedHashMap registeredPermissions = new LinkedHashMap(); protected GroupManager plugin; protected boolean dumpAllPermissions = true; protected boolean dumpMatchedPermissions = true; @@ -112,14 +111,10 @@ public class BukkitPermissions { public void collectPermissions() { registeredPermissions.clear(); - /* - for (Plugin bukkitPlugin : Bukkit.getServer().getPluginManager().getPlugins()) { - for (Permission permission : bukkitPlugin.getDescription().getPermissions()) - registeredPermissions.push(permission); + + for (Permission perm : Bukkit.getPluginManager().getPermissions()) { + registeredPermissions.put(perm.getName().toLowerCase(), perm); } - */ - - registeredPermissions = new LinkedList(Bukkit.getPluginManager().getPermissions()); } @@ -155,73 +150,22 @@ public class BukkitPermissions { OverloadedWorldHolder worldData = plugin.getWorldsHolder().getWorldData(world); Boolean value = false; - //User user = worldData.getUser(player.getName()); - - /* - // clear permissions - for (String permission : attachment.getPermissions().keySet()) - attachment.unsetPermission(permission); - */ - - /* - * find matching permissions - * - * and base bukkit perms if we are set to allow bukkit permissions to - * override. - */ - - /* - for (Permission permission : registeredPermissions) { - - PermissionCheckResult result = worldData.getPermissionsHandler().checkFullGMPermission(user, permission.getName(), false); - - // Only check bukkit override IF we don't have the permission - // directly. - if (result.resultType == PermissionCheckResult.Type.NOTFOUND) { - PermissionDefault permDefault = permission.getDefault(); - - if ((plugin.getGMConfig().isBukkitPermsOverride()) && ((permDefault == PermissionDefault.TRUE) - || ((permDefault == PermissionDefault.NOT_OP) && !player.isOp()) - || ((permDefault == PermissionDefault.OP) && player.isOp()))) { - value = true; - } else { - value = false; - } - } else if (result.resultType == PermissionCheckResult.Type.NEGATION) { - value = false; - } else { - value = true; - } - - // Set the root permission - if ((value == true) || (result.resultType == PermissionCheckResult.Type.NEGATION)) { - attachment.setPermission(permission, value); - } - } - */ // Add all permissions for this player (GM only) // child nodes will be calculated by Bukkit. - Set playerPermArray = worldData.getPermissionsHandler().getAllPlayersPermissions(player.getName(), false); - Map newPerms = new HashMap(); - - //Set hash = new HashSet(); - //for (String permission : playerPermArray) - // hash.add(permission); - + List playerPermArray = new ArrayList(worldData.getPermissionsHandler().getAllPlayersPermissions(player.getName(), false)); + LinkedHashMap newPerms = new LinkedHashMap(); + + // Sort the perm list by parent/child, so it will push to superperms correctly. + playerPermArray = sort(playerPermArray); + for (String permission : playerPermArray) { value = (!permission.startsWith("-")); - /* - if (!attachment.getPermissions().containsKey(permission)) { - attachment.setPermission(permission, value); - } - */ + newPerms.put((value? permission : permission.substring(1)), value); } - - //player.recalculatePermissions(); - + /** * This is put in place until such a time as Bukkit pull 466 is implemented * https://github.com/Bukkit/Bukkit/pull/466 @@ -241,6 +185,41 @@ public class BukkitPermissions { e.printStackTrace(); } } + + /** + * Sort a permission node list by parent/child + * + * @param permList + * @return List sorted for priority + */ + private List sort(List permList) { + + List result = new ArrayList(); + + for (String key : permList) { + String a = key.charAt(0) == '-'? key.substring(1):key; + Map allchildren = GroupManager.BukkitPermissions.getAllChildren(a, new HashSet()); + if (allchildren != null) { + + ListIterator itr = result.listIterator(); + + while (itr.hasNext()){ + String node = (String) itr.next(); + String b = node.charAt(0) == '-'? node.substring(1):node; + + if (allchildren.containsKey(b)) { + itr.previous(); + itr.add(key); + break; + } + } + } + if (!result.contains(key)) + result.add(key); + } + + return result; + } /** @@ -254,13 +233,12 @@ public class BukkitPermissions { List perms = new ArrayList(); - for (Permission permission : registeredPermissions) { - String name = permission.getName(); - if (!perms.contains(name)) { - perms.add(name); + for (String key : registeredPermissions.keySet()) { + if (!perms.contains(key)) { + perms.add(key); if (includeChildren) { - Map children = getAllChildren(name, new HashSet()); + Map children = getAllChildren(key, new HashSet()); if (children != null) { for (String node : children.keySet()) if (!perms.contains(node)) @@ -316,13 +294,13 @@ public class BukkitPermissions { * @return Map of child permissions */ public Map getChildren(String node) { - for (Permission permission : registeredPermissions) { - if (permission.getName().equalsIgnoreCase(node)) { - return permission.getChildren(); - } - } - return null; + Permission perm = registeredPermissions.get(node.toLowerCase()); + if (perm == null) + return null; + + return perm.getChildren(); + } /** -- cgit v1.2.3 From 92f83dfe732e6e14d5b5d027b87b15798cd4ee19 Mon Sep 17 00:00:00 2001 From: ElgarL Date: Sat, 28 Jan 2012 11:59:54 +0000 Subject: fix for an iterator error if there is only one element in the array. --- .../org/anjocaido/groupmanager/permissions/BukkitPermissions.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/BukkitPermissions.java') diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/BukkitPermissions.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/BukkitPermissions.java index 7c063e22d..396b49583 100644 --- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/BukkitPermissions.java +++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/BukkitPermissions.java @@ -207,9 +207,10 @@ public class BukkitPermissions { String node = (String) itr.next(); String b = node.charAt(0) == '-'? node.substring(1):node; + // Insert the parent node before the child if (allchildren.containsKey(b)) { - itr.previous(); - itr.add(key); + itr.set(key); + itr.add(node); break; } } -- cgit v1.2.3