2022-07-20 13:18:57 +02:00
|
|
|
package fr.pandacube.lib.permissions;
|
2021-03-21 20:17:31 +01:00
|
|
|
|
|
|
|
import java.util.List;
|
2021-04-17 00:06:21 +02:00
|
|
|
import java.util.Map;
|
2021-03-21 20:17:31 +01:00
|
|
|
import java.util.Objects;
|
2022-04-10 01:11:12 +02:00
|
|
|
import java.util.OptionalLong;
|
2021-03-21 20:17:31 +01:00
|
|
|
import java.util.Set;
|
2022-04-10 01:11:12 +02:00
|
|
|
import java.util.stream.LongStream;
|
2021-03-21 20:17:31 +01:00
|
|
|
|
2022-07-20 13:18:57 +02:00
|
|
|
import fr.pandacube.lib.chat.ChatUtil.DisplayTreeNode;
|
|
|
|
import fr.pandacube.lib.permissions.PermissionsCachedBackendReader.CachedEntity;
|
|
|
|
import fr.pandacube.lib.permissions.SQLPermissions.EntityType;
|
|
|
|
import fr.pandacube.lib.util.Log;
|
2021-03-21 20:17:31 +01:00
|
|
|
|
|
|
|
public abstract class PermEntity {
|
|
|
|
protected final String name;
|
|
|
|
protected final EntityType type;
|
|
|
|
protected PermEntity(String n, EntityType t) {
|
|
|
|
name = n; type = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract CachedEntity getBackendEntity();
|
|
|
|
public abstract List<PermGroup> getInheritances();
|
|
|
|
public abstract List<String> getInheritancesString();
|
|
|
|
public abstract String getName();
|
|
|
|
|
|
|
|
public String getInternalName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tells if the current entity inherits directly or indirectly from the specified group
|
|
|
|
* @param group the group to search for
|
|
|
|
* @param recursive true to search in the inheritance tree, or false to search only in the inheritance list of the current entity.
|
|
|
|
*/
|
|
|
|
public boolean inheritsFromGroup(String group, boolean recursive) {
|
|
|
|
if (group == null)
|
|
|
|
return false;
|
2022-07-10 00:55:56 +02:00
|
|
|
return getInheritances().stream().anyMatch(g -> g.name.equals(group) || (recursive && g.inheritsFromGroup(group, true)));
|
2021-03-21 20:17:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getPrefix() {
|
|
|
|
return Permissions.resolver.getEffectivePrefix(name, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public String getSelfPrefix() {
|
|
|
|
return getBackendEntity().getSelfPrefix();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public DisplayTreeNode debugPrefix() {
|
|
|
|
return Permissions.resolver.debugPrefix(name, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void setSelfPrefix(String prefix) {
|
|
|
|
Permissions.backendWriter.setSelfPrefix(name, type, prefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public String getSuffix() {
|
|
|
|
return Permissions.resolver.getEffectiveSuffix(name, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getSelfSuffix() {
|
|
|
|
return getBackendEntity().getSelfSuffix();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public DisplayTreeNode debugSuffix() {
|
|
|
|
return Permissions.resolver.debugSuffix(name, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void setSelfSuffix(String suffix) {
|
|
|
|
Permissions.backendWriter.setSelfSuffix(name, type, suffix);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-17 00:06:21 +02:00
|
|
|
public Map<String, Boolean> listEffectivePermissions() {
|
2021-07-25 20:14:06 +02:00
|
|
|
return listEffectivePermissions(null, null);
|
2021-04-17 00:06:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public Map<String, Boolean> listEffectivePermissions(String server) {
|
2021-07-25 20:14:06 +02:00
|
|
|
return listEffectivePermissions(server, null);
|
2021-04-17 00:06:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public Map<String, Boolean> listEffectivePermissions(String server, String world) {
|
|
|
|
return Permissions.resolver.getEffectivePermissionList(name, type, server, world);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-10 01:11:12 +02:00
|
|
|
public LongStream getPermissionRangeValues(String permissionPrefix) {
|
|
|
|
return getPermissionRangeValues(permissionPrefix, null, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public LongStream getPermissionRangeValues(String permissionPrefix, String server) {
|
|
|
|
return getPermissionRangeValues(permissionPrefix, server, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public LongStream getPermissionRangeValues(String permissionPrefix, String server, String world) {
|
|
|
|
String prefixWithEndingDot = permissionPrefix.endsWith(".") ? permissionPrefix : (permissionPrefix + ".");
|
|
|
|
int prefixLength = prefixWithEndingDot.length();
|
|
|
|
return listEffectivePermissions(server, world).entrySet().stream()
|
2022-07-10 00:55:56 +02:00
|
|
|
.filter(Map.Entry::getValue) // permission must be positive
|
|
|
|
.map(Map.Entry::getKey) // keep only the permission node (key), since the value is always true
|
2022-04-10 01:11:12 +02:00
|
|
|
.filter(p -> p.startsWith(prefixWithEndingDot)) // keep only relevant permissions
|
|
|
|
.map(p -> p.substring(prefixLength)) // keep only what is after the prefix
|
|
|
|
.map(suffix -> { // convert to long
|
|
|
|
try {
|
|
|
|
return Long.parseLong(suffix);
|
|
|
|
}
|
|
|
|
catch (NumberFormatException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
})
|
2022-07-10 00:55:56 +02:00
|
|
|
.filter(Objects::nonNull)
|
2022-04-10 01:11:12 +02:00
|
|
|
.mapToLong(longSuffix -> longSuffix)
|
|
|
|
.sorted();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public OptionalLong getPermissionRangeMax(String permissionPrefix) {
|
|
|
|
return getPermissionRangeMax(permissionPrefix, null, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public OptionalLong getPermissionRangeMax(String permissionPrefix, String server) {
|
|
|
|
return getPermissionRangeMax(permissionPrefix, server, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public OptionalLong getPermissionRangeMax(String permissionPrefix, String server, String world) {
|
|
|
|
return getPermissionRangeValues(permissionPrefix, server, world).max();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-21 20:17:31 +01:00
|
|
|
public Boolean hasPermission(String permission) {
|
2021-07-25 20:14:06 +02:00
|
|
|
return hasPermission(permission, null, null);
|
2021-03-21 20:17:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public Boolean hasPermission(String permission, String server) {
|
2021-07-25 20:14:06 +02:00
|
|
|
return hasPermission(permission, server, null);
|
2021-03-21 20:17:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public Boolean hasPermission(String permission, String server, String world) {
|
2021-07-25 20:14:06 +02:00
|
|
|
Boolean ret = Permissions.resolver.getEffectivePermission(name, type, permission, server, world);
|
2021-08-15 03:26:50 +02:00
|
|
|
Log.debug("[Perm] For " + type.toString().toLowerCase() + " " + getName() + ", '" + permission + "' is " + ret);
|
2021-07-25 20:14:06 +02:00
|
|
|
return ret;
|
2021-03-21 20:17:31 +01:00
|
|
|
}
|
|
|
|
|
2021-08-15 03:26:50 +02:00
|
|
|
public boolean hasPermissionOr(String permission, String server, String world, boolean deflt) {
|
|
|
|
Boolean ret = hasPermission(permission, server, world);
|
|
|
|
return ret != null ? ret : deflt;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasPermissionExpression(String permExpression, String server, String world) {
|
|
|
|
return PermissionExpressionParser.evaluate(permExpression, p -> hasPermissionOr(p, server, world, false));
|
|
|
|
}
|
|
|
|
|
2021-03-21 20:17:31 +01:00
|
|
|
|
|
|
|
public DisplayTreeNode debugPermission(String permission) {
|
2021-07-25 20:14:06 +02:00
|
|
|
return debugPermission(permission, null, null);
|
2021-03-21 20:17:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public DisplayTreeNode debugPermission(String permission, String server) {
|
2021-07-25 20:14:06 +02:00
|
|
|
return debugPermission(permission, server, null);
|
2021-03-21 20:17:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public DisplayTreeNode debugPermission(String permission, String server, String world) {
|
|
|
|
return Permissions.resolver.debugPermission(name, type, permission, server, world);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void addSelfPermission(String permission) {
|
2021-07-25 20:14:06 +02:00
|
|
|
addSelfPermission(permission, null, null);
|
2021-03-21 20:17:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void addSelfPermission(String permission, String server) {
|
2021-07-25 20:14:06 +02:00
|
|
|
addSelfPermission(permission, server, null);
|
2021-03-21 20:17:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void addSelfPermission(String permission, String server, String world) {
|
|
|
|
Permissions.backendWriter.addSelfPermission(name, type, permission, server, world);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void removeSelfPermission(String permission) {
|
2021-07-25 20:14:06 +02:00
|
|
|
removeSelfPermission(permission, null, null);
|
2021-03-21 20:17:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void removeSelfPermission(String permission, String server) {
|
2021-07-25 20:14:06 +02:00
|
|
|
removeSelfPermission(permission, server, null);
|
2021-03-21 20:17:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void removeSelfPermission(String permission, String server, String world) {
|
|
|
|
Permissions.backendWriter.removeSelfPermission(name, type, permission, server, world);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getSelfPermissionsCount() {
|
|
|
|
return getSelfPermissionsServerWorldKeys().stream()
|
|
|
|
.mapToInt(key -> getSelfPermissions(key.server, key.world).size())
|
|
|
|
.sum();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Set<ServerWorldKey> getSelfPermissionsServerWorldKeys() {
|
|
|
|
return getBackendEntity().getSelfPermissionsServerWorldKeys();
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<String> getSelfPermissions() {
|
2021-07-25 20:14:06 +02:00
|
|
|
return getSelfPermissions(null, null);
|
2021-03-21 20:17:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public List<String> getSelfPermissions(String server) {
|
2021-07-25 20:14:06 +02:00
|
|
|
return getSelfPermissions(server, null);
|
2021-03-21 20:17:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public List<String> getSelfPermissions(String server, String world) {
|
|
|
|
return getBackendEntity().getSelfPermissions(server, world);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
2022-07-10 00:55:56 +02:00
|
|
|
return obj instanceof PermEntity o
|
|
|
|
&& Objects.equals(name, o.name)
|
|
|
|
&& type == o.type;
|
2021-03-21 20:17:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return Objects.hash(name, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|