From dcfafb92cb46f1923ff74e1ed41800ee8c7f2ac1 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Sun, 15 Aug 2021 03:24:56 +0200 Subject: [PATCH] Add configuration of custom Special Permissions --- .../lib/core/permissions/Permissions.java | 8 ++++ .../core/permissions/PermissionsResolver.java | 45 ++++++------------- .../core/permissions/SpecialPermission.java | 16 +++++++ .../lib/core/players/PlayerFinder.java | 2 +- .../pandacube/lib/core/util/StringUtil.java | 15 +------ .../fr/pandacube/lib/core/util/TimeUtil.java | 2 +- 6 files changed, 42 insertions(+), 46 deletions(-) create mode 100644 Core/src/main/java/fr/pandacube/lib/core/permissions/SpecialPermission.java diff --git a/Core/src/main/java/fr/pandacube/lib/core/permissions/Permissions.java b/Core/src/main/java/fr/pandacube/lib/core/permissions/Permissions.java index 503908d..4fcf6c2 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/permissions/Permissions.java +++ b/Core/src/main/java/fr/pandacube/lib/core/permissions/Permissions.java @@ -33,6 +33,14 @@ public class Permissions { throw e; } } + + public static void addSpecialPermissions(SpecialPermission... specialPermissions) { + checkInitialized(); + if (specialPermissions == null) + return; + for (SpecialPermission sp : specialPermissions) + resolver.specialPermissions.add(sp); + } private static void checkInitialized() { if (backendReader == null) { diff --git a/Core/src/main/java/fr/pandacube/lib/core/permissions/PermissionsResolver.java b/Core/src/main/java/fr/pandacube/lib/core/permissions/PermissionsResolver.java index 801e0a0..a4016c7 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/permissions/PermissionsResolver.java +++ b/Core/src/main/java/fr/pandacube/lib/core/permissions/PermissionsResolver.java @@ -386,6 +386,12 @@ public class PermissionsResolver { + + + /* package */ List specialPermissions = new ArrayList<>(); + + + private PermResolutionNode resolveSelfPermission(CachedEntity entity, String permission, String server, String world) { // special permissions PermState result = PermState.UNDEFINED; @@ -396,38 +402,15 @@ public class PermissionsResolver { * Check for special permissions */ if (entity instanceof CachedPlayer) { - PermPlayer permP = new PermPlayer(((CachedPlayer) entity).playerId); + PermPlayer permP = Permissions.getPlayer(((CachedPlayer) entity).playerId); ParsedSelfPermission specialPerm = null; - if (permission.equals("pandacube.grade.isinstaff")) { - boolean res = permP.inheritsFromGroup("staff-base", true); - specialPerm = new ParsedSelfPermission(permission, res, PermType.SPECIAL); - conflict = "Special permission 'pandacube.grade.isinstaff' is deprecated. Use 'pandacube.inheritsfrom.' instead."; - } - else if (permission.startsWith("pandacube.grade.")) { - String group = permission.substring("pandacube.grade.".length()); - boolean res = permP.inheritsFromGroup(group, false); - specialPerm = new ParsedSelfPermission(permission, res, PermType.SPECIAL); - conflict = "Special permission 'pandacube.grade.' is deprecated. Use 'pandacube.ingroup.' instead."; - } - else if (permission.startsWith("pandacube.ingroup.")) { - String group = permission.substring("pandacube.ingroup.".length()); - boolean res = permP.inheritsFromGroup(group, false); - specialPerm = new ParsedSelfPermission(permission, res, PermType.SPECIAL); - } - else if (permission.startsWith("pandacube.inheritsfrom.")) { - String group = permission.substring("pandacube.inheritsfrom.".length()); - boolean res = permP.inheritsFromGroup(group, true); - specialPerm = new ParsedSelfPermission(permission, res, PermType.SPECIAL); - } - else if (permission.startsWith("pandacube.inserver.")) { - String testedServer = permission.substring("pandacube.inserver.".length()); - boolean res = testedServer.equals(server); - specialPerm = new ParsedSelfPermission(permission, res, PermType.SPECIAL); - } - else if (permission.startsWith("pandacube.inserverworld.")) { - String testedServerWorld = permission.substring("pandacube.inserverworld.".length()); - boolean res = server != null && world != null && testedServerWorld.equals(server + "." + world); - specialPerm = new ParsedSelfPermission(permission, res, PermType.SPECIAL); + + for (SpecialPermission spePerm : specialPermissions) { + if (spePerm.match().match(permission)) { + boolean res = spePerm.tester().test(permP, permission, server, world); + specialPerm = new ParsedSelfPermission(permission, res, PermType.SPECIAL); + break; + } } if (specialPerm != null) { diff --git a/Core/src/main/java/fr/pandacube/lib/core/permissions/SpecialPermission.java b/Core/src/main/java/fr/pandacube/lib/core/permissions/SpecialPermission.java new file mode 100644 index 0000000..d398c11 --- /dev/null +++ b/Core/src/main/java/fr/pandacube/lib/core/permissions/SpecialPermission.java @@ -0,0 +1,16 @@ +package fr.pandacube.lib.core.permissions; + +/** + * Represents a permission node that is based on an arbitrary player state. + */ +public record SpecialPermission(PermissionMatcher match, PermissionTester tester) { + + public interface PermissionMatcher { + boolean match(String permission); + } + + public interface PermissionTester { + boolean test(PermPlayer player, String permission, String server, String world); + } + +} diff --git a/Core/src/main/java/fr/pandacube/lib/core/players/PlayerFinder.java b/Core/src/main/java/fr/pandacube/lib/core/players/PlayerFinder.java index cdcaa82..f2fae7a 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/players/PlayerFinder.java +++ b/Core/src/main/java/fr/pandacube/lib/core/players/PlayerFinder.java @@ -33,7 +33,7 @@ import fr.pandacube.lib.core.util.Log; public class PlayerFinder { private static Cache playerLastKnownName = CacheBuilder.newBuilder() - .expireAfterWrite(2, TimeUnit.MINUTES) + .expireAfterWrite(10, TimeUnit.MINUTES) .maximumSize(1000) .build(); diff --git a/Core/src/main/java/fr/pandacube/lib/core/util/StringUtil.java b/Core/src/main/java/fr/pandacube/lib/core/util/StringUtil.java index 8701942..11b47fc 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/util/StringUtil.java +++ b/Core/src/main/java/fr/pandacube/lib/core/util/StringUtil.java @@ -1,5 +1,6 @@ package fr.pandacube.lib.core.util; +import java.util.Arrays; import java.util.List; public class StringUtil { @@ -33,23 +34,11 @@ public class StringUtil { - - public static String repeat(String base, int count) { - int baseLength = base.length(); - char[] baseChars = base.toCharArray(); - char[] chars = new char[baseLength * count]; - for (int i = 0; i < count; i++) { - System.arraycopy(baseChars, 0, chars, i * baseLength, baseLength); - } - return new String(chars); - } public static String repeat(char base, int count) { char[] chars = new char[count]; - for (int i = 0; i < count; i++) { - chars[i] = base; - } + Arrays.fill(chars, base); return new String(chars); } } diff --git a/Core/src/main/java/fr/pandacube/lib/core/util/TimeUtil.java b/Core/src/main/java/fr/pandacube/lib/core/util/TimeUtil.java index f12cc9e..5a02894 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/util/TimeUtil.java +++ b/Core/src/main/java/fr/pandacube/lib/core/util/TimeUtil.java @@ -215,7 +215,7 @@ public class TimeUtil { int padding = leftPad - valueStr.length(); if (padding <= 0) return valueStr; - return StringUtil.repeat("0", padding) + valueStr; + return "0".repeat(padding) + valueStr; }