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)
This commit is contained in:
Marc Baloup 2023-08-14 01:49:56 +02:00
parent 2d950117d3
commit 0fcd02c80d
4 changed files with 94 additions and 1 deletions

View File

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

View File

@ -10,7 +10,7 @@ import fr.pandacube.lib.permissions.SQLPermissions.EntityType;
/** /**
* Represents a player in the permission system. * 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; private final UUID playerId;
/* package */ PermPlayer(UUID id) { /* package */ PermPlayer(UUID id) {
super(id.toString(), EntityType.User); super(id.toString(), EntityType.User);

View File

@ -104,6 +104,19 @@ public class Permissions {
return new PermPlayer(playerId); 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. * 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 * This can be called as soon as possible when a player connects, so the permission data of the player are

View File

@ -28,6 +28,12 @@ import fr.pandacube.lib.util.Log;
} }
/* package */ static final CachedPlayer DEFAULT_PLAYER = new CachedPlayer(DefaultPlayer.ID, null, null, Map.of());
static {
DEFAULT_PLAYER.usingDefaultGroups = true;
}
private final Cache<UUID, CachedPlayer> usersCache = CacheBuilder.newBuilder() private final Cache<UUID, CachedPlayer> usersCache = CacheBuilder.newBuilder()
@ -58,6 +64,8 @@ import fr.pandacube.lib.util.Log;
} }
private CachedPlayer initPlayer(UUID playerId) throws DBException { private CachedPlayer initPlayer(UUID playerId) throws DBException {
if (playerId.equals(DEFAULT_PLAYER.playerId))
return DEFAULT_PLAYER;
SQLElementList<SQLPermissions> playerData = DB.getAll(SQLPermissions.class, SQLElementList<SQLPermissions> playerData = DB.getAll(SQLPermissions.class,
SQLPermissions.type.eq(EntityType.User.getCode()) SQLPermissions.type.eq(EntityType.User.getCode())
@ -196,6 +204,8 @@ import fr.pandacube.lib.util.Log;
cacheIsUpdating = false; cacheIsUpdating = false;
usersCache.invalidateAll(); usersCache.invalidateAll();
fullPermissionsList = newFullPermissionsList; fullPermissionsList = newFullPermissionsList;
DEFAULT_PLAYER.groups.clear();
DEFAULT_PLAYER.groups.addAll(getDefaultGroups());
} }
} finally { } finally {
synchronized (this) { synchronized (this) {