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

@ -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) {

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) {
// 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.<staffBaseGroup>' 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.<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);
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) {

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 {
private static Cache<UUID, String> playerLastKnownName = CacheBuilder.newBuilder()
.expireAfterWrite(2, TimeUnit.MINUTES)
.expireAfterWrite(10, TimeUnit.MINUTES)
.maximumSize(1000)
.build();

View File

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

View File

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