Skip to content

Commit 7deb601

Browse files
authored
fix(core): Check sender type before permissions in StandardHelpHandler (#761)
1 parent 8961d73 commit 7deb601

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

cloud-core/src/main/java/org/incendo/cloud/help/StandardHelpHandler.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
//
2424
package org.incendo.cloud.help;
2525

26+
import io.leangen.geantyref.GenericTypeReflector;
2627
import java.util.Collections;
2728
import java.util.HashSet;
2829
import java.util.LinkedList;
@@ -122,8 +123,7 @@ public StandardHelpHandler(
122123
.map(command -> CommandEntry.of(command, this.commandManager.commandSyntaxFormatter()
123124
.apply(query.sender(), command.components(), null)))
124125
.sorted()
125-
.filter(entry -> this.commandManager.testPermission(query.sender(),
126-
entry.command().commandPermission()).allowed())
126+
.filter(entry -> this.isAllowed(query.sender(), entry.command()))
127127
.collect(Collectors.toList())
128128
);
129129
}
@@ -143,7 +143,7 @@ public StandardHelpHandler(
143143

144144
if (head.component() != null && head.command() != null) {
145145
if (head.isLeaf() || index == queryFragments.size()) {
146-
if (this.commandManager.testPermission(query.sender(), head.command().commandPermission()).allowed()) {
146+
if (this.isAllowed(query.sender(), head.command())) {
147147
return VerboseCommandResult.of(
148148
query,
149149
CommandEntry.of(
@@ -193,8 +193,7 @@ public StandardHelpHandler(
193193

194194
final List<CommandComponent<C>> traversedNodesSub = new LinkedList<>(traversedNodes);
195195
if (child.component() == null || child.command() == null
196-
|| this.commandManager.testPermission(query.sender(),
197-
child.command().commandPermission()).allowed()
196+
|| this.isAllowed(query.sender(), child.command())
198197
) {
199198
traversedNodesSub.add(child.component());
200199
childSuggestions.add(this.commandManager.commandSyntaxFormatter()
@@ -221,7 +220,7 @@ public StandardHelpHandler(
221220
return this.commandManager.commands()
222221
.stream()
223222
.filter(this.commandFilter)
224-
.filter(command -> this.commandManager.testPermission(sender, command.commandPermission()).allowed())
223+
.filter(command -> this.isAllowed(sender, command))
225224
.map(command -> CommandEntry.of(
226225
command,
227226
this.commandManager.commandSyntaxFormatter()
@@ -230,6 +229,15 @@ public StandardHelpHandler(
230229
.collect(Collectors.toList());
231230
}
232231

232+
private boolean isAllowed(final C sender, final Command<C> command) {
233+
if (command.senderType().isPresent()) {
234+
if (!GenericTypeReflector.isSuperType(command.senderType().get().getType(), sender.getClass())) {
235+
return false;
236+
}
237+
}
238+
return this.commandManager.testPermission(sender, command.commandPermission()).allowed();
239+
}
240+
233241
/**
234242
* Checks using the predicate whether a command node or one of its children is visible
235243
*

cloud-kotlin/cloud-kotlin-extensions/src/test/kotlin/org/incendo/cloud/kotlin/CommandBuildingDSLTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class CommandBuildingDSLTest {
9595

9696
Assertions.assertEquals(
9797
manager.createHelpHandler()
98-
.queryRootIndex(TestCommandSender())
98+
.queryRootIndex(SpecificCommandSender())
9999
.entries()
100100
.map(CommandEntry<*>::syntax).sorted(),
101101
setOf(

0 commit comments

Comments
 (0)