mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2025-01-15 12:51:32 +01:00
Include info about wildcards in permission check command output
This commit is contained in:
parent
c904ede972
commit
33e412f663
@ -42,6 +42,14 @@ public class WildcardProcessor extends AbstractPermissionProcessor implements Pe
|
|||||||
private static final String ROOT_WILDCARD = "*";
|
private static final String ROOT_WILDCARD = "*";
|
||||||
private static final String ROOT_WILDCARD_WITH_QUOTES = "'*'";
|
private static final String ROOT_WILDCARD_WITH_QUOTES = "'*'";
|
||||||
|
|
||||||
|
public static boolean isRootWildcard(String permission) {
|
||||||
|
return ROOT_WILDCARD.equals(permission) || ROOT_WILDCARD_WITH_QUOTES.equals(permission);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isWildcardPermission(String permission) {
|
||||||
|
return isRootWildcard(permission) || (permission.endsWith(WILDCARD_SUFFIX) && permission.length() > 2);
|
||||||
|
}
|
||||||
|
|
||||||
private Map<String, TristateResult> wildcardPermissions = Collections.emptyMap();
|
private Map<String, TristateResult> wildcardPermissions = Collections.emptyMap();
|
||||||
private TristateResult rootWildcardState = TristateResult.UNDEFINED;
|
private TristateResult rootWildcardState = TristateResult.UNDEFINED;
|
||||||
|
|
||||||
|
@ -25,6 +25,7 @@
|
|||||||
|
|
||||||
package me.lucko.luckperms.common.commands.generic.permission;
|
package me.lucko.luckperms.common.commands.generic.permission;
|
||||||
|
|
||||||
|
import me.lucko.luckperms.common.calculator.processor.WildcardProcessor;
|
||||||
import me.lucko.luckperms.common.calculator.result.TristateResult;
|
import me.lucko.luckperms.common.calculator.result.TristateResult;
|
||||||
import me.lucko.luckperms.common.command.CommandResult;
|
import me.lucko.luckperms.common.command.CommandResult;
|
||||||
import me.lucko.luckperms.common.command.abstraction.CommandException;
|
import me.lucko.luckperms.common.command.abstraction.CommandException;
|
||||||
@ -35,8 +36,10 @@ import me.lucko.luckperms.common.command.spec.CommandSpec;
|
|||||||
import me.lucko.luckperms.common.command.tabcomplete.TabCompleter;
|
import me.lucko.luckperms.common.command.tabcomplete.TabCompleter;
|
||||||
import me.lucko.luckperms.common.command.tabcomplete.TabCompletions;
|
import me.lucko.luckperms.common.command.tabcomplete.TabCompletions;
|
||||||
import me.lucko.luckperms.common.command.utils.ArgumentList;
|
import me.lucko.luckperms.common.command.utils.ArgumentList;
|
||||||
|
import me.lucko.luckperms.common.config.ConfigKeys;
|
||||||
import me.lucko.luckperms.common.locale.Message;
|
import me.lucko.luckperms.common.locale.Message;
|
||||||
import me.lucko.luckperms.common.model.PermissionHolder;
|
import me.lucko.luckperms.common.model.PermissionHolder;
|
||||||
|
import me.lucko.luckperms.common.node.AbstractNode;
|
||||||
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
|
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
|
||||||
import me.lucko.luckperms.common.query.QueryOptionsImpl;
|
import me.lucko.luckperms.common.query.QueryOptionsImpl;
|
||||||
import me.lucko.luckperms.common.sender.Sender;
|
import me.lucko.luckperms.common.sender.Sender;
|
||||||
@ -48,12 +51,14 @@ import net.luckperms.api.context.ImmutableContextSet;
|
|||||||
import net.luckperms.api.model.PermissionHolder.Identifier;
|
import net.luckperms.api.model.PermissionHolder.Identifier;
|
||||||
import net.luckperms.api.node.Node;
|
import net.luckperms.api.node.Node;
|
||||||
import net.luckperms.api.node.metadata.types.InheritanceOriginMetadata;
|
import net.luckperms.api.node.metadata.types.InheritanceOriginMetadata;
|
||||||
|
import net.luckperms.api.node.types.PermissionNode;
|
||||||
import net.luckperms.api.node.types.RegexPermissionNode;
|
import net.luckperms.api.node.types.RegexPermissionNode;
|
||||||
import net.luckperms.api.query.QueryOptions;
|
import net.luckperms.api.query.QueryOptions;
|
||||||
import net.luckperms.api.util.Tristate;
|
import net.luckperms.api.util.Tristate;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class PermissionCheck extends GenericChildCommand {
|
public class PermissionCheck extends GenericChildCommand {
|
||||||
public PermissionCheck() {
|
public PermissionCheck() {
|
||||||
@ -72,21 +77,19 @@ public class PermissionCheck extends GenericChildCommand {
|
|||||||
// accumulate nodes
|
// accumulate nodes
|
||||||
List<Node> own = new ArrayList<>();
|
List<Node> own = new ArrayList<>();
|
||||||
List<Node> inherited = new ArrayList<>();
|
List<Node> inherited = new ArrayList<>();
|
||||||
|
List<Node> wildcards = new ArrayList<>();
|
||||||
|
|
||||||
List<Node> resolved = target.resolveInheritedNodes(QueryOptionsImpl.DEFAULT_NON_CONTEXTUAL);
|
List<Node> resolved = target.resolveInheritedNodes(QueryOptionsImpl.DEFAULT_NON_CONTEXTUAL);
|
||||||
for (Node n : resolved) {
|
for (Node n : resolved) {
|
||||||
if (!matches(node, n)) {
|
if (matches(node, n, plugin)) {
|
||||||
continue;
|
if (isInherited(n, target)) {
|
||||||
|
inherited.add(n);
|
||||||
|
} else {
|
||||||
|
own.add(n);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
if (matchesWildcard(node, n, plugin)) {
|
||||||
Identifier origin = n.getMetadata(InheritanceOriginMetadata.KEY)
|
wildcards.add(n);
|
||||||
.map(InheritanceOriginMetadata::getOrigin)
|
|
||||||
.orElse(null);
|
|
||||||
|
|
||||||
if (origin == null || target.getIdentifier().equals(origin)) {
|
|
||||||
own.add(n);
|
|
||||||
} else {
|
|
||||||
inherited.add(n);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,7 +99,7 @@ public class PermissionCheck extends GenericChildCommand {
|
|||||||
Message.PERMISSION_CHECK_INFO_NOT_DIRECTLY.send(sender, target, node);
|
Message.PERMISSION_CHECK_INFO_NOT_DIRECTLY.send(sender, target, node);
|
||||||
} else {
|
} else {
|
||||||
for (Node n : own) {
|
for (Node n : own) {
|
||||||
Message.PERMISSION_CHECK_INFO_DIRECTLY.send(sender, target, node, Tristate.of(n.getValue()), n.getContexts());
|
Message.PERMISSION_CHECK_INFO_DIRECTLY.send(sender, target, n.getKey(), Tristate.of(n.getValue()), n.getContexts());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (inherited.isEmpty()) {
|
if (inherited.isEmpty()) {
|
||||||
@ -104,7 +107,15 @@ public class PermissionCheck extends GenericChildCommand {
|
|||||||
} else {
|
} else {
|
||||||
for (Node n : inherited) {
|
for (Node n : inherited) {
|
||||||
String origin = n.metadata(InheritanceOriginMetadata.KEY).getOrigin().getName();
|
String origin = n.metadata(InheritanceOriginMetadata.KEY).getOrigin().getName();
|
||||||
Message.PERMISSION_CHECK_INFO_INHERITED.send(sender, target, node, Tristate.of(n.getValue()), n.getContexts(), origin);
|
Message.PERMISSION_CHECK_INFO_INHERITED.send(sender, target, n.getKey(), Tristate.of(n.getValue()), n.getContexts(), origin);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (Node n : wildcards) {
|
||||||
|
if (isInherited(n, target)) {
|
||||||
|
String origin = n.metadata(InheritanceOriginMetadata.KEY).getOrigin().getName();
|
||||||
|
Message.PERMISSION_CHECK_INFO_INHERITED.send(sender, target, n.getKey(), Tristate.of(n.getValue()), n.getContexts(), origin);
|
||||||
|
} else {
|
||||||
|
Message.PERMISSION_CHECK_INFO_DIRECTLY.send(sender, target, n.getKey(), Tristate.of(n.getValue()), n.getContexts());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -149,9 +160,65 @@ public class PermissionCheck extends GenericChildCommand {
|
|||||||
.complete(args);
|
.complete(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean matches(String permission, Node node) {
|
private static boolean isInherited(Node n, PermissionHolder target) {
|
||||||
return node.getKey().equals(permission) ||
|
Identifier origin = n.getMetadata(InheritanceOriginMetadata.KEY)
|
||||||
node.resolveShorthand().contains(permission) ||
|
.map(InheritanceOriginMetadata::getOrigin)
|
||||||
node instanceof RegexPermissionNode && ((RegexPermissionNode) node).getPattern().map(p -> p.matcher(permission).matches()).orElse(false);
|
.orElse(null);
|
||||||
|
|
||||||
|
return origin != null && !target.getIdentifier().equals(origin);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean matchesWildcard(String permission, Node node, LuckPermsPlugin plugin) {
|
||||||
|
if (plugin.getConfiguration().get(ConfigKeys.APPLYING_WILDCARDS)) {
|
||||||
|
if (node instanceof PermissionNode && ((PermissionNode) node).isWildcard()) {
|
||||||
|
String key = node.getKey();
|
||||||
|
if (WildcardProcessor.isRootWildcard(key)) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
// luckperms.* becomes luckperms.
|
||||||
|
String wildcardBody = key.substring(0, key.length() - 1);
|
||||||
|
if (permission.startsWith(wildcardBody)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (plugin.getConfiguration().get(ConfigKeys.APPLYING_WILDCARDS_SPONGE)) {
|
||||||
|
String key = node.getKey();
|
||||||
|
|
||||||
|
int endIndex = key.lastIndexOf(AbstractNode.NODE_SEPARATOR);
|
||||||
|
if (endIndex > 0) {
|
||||||
|
String wildcardBody = key.substring(0, endIndex);
|
||||||
|
if (permission.startsWith(wildcardBody)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean matches(String permission, Node node, LuckPermsPlugin plugin) {
|
||||||
|
if (node.getKey().equals(permission)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (plugin.getConfiguration().get(ConfigKeys.APPLYING_SHORTHAND)) {
|
||||||
|
if (node.resolveShorthand().contains(permission)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (plugin.getConfiguration().get(ConfigKeys.APPLYING_REGEX)) {
|
||||||
|
if (node instanceof RegexPermissionNode) {
|
||||||
|
Pattern pattern = ((RegexPermissionNode) node).getPattern().orElse(null);
|
||||||
|
if (pattern != null && pattern.matcher(permission).matches()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -56,7 +56,6 @@ public abstract class AbstractNode<N extends ScopedNode<N, B>, B extends NodeBui
|
|||||||
*/
|
*/
|
||||||
public static final char NODE_SEPARATOR = '.';
|
public static final char NODE_SEPARATOR = '.';
|
||||||
public static final String NODE_SEPARATOR_STRING = String.valueOf(NODE_SEPARATOR);
|
public static final String NODE_SEPARATOR_STRING = String.valueOf(NODE_SEPARATOR);
|
||||||
public static final int NODE_SEPARATOR_CODE = Character.getNumericValue(NODE_SEPARATOR);
|
|
||||||
|
|
||||||
// node attributes
|
// node attributes
|
||||||
protected final String key;
|
protected final String key;
|
||||||
|
@ -50,7 +50,7 @@ public class Permission extends AbstractNode<PermissionNode, PermissionNode.Buil
|
|||||||
|
|
||||||
public Permission(String permission, boolean value, long expireAt, ImmutableContextSet contexts, Map<NodeMetadataKey<?>, Object> metadata) {
|
public Permission(String permission, boolean value, long expireAt, ImmutableContextSet contexts, Map<NodeMetadataKey<?>, Object> metadata) {
|
||||||
super(permission, value, expireAt, contexts, metadata);
|
super(permission, value, expireAt, contexts, metadata);
|
||||||
this.wildcardLevel = permission.endsWith(WildcardProcessor.WILDCARD_SUFFIX) ? permission.chars().filter(num -> num == NODE_SEPARATOR_CODE).sum() : -1;
|
this.wildcardLevel = WildcardProcessor.isWildcardPermission(permission) ? permission.chars().filter(num -> num == NODE_SEPARATOR).sum() : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
Reference in New Issue
Block a user