From 0fcd02c80d4263d6708d36a2b26d85cc3a1cabbe Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Mon, 14 Aug 2023 01:49:56 +0200 Subject: [PATCH] Permission system now provides a default player The default player has a fixed name and uuid that should never collide with existing players. It will never have self permission data, and it will always be part of the default groups. It is useful for anonymous permission test (for instance, listing only the servers that are publicly visible for the ping server list) --- .../lib/permissions/DefaultPlayer.java | 70 +++++++++++++++++++ .../pandacube/lib/permissions/PermPlayer.java | 2 +- .../lib/permissions/Permissions.java | 13 ++++ .../PermissionsCachedBackendReader.java | 10 +++ 4 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 pandalib-permissions/src/main/java/fr/pandacube/lib/permissions/DefaultPlayer.java diff --git a/pandalib-permissions/src/main/java/fr/pandacube/lib/permissions/DefaultPlayer.java b/pandalib-permissions/src/main/java/fr/pandacube/lib/permissions/DefaultPlayer.java new file mode 100644 index 0000000..084cb2c --- /dev/null +++ b/pandalib-permissions/src/main/java/fr/pandacube/lib/permissions/DefaultPlayer.java @@ -0,0 +1,70 @@ +package fr.pandacube.lib.permissions; + +import fr.pandacube.lib.util.Log; + +import java.util.UUID; + +/** + * Represents a dummy player in the permission system, that have no specific data, only inheriting from the default + * groups. + * + * The current implementation provides a player named {@code default.0} with an uuid of + * {@code fffdef17-ffff-b0ff-ffff-ffffffffffff}. + * Trying to set a permission data for this player will log a warning. + */ +/* package */ final class DefaultPlayer extends PermPlayer { + + + // a static UUID that ensure it will not collide with any player, either online or offline or floodgate: + // the version bits are set to B (11), that is offline mode (3) + alt (8) account, and alt account counter + // set to 0 that is usually impossible due to the counter starting at 1. + /* package */ static final UUID ID = new UUID(0xfffdef17_ffff_b0ffL, -1L); + + /* package */ static final String NAME = "default.0"; + + + /* package */ DefaultPlayer() { + super(ID); + } + + @Override + public String getName() { + return NAME; + } + + public void setGroup(String group) { + warnDefaultPlayerSetData(); + } + + public void addGroup(String group) { + warnDefaultPlayerSetData(); + } + + public void removeGroup(String group) { + warnDefaultPlayerSetData(); + } + + @Override + public void setSelfPrefix(String prefix) { + warnDefaultPlayerSetData(); + } + + @Override + public void setSelfSuffix(String suffix) { + warnDefaultPlayerSetData(); + } + + @Override + public void addSelfPermission(String permission, String server, String world) { + warnDefaultPlayerSetData(); + } + + @Override + public void removeSelfPermission(String permission, String server, String world) { + warnDefaultPlayerSetData(); + } + + private void warnDefaultPlayerSetData() { + Log.warning(new UnsupportedOperationException("Trying to set permission data of default player")); + } +} \ No newline at end of file diff --git a/pandalib-permissions/src/main/java/fr/pandacube/lib/permissions/PermPlayer.java b/pandalib-permissions/src/main/java/fr/pandacube/lib/permissions/PermPlayer.java index 9a6e628..c96c75d 100644 --- a/pandalib-permissions/src/main/java/fr/pandacube/lib/permissions/PermPlayer.java +++ b/pandalib-permissions/src/main/java/fr/pandacube/lib/permissions/PermPlayer.java @@ -10,7 +10,7 @@ import fr.pandacube.lib.permissions.SQLPermissions.EntityType; /** * Represents a player in the permission system. */ -public final class PermPlayer extends PermEntity { +public sealed class PermPlayer extends PermEntity permits DefaultPlayer { private final UUID playerId; /* package */ PermPlayer(UUID id) { super(id.toString(), EntityType.User); diff --git a/pandalib-permissions/src/main/java/fr/pandacube/lib/permissions/Permissions.java b/pandalib-permissions/src/main/java/fr/pandacube/lib/permissions/Permissions.java index 070156d..3872205 100644 --- a/pandalib-permissions/src/main/java/fr/pandacube/lib/permissions/Permissions.java +++ b/pandalib-permissions/src/main/java/fr/pandacube/lib/permissions/Permissions.java @@ -104,6 +104,19 @@ public class Permissions { return new PermPlayer(playerId); } + /** + * Gets a dummy permission player object, that have no specific data, only inheriting from the default groups. + * + * The current implementation provides a player named {@code default.0} with an uuid of + * {@code fffdef17-ffff-b0ff-ffff-ffffffffffff}. + * Trying to set a permission data for this player will log a warning. + * @return the default permission player. + */ + public static PermPlayer getDefaultPlayer() { + checkInitialized(); + return new DefaultPlayer(); + } + /** * Asks the permission system to preventively and asynchronously cache the data of the provided player. * This can be called as soon as possible when a player connects, so the permission data of the player are diff --git a/pandalib-permissions/src/main/java/fr/pandacube/lib/permissions/PermissionsCachedBackendReader.java b/pandalib-permissions/src/main/java/fr/pandacube/lib/permissions/PermissionsCachedBackendReader.java index c65d1b4..4891b96 100644 --- a/pandalib-permissions/src/main/java/fr/pandacube/lib/permissions/PermissionsCachedBackendReader.java +++ b/pandalib-permissions/src/main/java/fr/pandacube/lib/permissions/PermissionsCachedBackendReader.java @@ -26,6 +26,12 @@ import fr.pandacube.lib.util.Log; /* package */ PermissionsCachedBackendReader() throws DBException { clearAndResetCache(); } + + + /* package */ static final CachedPlayer DEFAULT_PLAYER = new CachedPlayer(DefaultPlayer.ID, null, null, Map.of()); + static { + DEFAULT_PLAYER.usingDefaultGroups = true; + } @@ -58,6 +64,8 @@ import fr.pandacube.lib.util.Log; } private CachedPlayer initPlayer(UUID playerId) throws DBException { + if (playerId.equals(DEFAULT_PLAYER.playerId)) + return DEFAULT_PLAYER; SQLElementList playerData = DB.getAll(SQLPermissions.class, SQLPermissions.type.eq(EntityType.User.getCode()) @@ -196,6 +204,8 @@ import fr.pandacube.lib.util.Log; cacheIsUpdating = false; usersCache.invalidateAll(); fullPermissionsList = newFullPermissionsList; + DEFAULT_PLAYER.groups.clear(); + DEFAULT_PLAYER.groups.addAll(getDefaultGroups()); } } finally { synchronized (this) {