Skip to content

Commit

Permalink
fix(core): Check sender type before permissions in StandardHelpHandler (
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla authored Aug 8, 2024
1 parent 8961d73 commit 7deb601
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
//
package org.incendo.cloud.help;

import io.leangen.geantyref.GenericTypeReflector;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
Expand Down Expand Up @@ -122,8 +123,7 @@ public StandardHelpHandler(
.map(command -> CommandEntry.of(command, this.commandManager.commandSyntaxFormatter()
.apply(query.sender(), command.components(), null)))
.sorted()
.filter(entry -> this.commandManager.testPermission(query.sender(),
entry.command().commandPermission()).allowed())
.filter(entry -> this.isAllowed(query.sender(), entry.command()))
.collect(Collectors.toList())
);
}
Expand All @@ -143,7 +143,7 @@ public StandardHelpHandler(

if (head.component() != null && head.command() != null) {
if (head.isLeaf() || index == queryFragments.size()) {
if (this.commandManager.testPermission(query.sender(), head.command().commandPermission()).allowed()) {
if (this.isAllowed(query.sender(), head.command())) {
return VerboseCommandResult.of(
query,
CommandEntry.of(
Expand Down Expand Up @@ -193,8 +193,7 @@ public StandardHelpHandler(

final List<CommandComponent<C>> traversedNodesSub = new LinkedList<>(traversedNodes);
if (child.component() == null || child.command() == null
|| this.commandManager.testPermission(query.sender(),
child.command().commandPermission()).allowed()
|| this.isAllowed(query.sender(), child.command())
) {
traversedNodesSub.add(child.component());
childSuggestions.add(this.commandManager.commandSyntaxFormatter()
Expand All @@ -221,7 +220,7 @@ public StandardHelpHandler(
return this.commandManager.commands()
.stream()
.filter(this.commandFilter)
.filter(command -> this.commandManager.testPermission(sender, command.commandPermission()).allowed())
.filter(command -> this.isAllowed(sender, command))
.map(command -> CommandEntry.of(
command,
this.commandManager.commandSyntaxFormatter()
Expand All @@ -230,6 +229,15 @@ public StandardHelpHandler(
.collect(Collectors.toList());
}

private boolean isAllowed(final C sender, final Command<C> command) {
if (command.senderType().isPresent()) {
if (!GenericTypeReflector.isSuperType(command.senderType().get().getType(), sender.getClass())) {
return false;
}
}
return this.commandManager.testPermission(sender, command.commandPermission()).allowed();
}

/**
* Checks using the predicate whether a command node or one of its children is visible
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class CommandBuildingDSLTest {

Assertions.assertEquals(
manager.createHelpHandler()
.queryRootIndex(TestCommandSender())
.queryRootIndex(SpecificCommandSender())
.entries()
.map(CommandEntry<*>::syntax).sorted(),
setOf(
Expand Down

0 comments on commit 7deb601

Please sign in to comment.