Add configuration of custom Special Permissions

This commit is contained in:
Marc Baloup 2021-08-15 03:24:56 +02:00
parent 425527ddb2
commit dcfafb92cb
Signed by: marcbal
GPG Key ID: BBC0FE3ABC30B893
6 changed files with 42 additions and 46 deletions

View File

@ -34,6 +34,14 @@ public class Permissions {
} }
} }
public static void addSpecialPermissions(SpecialPermission... specialPermissions) {
checkInitialized();
if (specialPermissions == null)
return;
for (SpecialPermission sp : specialPermissions)
resolver.specialPermissions.add(sp);
}
private static void checkInitialized() { private static void checkInitialized() {
if (backendReader == null) { if (backendReader == null) {
throw new IllegalStateException("Permissions system not initialized. Check the server logs to check if there is an error during the startup, and check if the init() method is called properly."); throw new IllegalStateException("Permissions system not initialized. Check the server logs to check if there is an error during the startup, and check if the init() method is called properly.");

View File

@ -386,6 +386,12 @@ public class PermissionsResolver {
/* package */ List<SpecialPermission> specialPermissions = new ArrayList<>();
private PermResolutionNode resolveSelfPermission(CachedEntity entity, String permission, String server, String world) { private PermResolutionNode resolveSelfPermission(CachedEntity entity, String permission, String server, String world) {
// special permissions // special permissions
PermState result = PermState.UNDEFINED; PermState result = PermState.UNDEFINED;
@ -396,38 +402,15 @@ public class PermissionsResolver {
* Check for special permissions * Check for special permissions
*/ */
if (entity instanceof CachedPlayer) { if (entity instanceof CachedPlayer) {
PermPlayer permP = new PermPlayer(((CachedPlayer) entity).playerId); PermPlayer permP = Permissions.getPlayer(((CachedPlayer) entity).playerId);
ParsedSelfPermission specialPerm = null; ParsedSelfPermission specialPerm = null;
if (permission.equals("pandacube.grade.isinstaff")) {
boolean res = permP.inheritsFromGroup("staff-base", true); 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); specialPerm = new ParsedSelfPermission(permission, res, PermType.SPECIAL);
conflict = "Special permission 'pandacube.grade.isinstaff' is deprecated. Use 'pandacube.inheritsfrom.<staffBaseGroup>' instead."; break;
} }
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.<groupName>' is deprecated. Use 'pandacube.ingroup.<groupName>' 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);
} }
if (specialPerm != null) { if (specialPerm != null) {

View File

@ -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);
}
}

View File

@ -33,7 +33,7 @@ import fr.pandacube.lib.core.util.Log;
public class PlayerFinder { public class PlayerFinder {
private static Cache<UUID, String> playerLastKnownName = CacheBuilder.newBuilder() private static Cache<UUID, String> playerLastKnownName = CacheBuilder.newBuilder()
.expireAfterWrite(2, TimeUnit.MINUTES) .expireAfterWrite(10, TimeUnit.MINUTES)
.maximumSize(1000) .maximumSize(1000)
.build(); .build();

View File

@ -1,5 +1,6 @@
package fr.pandacube.lib.core.util; package fr.pandacube.lib.core.util;
import java.util.Arrays;
import java.util.List; import java.util.List;
public class StringUtil { public class StringUtil {
@ -34,22 +35,10 @@ 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) { public static String repeat(char base, int count) {
char[] chars = new char[count]; char[] chars = new char[count];
for (int i = 0; i < count; i++) { Arrays.fill(chars, base);
chars[i] = base;
}
return new String(chars); return new String(chars);
} }
} }

View File

@ -215,7 +215,7 @@ public class TimeUtil {
int padding = leftPad - valueStr.length(); int padding = leftPad - valueStr.length();
if (padding <= 0) if (padding <= 0)
return valueStr; return valueStr;
return StringUtil.repeat("0", padding) + valueStr; return "0".repeat(padding) + valueStr;
} }