2022-07-20 13:18:57 +02:00
|
|
|
|
package fr.pandacube.lib.permissions;
|
2021-03-21 20:17:31 +01:00
|
|
|
|
|
2023-01-07 17:12:21 +01:00
|
|
|
|
import fr.pandacube.lib.db.DBException;
|
2022-07-20 13:18:57 +02:00
|
|
|
|
import fr.pandacube.lib.permissions.PermissionsCachedBackendReader.CachedGroup;
|
|
|
|
|
import fr.pandacube.lib.permissions.SQLPermissions.EntityType;
|
2021-03-21 20:17:31 +01:00
|
|
|
|
|
2023-01-07 17:12:21 +01:00
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
2022-08-10 03:04:12 +02:00
|
|
|
|
/**
|
|
|
|
|
* Represents an group in the permission system.
|
|
|
|
|
*/
|
|
|
|
|
public final class PermGroup extends PermEntity {
|
2021-03-21 20:17:31 +01:00
|
|
|
|
/* package */ PermGroup(String name) {
|
|
|
|
|
super(name, EntityType.Group);
|
|
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
protected CachedGroup getBackendEntity() {
|
|
|
|
|
return Permissions.backendReader.getCachedGroup(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String getName() {
|
|
|
|
|
return getInternalName();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public List<PermGroup> getInheritances() {
|
|
|
|
|
return fromCachedGroups(getBackendEntity().inheritances);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public List<String> getInheritancesString() {
|
|
|
|
|
return getBackendEntity().inheritances.stream()
|
|
|
|
|
.map(cg -> cg.name)
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
}
|
2022-08-10 03:04:12 +02:00
|
|
|
|
|
2022-11-30 13:35:34 +01:00
|
|
|
|
/**
|
|
|
|
|
* Gets all the groups that directly inherits from this group.
|
|
|
|
|
* @return the groups that directly inherits from this group.
|
|
|
|
|
*/
|
|
|
|
|
public List<PermGroup> getInheritedGroups() {
|
|
|
|
|
CachedGroup thisCG = getBackendEntity();
|
|
|
|
|
return fromCachedGroups(Permissions.backendReader.getGroups().stream()
|
|
|
|
|
.filter(cg -> cg.inheritances.contains(thisCG))
|
|
|
|
|
.toList());
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 17:12:21 +01:00
|
|
|
|
/**
|
|
|
|
|
* 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<UUID> getInheritedPlayers(boolean recursive) throws DBException {
|
|
|
|
|
Set<UUID> players = new HashSet<>(getBackendEntity().getPlayersInGroup());
|
|
|
|
|
if (recursive) {
|
|
|
|
|
for (PermGroup inheritedGroups : getInheritedGroups()) {
|
|
|
|
|
players.addAll(inheritedGroups.getInheritedPlayers(true));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return players;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-10 03:04:12 +02:00
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
* @return true if this group is a default group, false otherwise.
|
|
|
|
|
*/
|
2021-03-21 20:17:31 +01:00
|
|
|
|
public boolean isDefault() {
|
|
|
|
|
return getBackendEntity().deflt;
|
|
|
|
|
}
|
2022-08-10 03:04:12 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets this group as a default group or not.
|
|
|
|
|
* All players that don’t explicitely inherit from at least one group will either start or stop implicitely
|
|
|
|
|
* inheriting from this group.
|
|
|
|
|
* @param deflt true to set this group as default, false to set is as not default.
|
|
|
|
|
*/
|
2021-03-21 20:17:31 +01:00
|
|
|
|
public void setDefault(boolean deflt) {
|
|
|
|
|
Permissions.backendWriter.setGroupDefault(name, deflt);
|
|
|
|
|
}
|
2022-08-10 03:04:12 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Makes this group inherit the provided group.
|
|
|
|
|
* @param group the name of the group to inherit from.
|
|
|
|
|
*/
|
2021-03-21 20:17:31 +01:00
|
|
|
|
public void addInheritance(String group) {
|
|
|
|
|
Permissions.backendWriter.addInheritance(name, type, group);
|
|
|
|
|
}
|
2022-08-10 03:04:12 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Makes this group inherit the provided group.
|
|
|
|
|
* @param group the group to inherit from.
|
|
|
|
|
*/
|
2021-03-21 20:17:31 +01:00
|
|
|
|
public void addInheritance(PermGroup group) {
|
|
|
|
|
addInheritance(group.name);
|
|
|
|
|
}
|
2022-08-10 03:04:12 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Makes this group stop inheriting from the provided group.
|
|
|
|
|
* @param group the name of the group to stop inheriting from.
|
|
|
|
|
*/
|
2021-03-21 20:17:31 +01:00
|
|
|
|
public void removeInheritance(String group) {
|
|
|
|
|
Permissions.backendWriter.removeInheritance(name, type, group);
|
|
|
|
|
}
|
2022-08-10 03:04:12 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Makes this group stop inheriting from the provided group.
|
|
|
|
|
* @param group the group to stop inheriting from.
|
|
|
|
|
*/
|
2021-03-21 20:17:31 +01:00
|
|
|
|
public void removeInheritance(PermGroup group) {
|
|
|
|
|
removeInheritance(group.name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* package */ static List<PermGroup> fromCachedGroups(List<CachedGroup> in) {
|
|
|
|
|
return in.stream()
|
|
|
|
|
.map(cg -> Permissions.getGroup(cg.name))
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
}
|
|
|
|
|
}
|