diff --git a/pandalib-permissions/src/main/java/fr/pandacube/lib/permissions/PermGroup.java b/pandalib-permissions/src/main/java/fr/pandacube/lib/permissions/PermGroup.java index 5752844..51b7bfe 100644 --- a/pandalib-permissions/src/main/java/fr/pandacube/lib/permissions/PermGroup.java +++ b/pandalib-permissions/src/main/java/fr/pandacube/lib/permissions/PermGroup.java @@ -1,12 +1,15 @@ package fr.pandacube.lib.permissions; -import java.util.ArrayList; -import java.util.List; -import java.util.stream.Collectors; - +import fr.pandacube.lib.db.DBException; import fr.pandacube.lib.permissions.PermissionsCachedBackendReader.CachedGroup; import fr.pandacube.lib.permissions.SQLPermissions.EntityType; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.UUID; +import java.util.stream.Collectors; + /** * Represents an group in the permission system. */ @@ -47,6 +50,23 @@ public final class PermGroup extends PermEntity { .toList()); } + /** + * Gets all the players that inherits from this group. + * This method does not use cached data. + * @param recursive true to include players that are in inherited groups. + * @return the players that inherits from this group. + * @throws DBException if a database error occurs. + */ + public Set getInheritedPlayers(boolean recursive) throws DBException { + Set players = new HashSet<>(getBackendEntity().getPlayersInGroup()); + if (recursive) { + for (PermGroup inheritedGroups : getInheritedGroups()) { + players.addAll(inheritedGroups.getInheritedPlayers(true)); + } + } + return players; + } + /** * Tells if this group is a default group. * A player inherits all default groups when they don’t explicitely inherit from at least one group. 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 a6dc496..0de67c0 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 @@ -1,6 +1,7 @@ package fr.pandacube.lib.permissions; import java.util.ArrayList; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -115,6 +116,13 @@ import fr.pandacube.lib.util.Log; return player; } + + + + + + + @@ -299,6 +307,19 @@ import fr.pandacube.lib.util.Log; super(n, p, s, perms); deflt = dflt; } + + + + /* package */ Set getPlayersInGroup() throws DBException { + Set ids = new HashSet<>(); + DB.forEach(SQLPermissions.class, + SQLPermissions.type.eq(EntityType.User.getCode()) + .and(SQLPermissions.key.eq("groups")) + .and(SQLPermissions.value.eq(name)), + e -> ids.add(UUID.fromString(e.get(SQLPermissions.name))) + ); + return ids; + } } }