From b1e34b55f88b5805c8cd16169ce0e7d64141b2bb Mon Sep 17 00:00:00 2001 From: Luck Date: Mon, 19 Sep 2016 21:00:23 +0100 Subject: [PATCH] Re-add regex permissions & actually take note of the config options --- .../lucko/luckperms/inject/LPPermissible.java | 9 +++++-- bukkit/src/main/resources/config.yml | 3 +-- .../me/lucko/luckperms/BungeePlayerCache.java | 9 +++++-- bungee/src/main/resources/config.yml | 3 +-- .../lucko/luckperms/constants/Patterns.java | 23 ++++++++-------- .../luckperms/utils/PermissionCalculator.java | 27 +++++++++++++++++++ .../api/sponge/LuckPermsUserSubject.java | 11 +++++--- sponge/src/main/resources/luckperms.conf | 3 +-- 8 files changed, 63 insertions(+), 25 deletions(-) diff --git a/bukkit/src/main/java/me/lucko/luckperms/inject/LPPermissible.java b/bukkit/src/main/java/me/lucko/luckperms/inject/LPPermissible.java index 3c7a87e88..03fb007f9 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/inject/LPPermissible.java +++ b/bukkit/src/main/java/me/lucko/luckperms/inject/LPPermissible.java @@ -59,10 +59,15 @@ public class LPPermissible extends PermissibleBase { super(sender); this.parent = sender; - List processors = new ArrayList<>(4); + List processors = new ArrayList<>(5); processors.add(new PermissionCalculator.MapProcessor(luckPermsPermissions)); processors.add(new AttachmentProcessor(attachmentPermissions)); - processors.add(new PermissionCalculator.WildcardProcessor(luckPermsPermissions)); + if (plugin.getConfiguration().getApplyWildcards()) { + processors.add(new PermissionCalculator.WildcardProcessor(luckPermsPermissions)); + } + if (plugin.getConfiguration().getApplyRegex()) { + processors.add(new PermissionCalculator.RegexProcessor(luckPermsPermissions)); + } processors.add(new BukkitDefaultsProcessor(parent::isOp)); calculator = new PermissionCalculator(plugin, parent.getName(), plugin.getConfiguration().getDebugPermissionChecks(), processors); diff --git a/bukkit/src/main/resources/config.yml b/bukkit/src/main/resources/config.yml index abe0514af..d029ccc7f 100644 --- a/bukkit/src/main/resources/config.yml +++ b/bukkit/src/main/resources/config.yml @@ -46,8 +46,7 @@ apply-wildcards: true # If the plugin should parse regex permissions. # If set to true, LuckPerms will detect regex permissions, marked with "r=" at the start of the node, and resolve & -# apply all registered permissions matching the regex. This will only work for plugins that define all of their -# permissions to the server. +# apply all registered permissions matching the regex. apply-regex: true # If the plugin should complete and apply shorthand permissions. diff --git a/bungee/src/main/java/me/lucko/luckperms/BungeePlayerCache.java b/bungee/src/main/java/me/lucko/luckperms/BungeePlayerCache.java index c0681d7cb..01c376e17 100644 --- a/bungee/src/main/java/me/lucko/luckperms/BungeePlayerCache.java +++ b/bungee/src/main/java/me/lucko/luckperms/BungeePlayerCache.java @@ -39,9 +39,14 @@ public class BungeePlayerCache { private final Map permissions = new ConcurrentHashMap<>(); public BungeePlayerCache(LuckPermsPlugin plugin, String name) { - List processors = new ArrayList<>(2); + List processors = new ArrayList<>(3); processors.add(new PermissionCalculator.MapProcessor(permissions)); - processors.add(new PermissionCalculator.WildcardProcessor(permissions)); + if (plugin.getConfiguration().getApplyWildcards()) { + processors.add(new PermissionCalculator.WildcardProcessor(permissions)); + } + if (plugin.getConfiguration().getApplyRegex()) { + processors.add(new PermissionCalculator.RegexProcessor(permissions)); + } calculator = new PermissionCalculator(plugin, name, plugin.getConfiguration().getDebugPermissionChecks(), processors); } diff --git a/bungee/src/main/resources/config.yml b/bungee/src/main/resources/config.yml index 7a4b8e3ea..6f75a55e5 100644 --- a/bungee/src/main/resources/config.yml +++ b/bungee/src/main/resources/config.yml @@ -46,8 +46,7 @@ apply-wildcards: true # If the plugin should parse regex permissions. # If set to true, LuckPerms will detect regex permissions, marked with "r=" at the start of the node, and resolve & -# apply all registered permissions matching the regex. This will only work for plugins that define all of their -# permissions to the server. +# apply all registered permissions matching the regex. apply-regex: true # If the plugin should complete and apply shorthand permissions. diff --git a/common/src/main/java/me/lucko/luckperms/constants/Patterns.java b/common/src/main/java/me/lucko/luckperms/constants/Patterns.java index 8db1c0a38..a85b781b5 100644 --- a/common/src/main/java/me/lucko/luckperms/constants/Patterns.java +++ b/common/src/main/java/me/lucko/luckperms/constants/Patterns.java @@ -40,19 +40,18 @@ public class Patterns { public static final Pattern STRIP_COLOR_PATTERN = Pattern.compile("(?i)" + String.valueOf('ยง') + "[0-9A-FK-OR]"); public static final Pattern NODE_CONTEXTS = Pattern.compile("\\(.+\\).*"); - public static Pattern compile(String regex) throws PatternSyntaxException { - if (!CACHE.containsKey(regex)) { - Pattern p; - try { - p = Pattern.compile(regex); - } catch (PatternSyntaxException e) { - return null; - } - - CACHE.put(regex, p); + public static Pattern compile(String regex) { + try { + return CACHE.computeIfAbsent(regex, s -> { + try { + return Pattern.compile(regex); + } catch (PatternSyntaxException e) { + return null; + } + }); + } catch (NullPointerException e) { + return null; } - - return CACHE.get(regex); } } diff --git a/common/src/main/java/me/lucko/luckperms/utils/PermissionCalculator.java b/common/src/main/java/me/lucko/luckperms/utils/PermissionCalculator.java index 646518271..6809ee4bd 100644 --- a/common/src/main/java/me/lucko/luckperms/utils/PermissionCalculator.java +++ b/common/src/main/java/me/lucko/luckperms/utils/PermissionCalculator.java @@ -27,10 +27,12 @@ import lombok.Getter; import lombok.RequiredArgsConstructor; import me.lucko.luckperms.LuckPermsPlugin; import me.lucko.luckperms.api.Tristate; +import me.lucko.luckperms.constants.Patterns; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import java.util.regex.Pattern; /** * Calculates and caches permissions @@ -85,6 +87,31 @@ public class PermissionCalculator { } } + @AllArgsConstructor + public static class RegexProcessor implements PermissionProcessor { + + @Getter + private final Map map; + + @Override + public Tristate hasPermission(String permission) { + for (Map.Entry e : map.entrySet()) { + if (e.getKey().toLowerCase().startsWith("r=")) { + Pattern p = Patterns.compile(e.getKey().substring(2)); + if (p == null) { + continue; + } + + if (p.matcher(permission).matches()) { + return Tristate.fromBoolean(e.getValue()); + } + } + } + + return Tristate.UNDEFINED; + } + } + @AllArgsConstructor public static class WildcardProcessor implements PermissionProcessor { diff --git a/sponge/src/main/java/me/lucko/luckperms/api/sponge/LuckPermsUserSubject.java b/sponge/src/main/java/me/lucko/luckperms/api/sponge/LuckPermsUserSubject.java index 9ad0088fd..772c9357a 100644 --- a/sponge/src/main/java/me/lucko/luckperms/api/sponge/LuckPermsUserSubject.java +++ b/sponge/src/main/java/me/lucko/luckperms/api/sponge/LuckPermsUserSubject.java @@ -54,10 +54,15 @@ public class LuckPermsUserSubject extends LuckPermsSubject { super(user, service); this.user = user; - List processors = new ArrayList<>(4); + List processors = new ArrayList<>(5); processors.add(new PermissionCalculator.MapProcessor(permissionCache)); - processors.add(new SpongeWildcardProcessor(permissionCache)); - processors.add(new PermissionCalculator.WildcardProcessor(permissionCache)); + if (service.getPlugin().getConfiguration().getApplyWildcards()) { + processors.add(new SpongeWildcardProcessor(permissionCache)); + processors.add(new PermissionCalculator.WildcardProcessor(permissionCache)); + } + if (service.getPlugin().getConfiguration().getApplyRegex()) { + processors.add(new PermissionCalculator.RegexProcessor(permissionCache)); + } processors.add(new SpongeDefaultsProcessor(service)); calculator = new PermissionCalculator(service.getPlugin(), user.getName(), service.getPlugin().getConfiguration().getDebugPermissionChecks(), processors); diff --git a/sponge/src/main/resources/luckperms.conf b/sponge/src/main/resources/luckperms.conf index f73509352..74ba68ff6 100644 --- a/sponge/src/main/resources/luckperms.conf +++ b/sponge/src/main/resources/luckperms.conf @@ -42,8 +42,7 @@ apply-wildcards=true # If the plugin should parse regex permissions. # If set to true, LuckPerms will detect regex permissions, marked with "r=" at the start of the node, and resolve & -# apply all registered permissions matching the regex. This will only work for plugins that define all of their -# permissions to the server. +# apply all registered permissions matching the regex. apply-regex=true # If the plugin should complete and apply shorthand permissions.