Change behaviour of resolveInheritanceTree when 'resolve inheritance' flag is false

This commit is contained in:
Luck 2020-06-12 20:34:57 +01:00
parent 0bef0902c0
commit 2bb1aa7511
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
2 changed files with 17 additions and 19 deletions

View File

@ -33,6 +33,7 @@ import net.luckperms.api.model.group.Group;
import net.luckperms.api.model.user.User;
import net.luckperms.api.node.Node;
import net.luckperms.api.node.NodeType;
import net.luckperms.api.query.Flag;
import net.luckperms.api.query.QueryOptions;
import org.checkerframework.checker.nullness.qual.NonNull;
@ -277,10 +278,14 @@ public interface PermissionHolder {
@NonNull SortedSet<Node> resolveDistinctInheritedNodes(@NonNull QueryOptions queryOptions);
/**
* Gets a collection of the {@link Group}s this holder inherits nodes from, both directly
* and indirectly (through directly inherited groups).
* Gets a collection of the {@link Group}s this holder inherits nodes from.
*
* <p>It effectively resolves the whole "inheritance tree".</p>
* <p>If {@link Flag#RESOLVE_INHERITANCE} is set, this will include holders inherited from both
* directly and indirectly (through directly inherited groups). It will effectively resolve the
* whole "inheritance tree".</p>
*
* <p>If {@link Flag#RESOLVE_INHERITANCE} is not set, then the traversal will only go one
* level up the inheritance tree, and return only directly inherited groups.</p>
*
* <p>The collection will be ordered according to the platforms inheritance rules. The groups
* which are inherited from first will appear earlier in the list.</p>

View File

@ -60,7 +60,6 @@ import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
@ -351,26 +350,20 @@ public abstract class PermissionHolder {
@SuppressWarnings({"unchecked", "rawtypes"})
public List<Group> resolveInheritanceTree(QueryOptions queryOptions) {
if (!queryOptions.flag(Flag.RESOLVE_INHERITANCE)) {
return Collections.emptyList();
}
InheritanceGraph graph = this.plugin.getInheritanceGraphFactory().getGraph(queryOptions);
// perform a full traversal of the inheritance tree
List<PermissionHolder> traversal = new ArrayList<>();
Iterables.addAll(traversal, graph.traverse(this));
List<PermissionHolder> inheritanceTree = new ArrayList<>();
// remove 'this' (the start node) - will usually be at traversal[0],
// but not always due to the possibility of post-traversal sorts!
if (traversal.get(0) == this) {
traversal.remove(0);
if (queryOptions.flag(Flag.RESOLVE_INHERITANCE)) {
Iterables.addAll(inheritanceTree, graph.traverse(this));
inheritanceTree.remove(this);
} else {
traversal.remove(this);
// if RESOLVE_INHERITANCE is not set, only go up by one level
Iterables.addAll(inheritanceTree, graph.successors(this));
}
// ensure our traversal now only consists of groups
for (PermissionHolder permissionHolder : traversal) {
// ensure our tree now only consists of groups
for (PermissionHolder permissionHolder : inheritanceTree) {
if (!(permissionHolder instanceof Group)) {
throw new IllegalStateException("Non-group object in inheritance tree: " + permissionHolder);
}
@ -378,7 +371,7 @@ public abstract class PermissionHolder {
// cast List<PermissionHolder> to List<Group>
// this feels a bit dirty but it works & avoids needless copying!
return (List) traversal;
return (List) inheritanceTree;
}
public Map<String, Boolean> exportPermissions(QueryOptions queryOptions, boolean convertToLowercase, boolean resolveShorthand) {