Ability to list all effective permissions for a specific player or group

This commit is contained in:
Marc Baloup 2021-04-17 00:06:21 +02:00
parent 85964ed624
commit 133b9917b4
Signed by: marcbal
GPG Key ID: BBC0FE3ABC30B893
4 changed files with 66 additions and 6 deletions

View File

@ -1,6 +1,7 @@
package fr.pandacube.lib.core.permissions; package fr.pandacube.lib.core.permissions;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
@ -76,6 +77,19 @@ public abstract class PermEntity {
} }
public Map<String, Boolean> listEffectivePermissions() {
return Permissions.resolver.getEffectivePermissionList(name, type, null, null);
}
public Map<String, Boolean> listEffectivePermissions(String server) {
return Permissions.resolver.getEffectivePermissionList(name, type, server, null);
}
public Map<String, Boolean> listEffectivePermissions(String server, String world) {
return Permissions.resolver.getEffectivePermissionList(name, type, server, world);
}
public Boolean hasPermission(String permission) { public Boolean hasPermission(String permission) {
return Permissions.resolver.getEffectivePermission(name, type, permission, null, null); return Permissions.resolver.getEffectivePermission(name, type, permission, null, null);
} }

View File

@ -89,7 +89,7 @@ import fr.pandacube.lib.core.util.Log;
playerSelfPerms = playerRawData.get("permissions").stream() playerSelfPerms = playerRawData.get("permissions").stream()
.peek(e -> { .peek(e -> {
String value = e.get(SQLPermissions.value); String value = e.get(SQLPermissions.value);
fullPermissionsList.add(value.substring(value.startsWith("-") ? 1 : 0)); fullPermissionsList.add(value.substring(value.startsWith("-") ? 1 : 0).toLowerCase());
}) })
.collect(Collectors.groupingBy(e -> new ServerWorldKey(e.get(SQLPermissions.server), e.get(SQLPermissions.world)), .collect(Collectors.groupingBy(e -> new ServerWorldKey(e.get(SQLPermissions.server), e.get(SQLPermissions.world)),
LinkedHashMap::new, LinkedHashMap::new,

View File

@ -2,7 +2,9 @@ package fr.pandacube.lib.core.permissions;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
@ -39,6 +41,9 @@ public class PermissionsResolver {
synchronized (effectivePermissionsCache) { synchronized (effectivePermissionsCache) {
effectivePermissionsCache.asMap().keySet().removeIf(k -> k.type == EntityType.User && playerId.equals(k.name)); effectivePermissionsCache.asMap().keySet().removeIf(k -> k.type == EntityType.User && playerId.equals(k.name));
} }
synchronized (effectivePermissionsListCache) {
effectivePermissionsListCache.asMap().keySet().removeIf(k -> k.type == EntityType.User && playerId.equals(k.name));
}
synchronized (effectiveDataCache) { synchronized (effectiveDataCache) {
effectiveDataCache.asMap().keySet().removeIf(k -> k.type == EntityType.User && playerId.equals(k.name)); effectiveDataCache.asMap().keySet().removeIf(k -> k.type == EntityType.User && playerId.equals(k.name));
} }
@ -46,6 +51,7 @@ public class PermissionsResolver {
/* package */ void clearCache() { /* package */ void clearCache() {
effectivePermissionsCache.invalidateAll(); effectivePermissionsCache.invalidateAll();
effectivePermissionsListCache.invalidateAll();
effectiveDataCache.invalidateAll(); effectiveDataCache.invalidateAll();
} }
@ -70,7 +76,7 @@ public class PermissionsResolver {
} }
private Cache<DataCacheKey, String> effectiveDataCache = CacheBuilder.newBuilder() private Cache<DataCacheKey, String> effectiveDataCache = CacheBuilder.newBuilder()
.expireAfterWrite(5, TimeUnit.MINUTES) .expireAfterAccess(10, TimeUnit.MINUTES)
.build(); .build();
private String getEffectiveData(String name, EntityType type, DataType dataType) { private String getEffectiveData(String name, EntityType type, DataType dataType) {
@ -221,8 +227,47 @@ public class PermissionsResolver {
private Cache<PermCacheKey, Map<String, Boolean>> effectivePermissionsListCache = CacheBuilder.newBuilder()
.expireAfterAccess(10, TimeUnit.MINUTES)
.build();
/* package */ Map<String, Boolean> getEffectivePermissionList(String name, EntityType type, String server, String world) {
Preconditions.checkNotNull(name, "name cant be null");
Preconditions.checkNotNull(type, "type cant be null");
Preconditions.checkArgument(world == null || server != null, "world not null but server is null");
String fServer = server == null ? null : server.toLowerCase();
String fWorld = world == null ? null : world.toLowerCase();
try {
return effectivePermissionsListCache.get(new PermCacheKey(name, type, null, fServer, fWorld), () -> {
Map<String, Boolean> permList = new LinkedHashMap<>();
for (String perm : backendReader.getFullPermissionsList()) {
Boolean has = getEffectivePermission(name, type, perm, fServer, fWorld);
if (has == null)
continue;
permList.put(perm.toLowerCase(), has);
}
return permList;
});
} catch (ExecutionException e) {
Log.severe(e);
return null;
}
}
private Cache<PermCacheKey, PermState> effectivePermissionsCache = CacheBuilder.newBuilder() private Cache<PermCacheKey, PermState> effectivePermissionsCache = CacheBuilder.newBuilder()
.expireAfterWrite(5, TimeUnit.MINUTES) .expireAfterAccess(10, TimeUnit.MINUTES)
.build(); .build();
/* package */ Boolean getEffectivePermission(String name, EntityType type, String permission, String server, String world) { /* package */ Boolean getEffectivePermission(String name, EntityType type, String permission, String server, String world) {

View File

@ -20,11 +20,12 @@ public class ServerWorldKey implements Comparable<ServerWorldKey> {
public int hashCode() { public int hashCode() {
return Objects.hash(world, server); return Objects.hash(world, server);
} }
private static final Comparator<String> STR_NULL_FIRST_COMPARATOR = Comparator.nullsFirst(String::compareToIgnoreCase);
@Override @Override
public int compareTo(ServerWorldKey o) { public int compareTo(ServerWorldKey o) {
Comparator<String> compStrNullFirst = Comparator.nullsFirst(String::compareToIgnoreCase); return Comparator.comparing((ServerWorldKey k) -> k.server, STR_NULL_FIRST_COMPARATOR)
return Comparator.comparing((ServerWorldKey k) -> k.server, compStrNullFirst) .thenComparing(k -> k.world, STR_NULL_FIRST_COMPARATOR)
.thenComparing(k -> k.world, compStrNullFirst)
.compare(this, o); .compare(this, o);
} }
} }