From 94983c3ca75317a2c6c7e0c8ca88b4f2a2ad0f4c Mon Sep 17 00:00:00 2001 From: ElgarL Date: Thu, 24 Nov 2011 13:26:30 +0000 Subject: Optimize sorting to speedup permission tests. --- EssentialsGroupManager/src/Changelog.txt | 3 ++- .../src/org/anjocaido/groupmanager/data/DataUnit.java | 12 ++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) (limited to 'EssentialsGroupManager/src') diff --git a/EssentialsGroupManager/src/Changelog.txt b/EssentialsGroupManager/src/Changelog.txt index 580d8bc88..d12f96cda 100644 --- a/EssentialsGroupManager/src/Changelog.txt +++ b/EssentialsGroupManager/src/Changelog.txt @@ -75,4 +75,5 @@ v 1.5: - Fixed a crash on reload due to bukkit not unloading plugins before reloading. v 1.6: - Prevent Group.equals tests throwing a NullPointerException for GlobalGroups. - - Stop throwing errors on an empty users file. \ No newline at end of file + - Stop throwing errors on an empty users file. + - Optimize sorting to speedup permission tests. \ No newline at end of file diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/DataUnit.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/DataUnit.java index 924da3616..25546a0ea 100644 --- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/DataUnit.java +++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/DataUnit.java @@ -18,7 +18,7 @@ public abstract class DataUnit { private WorldDataHolder dataSource; private String name; - private boolean changed; + private boolean changed, sorted = false; private ArrayList permissions = new ArrayList(); public DataUnit(WorldDataHolder dataSource, String name) { @@ -91,6 +91,7 @@ public abstract class DataUnit { // for(StackTraceElement st: Thread.currentThread().getStackTrace()){ // GroupManager.logger.finest(st.toString()); // } + sorted = false; changed = true; } @@ -135,8 +136,15 @@ public abstract class DataUnit { public ArrayList getPermissionList() { return new ArrayList(permissions); } + + public boolean isSorted() { + return this.sorted; + } public void sortPermissions() { - Collections.sort(permissions, StringPermissionComparator.getInstance()); + if (!isSorted()) { + Collections.sort(permissions, StringPermissionComparator.getInstance()); + sorted = true; + } } } \ No newline at end of file -- cgit v1.2.3 From dec7ea19268d0e4624c3bbb7da0f3a1b52e69c72 Mon Sep 17 00:00:00 2001 From: ElgarL Date: Thu, 24 Nov 2011 14:05:25 +0000 Subject: remove bukkit perms check from checkUserPermission as it's performed in checkFullUserPermission --- .../org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'EssentialsGroupManager/src') diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java index b858c2a6c..feec3f17f 100644 --- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java +++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java @@ -657,8 +657,6 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface { if (result.resultType.equals(PermissionCheckResult.Type.EXCEPTION) || result.resultType.equals(PermissionCheckResult.Type.FOUND)) { return true; } - if ((Bukkit.getPlayer(user.getName()) != null) && (Bukkit.getPlayer(user.getName()).hasPermission(permission))) - return true; return false; } @@ -699,6 +697,7 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface { } } + // Check Bukkit perms to support plugins which add perms via code (Heroes). if ((Bukkit.getPlayer(user.getName()) != null) && (Bukkit.getPlayer(user.getName()).hasPermission(targetPermission))) { result.resultType = PermissionCheckResult.Type.FOUND; result.owner = user; -- cgit v1.2.3 From 73a7a6a1ddbddb294254b0d271a2928509db574f Mon Sep 17 00:00:00 2001 From: ElgarL Date: Thu, 24 Nov 2011 15:28:32 +0000 Subject: Optimize comparePermissionString --- .../permissions/AnjoPermissionsHandler.java | 28 +++++++++++++++------- 1 file changed, 20 insertions(+), 8 deletions(-) (limited to 'EssentialsGroupManager/src') diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java index feec3f17f..94052bb25 100644 --- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java +++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java @@ -9,6 +9,8 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.StringTokenizer; +import java.util.logging.Level; + import org.anjocaido.groupmanager.GroupManager; import org.anjocaido.groupmanager.data.Group; import org.anjocaido.groupmanager.dataholder.WorldDataHolder; @@ -698,7 +700,8 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface { } // Check Bukkit perms to support plugins which add perms via code (Heroes). - if ((Bukkit.getPlayer(user.getName()) != null) && (Bukkit.getPlayer(user.getName()).hasPermission(targetPermission))) { + final Player player = Bukkit.getPlayer(user.getName()); + if ((player != null) && (player.hasPermission(targetPermission))) { result.resultType = PermissionCheckResult.Type.FOUND; result.owner = user; return result; @@ -967,7 +970,10 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface { if (userAcessLevel == null || fullPermissionName == null) { return false; } - GroupManager.logger.finest("COMPARING " + userAcessLevel + " WITH " + fullPermissionName); + boolean logFinest = (GroupManager.logger.getLevel() == Level.FINEST); + + if (logFinest) + GroupManager.logger.finest("COMPARING " + userAcessLevel + " WITH " + fullPermissionName); if (userAcessLevel.startsWith("+")) { userAcessLevel = userAcessLevel.substring(1); @@ -986,25 +992,31 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface { while (levelATokenizer.hasMoreTokens() && levelBTokenizer.hasMoreTokens()) { String levelA = levelATokenizer.nextToken(); String levelB = levelBTokenizer.nextToken(); - GroupManager.logger.finest("ROUND " + levelA + " AGAINST " + levelB); + if (logFinest) + GroupManager.logger.finest("ROUND " + levelA + " AGAINST " + levelB); if (levelA.contains("*")) { - GroupManager.logger.finest("WIN"); + if (logFinest) + GroupManager.logger.finest("WIN"); return true; } if (levelA.equalsIgnoreCase(levelB)) { if (!levelATokenizer.hasMoreTokens() && !levelBTokenizer.hasMoreTokens()) { - GroupManager.logger.finest("WIN"); + if (logFinest) + GroupManager.logger.finest("WIN"); return true; } - GroupManager.logger.finest("NEXT"); + if (logFinest) + GroupManager.logger.finest("NEXT"); continue; } else { - GroupManager.logger.finest("FAIL"); + if (logFinest) + GroupManager.logger.finest("FAIL"); return false; } } - GroupManager.logger.finest("FAIL"); + if (logFinest) + GroupManager.logger.finest("FAIL"); return false; } -- cgit v1.2.3 From 127681736de3e985c85acf4d7e1a61207f629033 Mon Sep 17 00:00:00 2001 From: ElgarL Date: Thu, 24 Nov 2011 15:57:21 +0000 Subject: rewrite of comparePermissionString (snowleo) --- .../permissions/AnjoPermissionsHandler.java | 80 ++++++++-------------- 1 file changed, 28 insertions(+), 52 deletions(-) (limited to 'EssentialsGroupManager/src') diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java index 94052bb25..66561a1b8 100644 --- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java +++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java @@ -966,59 +966,35 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface { * @param fullPermissionName * @return true if found a matching token. false if not. */ - public boolean comparePermissionString(String userAcessLevel, String fullPermissionName) { - if (userAcessLevel == null || fullPermissionName == null) { - return false; - } - boolean logFinest = (GroupManager.logger.getLevel() == Level.FINEST); - - if (logFinest) - GroupManager.logger.finest("COMPARING " + userAcessLevel + " WITH " + fullPermissionName); - - if (userAcessLevel.startsWith("+")) { - userAcessLevel = userAcessLevel.substring(1); - } else if (userAcessLevel.startsWith("-")) { - userAcessLevel = userAcessLevel.substring(1); - } - - if (fullPermissionName.startsWith("+")) { - fullPermissionName = fullPermissionName.substring(1); - } else if (fullPermissionName.startsWith("-")) { - fullPermissionName = fullPermissionName.substring(1); - } + public boolean comparePermissionString(String userAccessLevel, String fullPermissionName) { + int userAccessLevelLength; + if (userAccessLevel == null || fullPermissionName == null + || (userAccessLevelLength = userAccessLevel.length()) == 0 || fullPermissionName.length() == 0) { + return false; + } + + int userAccessLevelOffset; + if (userAccessLevel.charAt(0) == '+' || userAccessLevel.charAt(0) == '-') { + userAccessLevelOffset = 1; + } else { + userAccessLevelOffset = 0; + } + if ("*".regionMatches(0, userAccessLevel, userAccessLevelOffset, userAccessLevelLength - userAccessLevelOffset)) { + return true; + } + int fullPermissionNameOffset; + if (fullPermissionName.charAt(0) == '+' || fullPermissionName.charAt(0) == '-') { + fullPermissionNameOffset = 1; + } else { + fullPermissionNameOffset = 0; + } + + if (userAccessLevel.charAt(userAccessLevel.length() - 1) == '*') { + userAccessLevelLength--; + } + return userAccessLevel.regionMatches(true, userAccessLevelOffset, fullPermissionName, fullPermissionNameOffset, userAccessLevelLength - userAccessLevelOffset); + } - StringTokenizer levelATokenizer = new StringTokenizer(userAcessLevel, "."); - StringTokenizer levelBTokenizer = new StringTokenizer(fullPermissionName, "."); - while (levelATokenizer.hasMoreTokens() && levelBTokenizer.hasMoreTokens()) { - String levelA = levelATokenizer.nextToken(); - String levelB = levelBTokenizer.nextToken(); - if (logFinest) - GroupManager.logger.finest("ROUND " + levelA + " AGAINST " + levelB); - if (levelA.contains("*")) { - if (logFinest) - GroupManager.logger.finest("WIN"); - return true; - } - if (levelA.equalsIgnoreCase(levelB)) { - if (!levelATokenizer.hasMoreTokens() && !levelBTokenizer.hasMoreTokens()) { - if (logFinest) - GroupManager.logger.finest("WIN"); - return true; - } - if (logFinest) - GroupManager.logger.finest("NEXT"); - continue; - } else { - if (logFinest) - GroupManager.logger.finest("FAIL"); - return false; - } - - } - if (logFinest) - GroupManager.logger.finest("FAIL"); - return false; - } /** * Returns a list of all groups. -- cgit v1.2.3 From 70c235d4f074f24ce83240cd3033aa6ffcbfa61f Mon Sep 17 00:00:00 2001 From: ElgarL Date: Thu, 24 Nov 2011 16:00:46 +0000 Subject: remove unused imports. --- .../org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java | 2 -- 1 file changed, 2 deletions(-) (limited to 'EssentialsGroupManager/src') diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java index 66561a1b8..2b48eafa0 100644 --- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java +++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java @@ -8,8 +8,6 @@ import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.StringTokenizer; -import java.util.logging.Level; import org.anjocaido.groupmanager.GroupManager; import org.anjocaido.groupmanager.data.Group; -- cgit v1.2.3 From 5a9b573309fc36073660220c48fdfa172734e9c4 Mon Sep 17 00:00:00 2001 From: ElgarL Date: Thu, 24 Nov 2011 16:05:41 +0000 Subject: More optimization (charAt(0)) --- .../groupmanager/permissions/AnjoPermissionsHandler.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'EssentialsGroupManager/src') diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java index 2b48eafa0..b20780a54 100644 --- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java +++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java @@ -601,9 +601,9 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface { for (String access : user.getPermissionList()) { if (comparePermissionString(access, permission)) { result.accessLevel = access; - if (access.startsWith("-")) { + if (access.charAt(0) == '-') { result.resultType = PermissionCheckResult.Type.NEGATION; - } else if (access.startsWith("+")) { + } else if (access.charAt(0) == '+') { result.resultType = PermissionCheckResult.Type.EXCEPTION; } else { result.resultType = PermissionCheckResult.Type.FOUND; @@ -631,9 +631,9 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface { for (String access : group.getPermissionList()) { if (comparePermissionString(access, permission)) { result.accessLevel = access; - if (access.startsWith("-")) { + if (access.charAt(0) == '-') { result.resultType = PermissionCheckResult.Type.NEGATION; - } else if (access.startsWith("+")) { + } else if (access.charAt(0) == '+') { result.resultType = PermissionCheckResult.Type.EXCEPTION; } else { result.resultType = PermissionCheckResult.Type.FOUND; -- cgit v1.2.3 From eea381e7143aac4b2a0dd63878bce769078e3bed Mon Sep 17 00:00:00 2001 From: ElgarL Date: Thu, 24 Nov 2011 16:34:05 +0000 Subject: optimize getPermissionList --- .../src/org/anjocaido/groupmanager/data/DataUnit.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'EssentialsGroupManager/src') diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/DataUnit.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/DataUnit.java index 25546a0ea..a35b5aeee 100644 --- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/DataUnit.java +++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/DataUnit.java @@ -6,6 +6,8 @@ package org.anjocaido.groupmanager.data; import java.util.ArrayList; import java.util.Collections; +import java.util.List; + import org.anjocaido.groupmanager.GroupManager; import org.anjocaido.groupmanager.dataholder.WorldDataHolder; import org.anjocaido.groupmanager.utils.StringPermissionComparator; @@ -133,8 +135,8 @@ public abstract class DataUnit { * You can't edit the permissions using the returned ArrayList instance * @return a copy of the permission list */ - public ArrayList getPermissionList() { - return new ArrayList(permissions); + public List getPermissionList() { + return Collections.unmodifiableList(permissions); } public boolean isSorted() { -- cgit v1.2.3 From cd8ef9361bd103f380ec6d5fa11063a3ba7556e7 Mon Sep 17 00:00:00 2001 From: ElgarL Date: Thu, 24 Nov 2011 16:45:57 +0000 Subject: minimal optimizations --- .../anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'EssentialsGroupManager/src') diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java index b20780a54..9387f13ac 100644 --- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java +++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java @@ -967,15 +967,13 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface { public boolean comparePermissionString(String userAccessLevel, String fullPermissionName) { int userAccessLevelLength; if (userAccessLevel == null || fullPermissionName == null - || (userAccessLevelLength = userAccessLevel.length()) == 0 || fullPermissionName.length() == 0) { + || fullPermissionName.length() == 0 || (userAccessLevelLength = userAccessLevel.length()) == 0) { return false; } - int userAccessLevelOffset; + int userAccessLevelOffset = 0; if (userAccessLevel.charAt(0) == '+' || userAccessLevel.charAt(0) == '-') { userAccessLevelOffset = 1; - } else { - userAccessLevelOffset = 0; } if ("*".regionMatches(0, userAccessLevel, userAccessLevelOffset, userAccessLevelLength - userAccessLevelOffset)) { return true; -- cgit v1.2.3 From 24a60734885deb8128a9640d26e49e173b065a56 Mon Sep 17 00:00:00 2001 From: ElgarL Date: Fri, 25 Nov 2011 15:49:35 +0000 Subject: Fix superperms to pass all tests http://dev.bukkit.org/server-mods/superpermstest/ --- EssentialsGroupManager/src/Changelog.txt | 3 +- .../permissions/AnjoPermissionsHandler.java | 74 ++- .../permissions/BukkitPermissions.java | 522 +++++++++++---------- 3 files changed, 340 insertions(+), 259 deletions(-) (limited to 'EssentialsGroupManager/src') diff --git a/EssentialsGroupManager/src/Changelog.txt b/EssentialsGroupManager/src/Changelog.txt index d12f96cda..d942a470e 100644 --- a/EssentialsGroupManager/src/Changelog.txt +++ b/EssentialsGroupManager/src/Changelog.txt @@ -76,4 +76,5 @@ v 1.5: v 1.6: - Prevent Group.equals tests throwing a NullPointerException for GlobalGroups. - Stop throwing errors on an empty users file. - - Optimize sorting to speedup permission tests. \ No newline at end of file + - Optimize sorting to speedup permission tests. + - Fix superperms to pass all tests http://dev.bukkit.org/server-mods/superpermstest/ \ No newline at end of file diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java index 9387f13ac..acfc65232 100644 --- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java +++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java @@ -97,43 +97,59 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface { @Override public List getAllPlayersPermissions(String userName) { - List playerPermArray = new ArrayList(ph.getUser(userName).getPermissionList()); - + List playerPermArray = new ArrayList(); + + for (String perm : ph.getUser(userName).getPermissionList()) { + if ((!playerPermArray.contains(perm)) && (!playerPermArray.contains("-" + perm))) { + playerPermArray.add(perm); + + 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); + } + } + } + } + } for (String group : getGroups(userName)) { 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); - Map children = GroupManager.BukkitPermissions.getChildren(perm); + Map children = GroupManager.BukkitPermissions.getAllChildren(perm, playerPermArray); if (children != null) { for (String child : children.keySet()) { if (children.get(child)) - if ((!playerPermArray.contains(perm)) && (!playerPermArray.contains("-" + perm))) + if ((!playerPermArray.contains(child)) && (!playerPermArray.contains("-" + child))) playerPermArray.add(child); } } } } - } else { for (String perm : ph.getGroup(group).getPermissionList()) { if ((!playerPermArray.contains(perm)) && (!playerPermArray.contains("-" + perm))) { playerPermArray.add(perm); - - Map children = GroupManager.BukkitPermissions.getChildren(perm); + + Map children = GroupManager.BukkitPermissions.getAllChildren(perm, playerPermArray); if (children != null) { for (String child : children.keySet()) { if (children.get(child)) - if ((!playerPermArray.contains(perm)) && (!playerPermArray.contains("-" + perm))) + if ((!playerPermArray.contains(child)) && (!playerPermArray.contains("-" + child))) { playerPermArray.add(child); + } } } - } } } } + //Collections.sort(playerPermArray, StringPermissionComparator.getInstance()); return playerPermArray; } @@ -670,8 +686,22 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface { * @return PermissionCheckResult */ public PermissionCheckResult checkFullUserPermission(User user, String targetPermission) { + + return checkFullGMPermission(user, targetPermission, true); + } + + /** + * Check user and groups with inheritance and Bukkit if bukkit = true + * return a PermissionCheckResult. + * + * @param user + * @param targetPermission + * @param checkBukkit + * @return PermissionCheckResult + */ + public PermissionCheckResult checkFullGMPermission(User user, String targetPermission, Boolean checkBukkit) { PermissionCheckResult result = new PermissionCheckResult(); - result.askedPermission = targetPermission; + result.accessLevel = targetPermission; result.resultType = PermissionCheckResult.Type.NOTFOUND; if (user == null || targetPermission == null || targetPermission.isEmpty()) { @@ -697,12 +727,14 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface { } } - // Check Bukkit perms to support plugins which add perms via code (Heroes). - final Player player = Bukkit.getPlayer(user.getName()); - if ((player != null) && (player.hasPermission(targetPermission))) { - result.resultType = PermissionCheckResult.Type.FOUND; - result.owner = user; - return result; + if (checkBukkit == true) { + // Check Bukkit perms to support plugins which add perms via code (Heroes). + final Player player = Bukkit.getPlayer(user.getName()); + if ((player != null) && (player.hasPermission(targetPermission))) { + result.resultType = PermissionCheckResult.Type.FOUND; + result.owner = user; + return result; + } } // THEN IT RETURNS A NOT FOUND @@ -960,7 +992,7 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface { * Every '-' or '+' in the beginning is ignored. It will match only node * names. * - * @param userAcessLevel + * @param userAccessLevel * @param fullPermissionName * @return true if found a matching token. false if not. */ @@ -986,9 +1018,11 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface { } if (userAccessLevel.charAt(userAccessLevel.length() - 1) == '*') { - userAccessLevelLength--; - } - return userAccessLevel.regionMatches(true, userAccessLevelOffset, fullPermissionName, fullPermissionNameOffset, userAccessLevelLength - userAccessLevelOffset); + return userAccessLevel.regionMatches(true, userAccessLevelOffset, fullPermissionName, fullPermissionNameOffset, userAccessLevelLength - userAccessLevelOffset - 1); + } else { + return userAccessLevel.regionMatches(true, userAccessLevelOffset, fullPermissionName, fullPermissionNameOffset, + Math.max(userAccessLevelLength - userAccessLevelOffset, fullPermissionName.length() - fullPermissionNameOffset)); + } } diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/BukkitPermissions.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/BukkitPermissions.java index abfb73bfd..202e27e3a 100644 --- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/BukkitPermissions.java +++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/BukkitPermissions.java @@ -18,14 +18,14 @@ package org.anjocaido.groupmanager.permissions; import java.util.ArrayList; import java.util.HashMap; -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.User; import org.anjocaido.groupmanager.dataholder.OverloadedWorldHolder; +import org.anjocaido.groupmanager.utils.PermissionCheckResult; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -49,246 +49,292 @@ import org.bukkit.plugin.PluginManager; /** - * + * * BukkitPermissions overrides to force GM reponses to Superperms - * + * * @author ElgarL, based upon PermissionsEX implementation */ public class BukkitPermissions { - protected Map attachments = new HashMap(); - protected Set registeredPermissions = new HashSet(); - protected GroupManager plugin; - protected boolean dumpAllPermissions = true; - protected boolean dumpMatchedPermissions = true; - public boolean player_join = false; - - public BukkitPermissions(GroupManager plugin) { - this.plugin = plugin; - - this.collectPermissions(); - this.registerEvents(); - - this.updateAllPlayers(); - - GroupManager.logger.info("Superperms support enabled."); - } - - private void registerEvents() { - PluginManager manager = plugin.getServer().getPluginManager(); - - PlayerEvents playerEventListener = new PlayerEvents(); - - manager.registerEvent(Event.Type.PLAYER_JOIN, playerEventListener, Event.Priority.Lowest, plugin); - manager.registerEvent(Event.Type.PLAYER_KICK, playerEventListener, Event.Priority.Lowest, plugin); - manager.registerEvent(Event.Type.PLAYER_QUIT, playerEventListener, Event.Priority.Lowest, plugin); - - manager.registerEvent(Event.Type.PLAYER_RESPAWN, playerEventListener, Event.Priority.Lowest, plugin); - manager.registerEvent(Event.Type.PLAYER_TELEPORT, playerEventListener, Event.Priority.Lowest, plugin); - manager.registerEvent(Event.Type.PLAYER_PORTAL, playerEventListener, Event.Priority.Lowest, plugin); - - ServerListener serverListener = new BukkitEvents(); - - manager.registerEvent(Event.Type.PLUGIN_ENABLE, serverListener, Event.Priority.Normal, plugin); - manager.registerEvent(Event.Type.PLUGIN_DISABLE, serverListener, Event.Priority.Normal, plugin); - } - - public void collectPermissions() { - registeredPermissions.clear(); - for (Plugin bukkitPlugin : Bukkit.getServer().getPluginManager().getPlugins()) { - for(Permission permission : bukkitPlugin.getDescription().getPermissions()) - registeredPermissions.add(permission); - } - } - - public void updatePermissions(Player player){ - this.updatePermissions(player, null); - } - - public void updatePermissions(Player player, String world) { - if (player == null || !GroupManager.isLoaded()) { - return; - } - - if (!this.attachments.containsKey(player)) { - this.attachments.put(player, player.addAttachment(plugin)); - } - - if(world == null){ - world = player.getWorld().getName(); - } - - // All permissions registered with Bukkit for this player - PermissionAttachment attachment = this.attachments.get(player); - - OverloadedWorldHolder worldData = plugin.getWorldsHolder().getWorldData(world); - - 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. - */ - Boolean value; - for (Permission permission : registeredPermissions) { - - value = worldData.getPermissionsHandler().checkUserPermission(user, permission.getName()); - - // Only check bukkit override IF we don't have the permission directly. - if (value = false) { - PermissionDefault permDefault = permission.getDefault(); - - if ((plugin.getGMConfig().isBukkitPermsOverride()) - && ((permDefault == PermissionDefault.TRUE) - || ((permDefault == PermissionDefault.NOT_OP) && !player.isOp()) - || ((permDefault == PermissionDefault.OP) && player.isOp()))) - value = true; - } - - if (value == true){ - // Set the root permission - attachment.setPermission(permission, value); - // fetch and set all children of this permission node - Map children = permission.getChildren(); - if (children != null) { - for (String child : children.keySet()) { - if (children.get(child)) - attachment.setPermission(child, true); - } - } - - } - } - - // Add any missing permissions for this player (non bukkit plugins) - List playerPermArray = new ArrayList(worldData.getPermissionsHandler().getAllPlayersPermissions(player.getName())); - - for (String permission : playerPermArray) { - value = true; - if (permission.startsWith("-")) { - permission = permission.substring(1); // cut off - - value = false; - } - - if (!attachment.getPermissions().containsKey(permission)) { - attachment.setPermission(permission, value); - } - } - player.recalculatePermissions(); - } - - /** - * Returns a map of the child permissions as defined by the supplying plugin - * null is empty - * - * @param node - * @return Map of child permissions - */ - public Map getChildren(String node) { - for (Permission permission : registeredPermissions) { - if (permission.getName() == node) { - return permission.getChildren(); - } - } - return null; - } - - public List listPerms(Player player) { - List perms = new ArrayList(); - - /* - // All permissions registered with Bukkit for this player - PermissionAttachment attachment = this.attachments.get(player); - - // List perms for this player - perms.add("Attachment Permissions:"); - for(Map.Entry entry : attachment.getPermissions().entrySet()){ - perms.add(" " + entry.getKey() + " = " + entry.getValue()); - } - */ - - perms.add("Effective Permissions:"); - for(PermissionAttachmentInfo info : player.getEffectivePermissions()){ - if (info.getValue() == true) - perms.add(" " + info.getPermission() + " = " + info.getValue()); - } - return perms; - } - - public void updateAllPlayers() { - for (Player player : Bukkit.getServer().getOnlinePlayers()) { - updatePermissions(player); - } - } - - protected class PlayerEvents extends PlayerListener { - - @Override - public void onPlayerJoin(PlayerJoinEvent event) { - player_join = true; - Player player = event.getPlayer(); - //force GM to create the player if they are not already listed. - if (plugin.getWorldsHolder().getWorldData(player.getWorld().getName()).getUser(player.getName()) != null) { - player_join = false; - updatePermissions(event.getPlayer()); - } else - player_join = false; - } - - @Override - public void onPlayerPortal(PlayerPortalEvent event) { // will portal into another world - if(event.getTo() != null && !event.getFrom().getWorld().equals(event.getTo().getWorld())){ // only if world actually changed - updatePermissions(event.getPlayer(), event.getTo().getWorld().getName()); - } - } - - @Override - public void onPlayerRespawn(PlayerRespawnEvent event) { // can be respawned in another world - updatePermissions(event.getPlayer(), event.getRespawnLocation().getWorld().getName()); - } - - @Override - public void onPlayerTeleport(PlayerTeleportEvent event) { // can be teleported into another world - if (event.getTo() != null && !event.getFrom().getWorld().equals(event.getTo().getWorld())) { // only if world actually changed - updatePermissions(event.getPlayer(), event.getTo().getWorld().getName()); - } - } - - @Override - public void onPlayerQuit(PlayerQuitEvent event) { - if (!GroupManager.isLoaded()) - return; - - attachments.remove(event.getPlayer()); - } - - @Override - public void onPlayerKick(PlayerKickEvent event) { - attachments.remove(event.getPlayer()); - } - } - - protected class BukkitEvents extends ServerListener { - - @Override - public void onPluginEnable(PluginEnableEvent event) { - if (!GroupManager.isLoaded()) - return; - - collectPermissions(); - updateAllPlayers(); - } - - @Override - public void onPluginDisable(PluginDisableEvent event) { - //collectPermissions(); - //updateAllPlayers(); - } - } + protected Map attachments = new HashMap(); + protected LinkedList registeredPermissions = new LinkedList(); + protected GroupManager plugin; + protected boolean dumpAllPermissions = true; + protected boolean dumpMatchedPermissions = true; + public boolean player_join = false; + + public BukkitPermissions(GroupManager plugin) { + this.plugin = plugin; + + this.collectPermissions(); + this.registerEvents(); + + this.updateAllPlayers(); + + GroupManager.logger.info("Superperms support enabled."); + } + + private void registerEvents() { + PluginManager manager = plugin.getServer().getPluginManager(); + + PlayerEvents playerEventListener = new PlayerEvents(); + + manager.registerEvent(Event.Type.PLAYER_JOIN, playerEventListener, Event.Priority.Lowest, plugin); + manager.registerEvent(Event.Type.PLAYER_KICK, playerEventListener, Event.Priority.Lowest, plugin); + manager.registerEvent(Event.Type.PLAYER_QUIT, playerEventListener, Event.Priority.Lowest, plugin); + + manager.registerEvent(Event.Type.PLAYER_RESPAWN, playerEventListener, Event.Priority.Lowest, plugin); + manager.registerEvent(Event.Type.PLAYER_TELEPORT, playerEventListener, Event.Priority.Lowest, plugin); + manager.registerEvent(Event.Type.PLAYER_PORTAL, playerEventListener, Event.Priority.Lowest, plugin); + + ServerListener serverListener = new BukkitEvents(); + + manager.registerEvent(Event.Type.PLUGIN_ENABLE, serverListener, Event.Priority.Normal, plugin); + manager.registerEvent(Event.Type.PLUGIN_DISABLE, serverListener, Event.Priority.Normal, plugin); + } + + public void collectPermissions() { + registeredPermissions.clear(); + for (Plugin bukkitPlugin : Bukkit.getServer().getPluginManager().getPlugins()) { + for (Permission permission : bukkitPlugin.getDescription().getPermissions()) + registeredPermissions.push(permission); + } + } + + public void updatePermissions(Player player) { + this.updatePermissions(player, null); + } + + public void updatePermissions(Player player, String world) { + if (player == null || !GroupManager.isLoaded()) { + return; + } + + if (!this.attachments.containsKey(player)) { + this.attachments.put(player, player.addAttachment(plugin)); + } + + if (world == null) { + world = player.getWorld().getName(); + } + + // All permissions registered with Bukkit for this player + PermissionAttachment attachment = this.attachments.get(player); + + OverloadedWorldHolder worldData = plugin.getWorldsHolder().getWorldData(world); + + 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. + */ + Boolean value = false; + + 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); + } + /* + if ((value == true) || (result.resultType == PermissionCheckResult.Type.NOTFOUND)) { + // fetch and set all children of this permission node + Map children = permission.getChildren(); + if (children != null) { + for (String child : children.keySet()) { + if (children.get(child)) + attachment.setPermission(child, value); + } + } + }*/ + + } + + // Add any missing permissions for this player (non bukkit plugins and child nodes) + List playerPermArray = worldData.getPermissionsHandler().getAllPlayersPermissions(player.getName()); + + for (String permission : playerPermArray) { + value = true; + if (permission.startsWith("-")) { + permission = permission.substring(1); // cut off - + value = false; + } + + if (!attachment.getPermissions().containsKey(permission)) { + attachment.setPermission(permission, value); + } + } + player.recalculatePermissions(); + } + + /** + * Returns a map of the ALL child permissions as defined by the supplying plugin + * null is empty + * + * @param node + * @return Map of child permissions + */ + public Map getAllChildren(String node, List playerPermArray) { + + LinkedList stack = new LinkedList(); + Map alreadyVisited = new HashMap(); + stack.push(node); + alreadyVisited.put(node, true); + + while (!stack.isEmpty()) { + String now = stack.pop(); + + Map children = getChildren(now); + + if ((children != null) && (!playerPermArray.contains("-"+now))) { + for (String childName : children.keySet()) { + if (!alreadyVisited.containsKey(childName)) { + stack.push(childName); + alreadyVisited.put(childName, children.get(childName)); + } + } + } + } + alreadyVisited.remove(node); + if (!alreadyVisited.isEmpty()) return alreadyVisited; + + return null; + } + + /** + * Returns a map of the child permissions (1 node deep) as defined by the supplying plugin + * null is empty + * + * @param node + * @return Map of child permissions + */ + public Map getChildren(String node) { + for (Permission permission : registeredPermissions) { + if (permission.getName().equalsIgnoreCase(node)) { + return permission.getChildren(); + } + } + + return null; + } + + public List listPerms(Player player) { + List perms = new ArrayList(); + + /* + * // All permissions registered with Bukkit for this player + * PermissionAttachment attachment = this.attachments.get(player); + * + * // List perms for this player perms.add("Attachment Permissions:"); + * for(Map.Entry entry : + * attachment.getPermissions().entrySet()){ perms.add(" " + + * entry.getKey() + " = " + entry.getValue()); } + */ + + perms.add("Effective Permissions:"); + for (PermissionAttachmentInfo info : player.getEffectivePermissions()) { + if (info.getValue() == true) + perms.add(" " + info.getPermission() + " = " + info.getValue()); + } + return perms; + } + + public void updateAllPlayers() { + for (Player player : Bukkit.getServer().getOnlinePlayers()) { + updatePermissions(player); + } + } + + protected class PlayerEvents extends PlayerListener { + + @Override + public void onPlayerJoin(PlayerJoinEvent event) { + player_join = true; + Player player = event.getPlayer(); + // force GM to create the player if they are not already listed. + if (plugin.getWorldsHolder().getWorldData(player.getWorld().getName()).getUser(player.getName()) != null) { + player_join = false; + updatePermissions(event.getPlayer()); + } else + player_join = false; + } + + @Override + public void onPlayerPortal(PlayerPortalEvent event) { // will portal into another world + if (event.getTo() != null && !event.getFrom().getWorld().equals(event.getTo().getWorld())) { // only if world actually changed + updatePermissions(event.getPlayer(), event.getTo().getWorld().getName()); + } + } + + @Override + public void onPlayerRespawn(PlayerRespawnEvent event) { // can be respawned in another world + updatePermissions(event.getPlayer(), event.getRespawnLocation().getWorld().getName()); + } + + @Override + public void onPlayerTeleport(PlayerTeleportEvent event) { // can be teleported into another world + if (event.getTo() != null && !event.getFrom().getWorld().equals(event.getTo().getWorld())) { // only if world actually changed + updatePermissions(event.getPlayer(), event.getTo().getWorld().getName()); + } + } + + @Override + public void onPlayerQuit(PlayerQuitEvent event) { + if (!GroupManager.isLoaded()) + return; + + attachments.remove(event.getPlayer()); + } + + @Override + public void onPlayerKick(PlayerKickEvent event) { + attachments.remove(event.getPlayer()); + } + } + + protected class BukkitEvents extends ServerListener { + + @Override + public void onPluginEnable(PluginEnableEvent event) { + if (!GroupManager.isLoaded()) + return; + + collectPermissions(); + updateAllPlayers(); + } + + @Override + public void onPluginDisable(PluginDisableEvent event) { + // collectPermissions(); + // updateAllPlayers(); + } + } } \ No newline at end of file -- cgit v1.2.3 From b41c3709bae6b1ad6f2b6ce9e5494421168c8f5f Mon Sep 17 00:00:00 2001 From: ElgarL Date: Fri, 25 Nov 2011 16:01:46 +0000 Subject: Raised bukkit perms priority --- .../permissions/AnjoPermissionsHandler.java | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'EssentialsGroupManager/src') diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java index acfc65232..91c73b5c2 100644 --- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java +++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java @@ -669,7 +669,7 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface { * @return true if permission was found. false if not, or was negated. */ public boolean checkUserPermission(User user, String permission) { - PermissionCheckResult result = checkFullUserPermission(user, permission); + PermissionCheckResult result = checkFullGMPermission(user, permission, true); if (result.resultType.equals(PermissionCheckResult.Type.EXCEPTION) || result.resultType.equals(PermissionCheckResult.Type.FOUND)) { return true; } @@ -708,6 +708,16 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface { return result; } + if (checkBukkit == true) { + // Check Bukkit perms to support plugins which add perms via code (Heroes). + final Player player = Bukkit.getPlayer(user.getName()); + if ((player != null) && (player.hasPermission(targetPermission))) { + result.resultType = PermissionCheckResult.Type.FOUND; + result.owner = user; + return result; + } + } + PermissionCheckResult resultUser = checkUserOnlyPermission(user, targetPermission); if (!resultUser.resultType.equals(PermissionCheckResult.Type.NOTFOUND)) { return resultUser; @@ -727,16 +737,6 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface { } } - if (checkBukkit == true) { - // Check Bukkit perms to support plugins which add perms via code (Heroes). - final Player player = Bukkit.getPlayer(user.getName()); - if ((player != null) && (player.hasPermission(targetPermission))) { - result.resultType = PermissionCheckResult.Type.FOUND; - result.owner = user; - return result; - } - } - // THEN IT RETURNS A NOT FOUND return result; } -- cgit v1.2.3 From 0cb77d8a03abc0879bec537bd672e09050c16a9e Mon Sep 17 00:00:00 2001 From: ElgarL Date: Fri, 25 Nov 2011 16:34:34 +0000 Subject: Minor optimization for groups --- .../src/org/anjocaido/groupmanager/data/Group.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'EssentialsGroupManager/src') diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/Group.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/Group.java index c05065f5e..ba8915a18 100644 --- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/Group.java +++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/Group.java @@ -7,6 +7,8 @@ package org.anjocaido.groupmanager.data; import org.anjocaido.groupmanager.GroupManager; import org.anjocaido.groupmanager.dataholder.WorldDataHolder; import java.util.ArrayList; +import java.util.Collections; +import java.util.List; import java.util.Map; /** @@ -92,13 +94,13 @@ public class Group extends DataUnit implements Cloneable { } /** - * a COPY of inherits list + * an unmodifiable list of inherits list * You can't manage the list by here * Lol... version 0.6 had a problem because this. * @return the inherits */ - public ArrayList getInherits() { - return new ArrayList(inherits); + public List getInherits() { + return Collections.unmodifiableList(inherits); } /** -- cgit v1.2.3 From b25a8f059b4d383b21d789aefbd3e3273503c0ba Mon Sep 17 00:00:00 2001 From: ElgarL Date: Fri, 25 Nov 2011 19:33:32 +0000 Subject: Optimizations include changing the return of comparePermissionString. --- EssentialsGroupManager/src/Changelog.txt | 3 +- .../src/org/anjocaido/groupmanager/data/User.java | 415 +++++++++++---------- .../dataholder/OverloadedWorldHolder.java | 9 +- .../dataholder/worlds/WorldsHolder.java | 7 +- .../permissions/AnjoPermissionsHandler.java | 136 ++++--- 5 files changed, 296 insertions(+), 274 deletions(-) (limited to 'EssentialsGroupManager/src') diff --git a/EssentialsGroupManager/src/Changelog.txt b/EssentialsGroupManager/src/Changelog.txt index d942a470e..7b9a99bb7 100644 --- a/EssentialsGroupManager/src/Changelog.txt +++ b/EssentialsGroupManager/src/Changelog.txt @@ -77,4 +77,5 @@ v 1.6: - Prevent Group.equals tests throwing a NullPointerException for GlobalGroups. - Stop throwing errors on an empty users file. - Optimize sorting to speedup permission tests. - - Fix superperms to pass all tests http://dev.bukkit.org/server-mods/superpermstest/ \ No newline at end of file + - Fix superperms to pass all tests http://dev.bukkit.org/server-mods/superpermstest/ + - Optimizations include changing the return of comparePermissionString. \ No newline at end of file diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/User.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/User.java index 6c4cdbcf2..eea488ab2 100644 --- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/User.java +++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/User.java @@ -10,209 +10,232 @@ import java.util.ArrayList; import org.anjocaido.groupmanager.GroupManager; import org.anjocaido.groupmanager.dataholder.WorldDataHolder; import java.util.Map; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + /** - * + * * @author gabrielcouto */ public class User extends DataUnit implements Cloneable { - /** - * - */ - private String group = null; - private ArrayList subGroups = new ArrayList(); - /** - *This one holds the fields in INFO node. - * like prefix = 'c' - * or build = false - */ - private UserVariables variables = new UserVariables(this); - - /** + /** * - * @param name */ - public User(WorldDataHolder source, String name) { - super(source, name); - this.group = source.getDefaultGroup().getName(); - } + private String group = null; + private ArrayList subGroups = new ArrayList(); + /** + * This one holds the fields in INFO node. like prefix = 'c' or build = + * false + */ + private UserVariables variables = new UserVariables(this); + private transient Player bukkitPlayer = null; + + /** + * + * @param name + */ + public User(WorldDataHolder source, String name) { + super(source, name); + this.group = source.getDefaultGroup().getName(); + } + + /** + * + * @return User clone + */ + @Override + public User clone() { + User clone = new User(getDataSource(), this.getName()); + clone.group = this.group; + for (String perm : this.getPermissionList()) { + clone.addPermission(perm); + } + // clone.variables = this.variables.clone(); + // clone.flagAsChanged(); + return clone; + } + + /** + * Use this to deliver a user from one WorldDataHolder to another + * + * @param dataSource + * @return null if given dataSource already contains the same user + */ + public User clone(WorldDataHolder dataSource) { + if (dataSource.isUserDeclared(this.getName())) { + return null; + } + User clone = dataSource.createUser(this.getName()); + if (dataSource.getGroup(group) == null) { + clone.setGroup(dataSource.getDefaultGroup()); + } else { + clone.setGroup(dataSource.getGroup(this.getGroupName())); + } + for (String perm : this.getPermissionList()) { + clone.addPermission(perm); + } + // clone.variables = this.variables.clone(); + clone.flagAsChanged(); + return clone; + } + + public Group getGroup() { + Group result = getDataSource().getGroup(group); + if (result == null) { + this.setGroup(getDataSource().getDefaultGroup()); + result = getDataSource().getDefaultGroup(); + } + return result; + } + + /** + * @return the group + */ + public String getGroupName() { + Group result = getDataSource().getGroup(group); + if (result == null) { + group = getDataSource().getDefaultGroup().getName(); + } + return group; + } + + /** + * @param group + * the group to set + */ + @Deprecated + public void setGroup(String group) { + this.group = group; + flagAsChanged(); + if (GroupManager.isLoaded()) + if (GroupManager.BukkitPermissions.player_join = false) + GroupManager.BukkitPermissions.updateAllPlayers(); + } + + /** + * @param group + * the group to set + */ + public void setGroup(Group group) { + if (!this.getDataSource().groupExists(group.getName())) { + getDataSource().addGroup(group); + } + group = getDataSource().getGroup(group.getName()); + String oldGroup = this.group; + this.group = group.getName(); + flagAsChanged(); + if (GroupManager.isLoaded()) { + if (GroupManager.BukkitPermissions.player_join = false) + GroupManager.BukkitPermissions.updateAllPlayers(); + + // Do we notify of the group change? + String defaultGroupName = getDataSource().getDefaultGroup().getName(); + // if we were not in the default group + // or we were in the default group and the move is to a different + // group. + boolean notify = (!oldGroup.equalsIgnoreCase(defaultGroupName)) || ((oldGroup.equalsIgnoreCase(defaultGroupName)) && (!this.group.equalsIgnoreCase(defaultGroupName))); + + if (notify) + GroupManager.notify(this.getName(), String.format(" moved to the group %s.", group.getName())); + } + } + + public void addSubGroup(Group subGroup) { + if (this.group.equalsIgnoreCase(subGroup.getName())) { + return; + } + if (!this.getDataSource().groupExists(subGroup.getName())) { + getDataSource().addGroup(subGroup); + } + subGroup = getDataSource().getGroup(subGroup.getName()); + removeSubGroup(subGroup); + subGroups.add(subGroup.getName()); + flagAsChanged(); + if (GroupManager.isLoaded()) + if (GroupManager.BukkitPermissions.player_join = false) + GroupManager.BukkitPermissions.updateAllPlayers(); + } + + public int subGroupsSize() { + return subGroups.size(); + } + + public boolean isSubGroupsEmpty() { + return subGroups.isEmpty(); + } + + public boolean containsSubGroup(Group subGroup) { + return subGroups.contains(subGroup.getName()); + } + + public boolean removeSubGroup(Group subGroup) { + try { + if (subGroups.remove(subGroup.getName())) { + flagAsChanged(); + if (GroupManager.isLoaded()) + if (GroupManager.BukkitPermissions.player_join = false) + GroupManager.BukkitPermissions.updateAllPlayers(); + return true; + } + } catch (Exception e) { + } + return false; + } + + public ArrayList subGroupListCopy() { + ArrayList val = new ArrayList(); + for (String gstr : subGroups) { + Group g = getDataSource().getGroup(gstr); + if (g == null) { + removeSubGroup(g); + continue; + } + val.add(g); + } + return val; + } + + public ArrayList subGroupListStringCopy() { + return new ArrayList(subGroups); + } + + /** + * @return the variables + */ + public UserVariables getVariables() { + return variables; + } + + /** + * + * @param varList + */ + public void setVariables(Map varList) { + UserVariables temp = new UserVariables(this, varList); + variables.clearVars(); + for (String key : temp.getVarKeyList()) { + variables.addVar(key, temp.getVarObject(key)); + } + flagAsChanged(); + if (GroupManager.isLoaded()) + if (GroupManager.BukkitPermissions.player_join = false) + GroupManager.BukkitPermissions.updateAllPlayers(); + } + + public User updatePlayer(Player player) { + if (player != null) { + bukkitPlayer = player; + } + return this; + } + + public Player getBukkitPlayer() { + if (bukkitPlayer == null) { + bukkitPlayer = Bukkit.getPlayer(this.getName()); + } + return bukkitPlayer; + } - /** - * - * @return User clone - */ - @Override - public User clone() { - User clone = new User(getDataSource(), this.getName()); - clone.group = this.group; - for (String perm : this.getPermissionList()) { - clone.addPermission(perm); - } - //clone.variables = this.variables.clone(); - //clone.flagAsChanged(); - return clone; - } - - /** - * Use this to deliver a user from one WorldDataHolder to another - * @param dataSource - * @return null if given dataSource already contains the same user - */ - public User clone(WorldDataHolder dataSource) { - if (dataSource.isUserDeclared(this.getName())) { - return null; - } - User clone = dataSource.createUser(this.getName()); - if (dataSource.getGroup(group) == null) { - clone.setGroup(dataSource.getDefaultGroup()); - } else { - clone.setGroup(dataSource.getGroup(this.getGroupName())); - } - for (String perm : this.getPermissionList()) { - clone.addPermission(perm); - } - //clone.variables = this.variables.clone(); - clone.flagAsChanged(); - return clone; - } - - public Group getGroup() { - Group result = getDataSource().getGroup(group); - if (result == null) { - this.setGroup(getDataSource().getDefaultGroup()); - result = getDataSource().getDefaultGroup(); - } - return result; - } - - /** - * @return the group - */ - public String getGroupName() { - Group result = getDataSource().getGroup(group); - if (result == null) { - group = getDataSource().getDefaultGroup().getName(); - } - return group; - } - - /** - * @param group the group to set - */ - @Deprecated - public void setGroup(String group) { - this.group = group; - flagAsChanged(); - if (GroupManager.isLoaded()) - if(GroupManager.BukkitPermissions.player_join = false) - GroupManager.BukkitPermissions.updateAllPlayers(); - } - - /** - * @param group the group to set - */ - public void setGroup(Group group) { - if (!this.getDataSource().groupExists(group.getName())) { - getDataSource().addGroup(group); - } - group = getDataSource().getGroup(group.getName()); - String oldGroup = this.group; - this.group = group.getName(); - flagAsChanged(); - if (GroupManager.isLoaded()) { - if (GroupManager.BukkitPermissions.player_join = false) - GroupManager.BukkitPermissions.updateAllPlayers(); - - // Do we notify of the group change? - String defaultGroupName = getDataSource().getDefaultGroup().getName(); - // if we were not in the default group - // or we were in the default group and the move is to a different group. - boolean notify = (!oldGroup.equalsIgnoreCase(defaultGroupName)) || ((oldGroup.equalsIgnoreCase(defaultGroupName)) && (!this.group.equalsIgnoreCase(defaultGroupName))) ; - - if (notify) GroupManager.notify(this.getName(), String.format(" moved to the group %s.", group.getName())); - } - } - - public void addSubGroup(Group subGroup) { - if (this.group.equalsIgnoreCase(subGroup.getName())) { - return; - } - if (!this.getDataSource().groupExists(subGroup.getName())) { - getDataSource().addGroup(subGroup); - } - subGroup = getDataSource().getGroup(subGroup.getName()); - removeSubGroup(subGroup); - subGroups.add(subGroup.getName()); - flagAsChanged(); - if (GroupManager.isLoaded()) - if (GroupManager.BukkitPermissions.player_join = false) - GroupManager.BukkitPermissions.updateAllPlayers(); - } - - public int subGroupsSize() { - return subGroups.size(); - } - - public boolean isSubGroupsEmpty() { - return subGroups.isEmpty(); - } - - public boolean containsSubGroup(Group subGroup) { - return subGroups.contains(subGroup.getName()); - } - - public boolean removeSubGroup(Group subGroup) { - try { - if (subGroups.remove(subGroup.getName())) { - flagAsChanged(); - if (GroupManager.isLoaded()) - if (GroupManager.BukkitPermissions.player_join = false) - GroupManager.BukkitPermissions.updateAllPlayers(); - return true; - } - } catch (Exception e) { - } - return false; - } - - public ArrayList subGroupListCopy() { - ArrayList val = new ArrayList(); - for (String gstr : subGroups) { - Group g = getDataSource().getGroup(gstr); - if (g == null) { - removeSubGroup(g); - continue; - } - val.add(g); - } - return val; - } - - public ArrayList subGroupListStringCopy() { - return new ArrayList(subGroups); - } - - /** - * @return the variables - */ - public UserVariables getVariables() { - return variables; - } - - /** - * - * @param varList - */ - public void setVariables(Map varList) { - UserVariables temp = new UserVariables(this, varList); - variables.clearVars(); - for (String key : temp.getVarKeyList()) { - variables.addVar(key, temp.getVarObject(key)); - } - flagAsChanged(); - if (GroupManager.isLoaded()) - if (GroupManager.BukkitPermissions.player_join = false) - GroupManager.BukkitPermissions.updateAllPlayers(); - } } diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/OverloadedWorldHolder.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/OverloadedWorldHolder.java index e7fa81627..8e2df5d10 100644 --- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/OverloadedWorldHolder.java +++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/OverloadedWorldHolder.java @@ -44,12 +44,13 @@ public class OverloadedWorldHolder extends WorldDataHolder { @Override public User getUser(String userName) { //OVERLOADED CODE - if (overloadedUsers.containsKey(userName.toLowerCase())) { - return overloadedUsers.get(userName.toLowerCase()); + String userNameLowered = userName.toLowerCase(); + if (overloadedUsers.containsKey(userNameLowered)) { + return overloadedUsers.get(userNameLowered); } //END CODE - if (users.containsKey(userName.toLowerCase())) { - return users.get(userName.toLowerCase()); + if (users.containsKey(userNameLowered)) { + return users.get(userNameLowered); } User newUser = createUser(userName); haveUsersChanged = true; diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/worlds/WorldsHolder.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/worlds/WorldsHolder.java index 6544bb1cc..25eaf8526 100644 --- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/worlds/WorldsHolder.java +++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/worlds/WorldsHolder.java @@ -268,9 +268,10 @@ public class WorldsHolder { * @return OverloadedWorldHolder */ public OverloadedWorldHolder getWorldData(String worldName) { - OverloadedWorldHolder data = worldsData.get(worldName.toLowerCase()); - if (mirrors.containsKey(worldName.toLowerCase())) { - String realOne = mirrors.get(worldName.toLowerCase()); + String worldNameLowered = worldName.toLowerCase(); + OverloadedWorldHolder data = worldsData.get(worldNameLowered); + if (mirrors.containsKey(worldNameLowered)) { + String realOne = mirrors.get(worldNameLowered); data = worldsData.get(realOne.toLowerCase()); } if (data == null) { diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java index 91c73b5c2..6e18bf3f8 100644 --- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java +++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java @@ -17,6 +17,7 @@ import org.anjocaido.groupmanager.utils.PermissionCheckResult; import org.anjocaido.groupmanager.utils.PermissionCheckResult.Type; import org.bukkit.Bukkit; import org.bukkit.entity.Player; +import org.bukkit.permissions.Permission; /** * Everything here maintains the model created by Nijikokun @@ -62,7 +63,7 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface { */ @Override public boolean permission(Player player, String permission) { - return checkUserPermission(ph.getUser(player.getName()), permission); + return checkUserPermission(ph.getUser(player.getName()).updatePlayer(player), permission); } /** @@ -98,13 +99,13 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface { public List getAllPlayersPermissions(String userName) { List playerPermArray = new ArrayList(); - + for (String perm : ph.getUser(userName).getPermissionList()) { if ((!playerPermArray.contains(perm)) && (!playerPermArray.contains("-" + perm))) { playerPermArray.add(perm); - + Map children = GroupManager.BukkitPermissions.getAllChildren(perm, playerPermArray); - + if (children != null) { for (String child : children.keySet()) { if (children.get(child)) @@ -135,7 +136,7 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface { for (String perm : ph.getGroup(group).getPermissionList()) { if ((!playerPermArray.contains(perm)) && (!playerPermArray.contains("-" + perm))) { playerPermArray.add(perm); - + Map children = GroupManager.BukkitPermissions.getAllChildren(perm, playerPermArray); if (children != null) { for (String child : children.keySet()) { @@ -149,7 +150,8 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface { } } } - //Collections.sort(playerPermArray, StringPermissionComparator.getInstance()); + // Collections.sort(playerPermArray, + // StringPermissionComparator.getInstance()); return playerPermArray; } @@ -243,7 +245,8 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface { /** * Check if user can build. Checks inheritance and subgroups. * - * @param userName Player's name + * @param userName + * Player's name * @return true if the user can build */ public boolean canUserBuild(String userName) { @@ -283,8 +286,8 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface { } /** - * Checks the specified group for the Info Build node. - * Does NOT check inheritance + * Checks the specified group for the Info Build node. Does NOT check + * inheritance * * @param groupName * @return true if can build @@ -615,15 +618,8 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface { result.askedPermission = permission; result.owner = user; for (String access : user.getPermissionList()) { - if (comparePermissionString(access, permission)) { - result.accessLevel = access; - if (access.charAt(0) == '-') { - result.resultType = PermissionCheckResult.Type.NEGATION; - } else if (access.charAt(0) == '+') { - result.resultType = PermissionCheckResult.Type.EXCEPTION; - } else { - result.resultType = PermissionCheckResult.Type.FOUND; - } + result.resultType = comparePermissionString(access, permission); + if (result.resultType != PermissionCheckResult.Type.NOTFOUND) { return result; } } @@ -645,15 +641,8 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface { result.owner = group; result.askedPermission = permission; for (String access : group.getPermissionList()) { - if (comparePermissionString(access, permission)) { - result.accessLevel = access; - if (access.charAt(0) == '-') { - result.resultType = PermissionCheckResult.Type.NEGATION; - } else if (access.charAt(0) == '+') { - result.resultType = PermissionCheckResult.Type.EXCEPTION; - } else { - result.resultType = PermissionCheckResult.Type.FOUND; - } + result.resultType = comparePermissionString(access, permission); + if (result.resultType != PermissionCheckResult.Type.NOTFOUND) { return result; } } @@ -670,7 +659,7 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface { */ public boolean checkUserPermission(User user, String permission) { PermissionCheckResult result = checkFullGMPermission(user, permission, true); - if (result.resultType.equals(PermissionCheckResult.Type.EXCEPTION) || result.resultType.equals(PermissionCheckResult.Type.FOUND)) { + if (result.resultType == PermissionCheckResult.Type.EXCEPTION || result.resultType == PermissionCheckResult.Type.FOUND) { return true; } @@ -686,13 +675,13 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface { * @return PermissionCheckResult */ public PermissionCheckResult checkFullUserPermission(User user, String targetPermission) { - + return checkFullGMPermission(user, targetPermission, true); } - + /** - * Check user and groups with inheritance and Bukkit if bukkit = true - * return a PermissionCheckResult. + * Check user and groups with inheritance and Bukkit if bukkit = true return + * a PermissionCheckResult. * * @param user * @param targetPermission @@ -708,31 +697,33 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface { return result; } - if (checkBukkit == true) { - // Check Bukkit perms to support plugins which add perms via code (Heroes). - final Player player = Bukkit.getPlayer(user.getName()); - if ((player != null) && (player.hasPermission(targetPermission))) { - result.resultType = PermissionCheckResult.Type.FOUND; + if (checkBukkit) { + // Check Bukkit perms to support plugins which add perms via code + // (Heroes). + final Player player = user.getBukkitPlayer(); + final Permission bukkitPerm = Bukkit.getPluginManager().getPermission(targetPermission); + if (player != null && bukkitPerm != null) { + result.resultType = player.hasPermission(bukkitPerm) ? PermissionCheckResult.Type.FOUND : PermissionCheckResult.Type.NEGATION; result.owner = user; return result; } } - + PermissionCheckResult resultUser = checkUserOnlyPermission(user, targetPermission); - if (!resultUser.resultType.equals(PermissionCheckResult.Type.NOTFOUND)) { + if (resultUser.resultType != PermissionCheckResult.Type.NOTFOUND) { return resultUser; } // IT ONLY CHECKS GROUPS PERMISSIONS IF RESULT FOR USER IS NOT FOUND PermissionCheckResult resultGroup = checkGroupPermissionWithInheritance(user.getGroup(), targetPermission); - if (!resultGroup.resultType.equals(PermissionCheckResult.Type.NOTFOUND)) { + if (resultGroup.resultType != PermissionCheckResult.Type.NOTFOUND) { return resultGroup; } // SUBGROUPS CHECK for (Group subGroup : user.subGroupListCopy()) { PermissionCheckResult resultSubGroup = checkGroupPermissionWithInheritance(subGroup, targetPermission); - if (!resultSubGroup.resultType.equals(PermissionCheckResult.Type.NOTFOUND)) { + if (resultSubGroup.resultType != PermissionCheckResult.Type.NOTFOUND) { return resultSubGroup; } } @@ -994,37 +985,42 @@ public class AnjoPermissionsHandler extends PermissionsReaderInterface { * * @param userAccessLevel * @param fullPermissionName - * @return true if found a matching token. false if not. + * @return PermissionCheckResult.Type */ - public boolean comparePermissionString(String userAccessLevel, String fullPermissionName) { - int userAccessLevelLength; - if (userAccessLevel == null || fullPermissionName == null - || fullPermissionName.length() == 0 || (userAccessLevelLength = userAccessLevel.length()) == 0) { - return false; - } - - int userAccessLevelOffset = 0; - if (userAccessLevel.charAt(0) == '+' || userAccessLevel.charAt(0) == '-') { - userAccessLevelOffset = 1; - } - if ("*".regionMatches(0, userAccessLevel, userAccessLevelOffset, userAccessLevelLength - userAccessLevelOffset)) { - return true; - } - int fullPermissionNameOffset; - if (fullPermissionName.charAt(0) == '+' || fullPermissionName.charAt(0) == '-') { - fullPermissionNameOffset = 1; - } else { - fullPermissionNameOffset = 0; - } - - if (userAccessLevel.charAt(userAccessLevel.length() - 1) == '*') { - return userAccessLevel.regionMatches(true, userAccessLevelOffset, fullPermissionName, fullPermissionNameOffset, userAccessLevelLength - userAccessLevelOffset - 1); - } else { - return userAccessLevel.regionMatches(true, userAccessLevelOffset, fullPermissionName, fullPermissionNameOffset, - Math.max(userAccessLevelLength - userAccessLevelOffset, fullPermissionName.length() - fullPermissionNameOffset)); - } - } + public PermissionCheckResult.Type comparePermissionString(String userAccessLevel, String fullPermissionName) { + int userAccessLevelLength; + if (userAccessLevel == null || fullPermissionName == null || fullPermissionName.length() == 0 || (userAccessLevelLength = userAccessLevel.length()) == 0) { + return PermissionCheckResult.Type.NOTFOUND; + } + + PermissionCheckResult.Type result = PermissionCheckResult.Type.FOUND; + int userAccessLevelOffset = 0; + if (userAccessLevel.charAt(0) == '+') { + userAccessLevelOffset = 1; + result = PermissionCheckResult.Type.EXCEPTION; + } else if (userAccessLevel.charAt(0) == '-') { + userAccessLevelOffset = 1; + result = PermissionCheckResult.Type.NEGATION; + } + if ("*".regionMatches(0, userAccessLevel, userAccessLevelOffset, userAccessLevelLength - userAccessLevelOffset)) { + return result; + } + int fullPermissionNameOffset; + if (fullPermissionName.charAt(0) == '+' || fullPermissionName.charAt(0) == '-') { + fullPermissionNameOffset = 1; + } else { + fullPermissionNameOffset = 0; + } + if (userAccessLevel.charAt(userAccessLevel.length() - 1) == '*') { + return userAccessLevel.regionMatches(true, userAccessLevelOffset, fullPermissionName, fullPermissionNameOffset, userAccessLevelLength - userAccessLevelOffset - 1) ? + result : PermissionCheckResult.Type.NOTFOUND; + } else { + return userAccessLevel.regionMatches(true, userAccessLevelOffset, fullPermissionName, fullPermissionNameOffset, + Math.max(userAccessLevelLength - userAccessLevelOffset, fullPermissionName.length() - fullPermissionNameOffset)) ? + result : PermissionCheckResult.Type.NOTFOUND; + } + } /** * Returns a list of all groups. -- cgit v1.2.3