Skip to content

Commit

Permalink
GH-390 Fix suggestions permission validation for children of command. (
Browse files Browse the repository at this point in the history
  • Loading branch information
Rollczi authored Apr 5, 2024
1 parent a9d7461 commit d0e69d5
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package dev.rollczi.litecommands.annotations.permission;

import dev.rollczi.litecommands.annotations.LiteTestSpec;
import dev.rollczi.litecommands.annotations.command.Command;
import dev.rollczi.litecommands.annotations.execute.Execute;
import static dev.rollczi.litecommands.unit.TestPlatformSender.*;
import org.junit.jupiter.api.Test;

class PermissionAnnotationSuggestionTest extends LiteTestSpec {

@Command(name = "test")
@Permission("test.permission")
static class TestCommand {

@Execute(name = "sub")
@Permission("test.permission.execute")
void execute() {}

}

@Test
void test() {
platform.suggest("test")
.assertSuggest();

platform.suggest(permitted("test.permission"), "test")
.assertSuggest("test");
}


@Test
void testSub() {
platform.suggest(permitted("test.permission"), "test ")
.assertSuggest();

platform.suggest(permitted("test.permission", "test.permission.execute"), "test ")
.assertSuggest("sub");

platform.suggest(permitted("test.permission.execute"), "test ")
.assertSuggest();
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.rollczi.litecommands.annotations.permission;

import dev.rollczi.litecommands.annotations.LiteTestSpec;
import dev.rollczi.litecommands.annotations.command.Command;
import dev.rollczi.litecommands.annotations.execute.Execute;
import dev.rollczi.litecommands.permission.MissingPermissions;
import org.junit.jupiter.api.Test;
Expand All @@ -12,9 +13,9 @@

class PermissionAnnotationTest extends LiteTestSpec {

@dev.rollczi.litecommands.annotations.command.Command(name = "test")
@Command(name = "test")
@Permission("test.permission")
static class Command {
static class TestCommand {

@Execute
@Permission("test.permission.execute")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ private <MATCHER extends SuggestionInputMatcher<MATCHER>> SuggestionResult sugge
CommandRoute<SENDER> commandRoute
) {
if (matcher.hasNoNextRouteAndArguments()) {
Flow flow = this.validatorService.validate(invocation, commandRoute);

if (flow.isTerminate() || flow.isStopCurrent()) {
return SuggestionResult.empty();
}

return SuggestionResult.of(commandRoute.names());
}

Expand All @@ -64,6 +70,10 @@ private <MATCHER extends SuggestionInputMatcher<MATCHER>> SuggestionResult sugge
String current = matcher.showNextRoute();

for (CommandRoute<SENDER> child : commandRoute.getChildren()) {
if (!this.isAnyExecutorValid(invocation, child)) {
continue;
}

for (String name : child.names()) {
if (!StringUtil.startsWithIgnoreCase(name, current)) {
continue;
Expand All @@ -76,6 +86,32 @@ private <MATCHER extends SuggestionInputMatcher<MATCHER>> SuggestionResult sugge
return all;
}

private boolean isAnyExecutorValid(Invocation<SENDER> invocation, CommandRoute<SENDER> route) {
Flow flow = this.validatorService.validate(invocation, route);

if (flow.isTerminate() || flow.isStopCurrent()) {
return false;
}

for (CommandExecutor<SENDER> executor : route.getExecutors()) {
Flow flowExecutor = this.validatorService.validate(invocation, executor);

if (flowExecutor.isTerminate() || flowExecutor.isStopCurrent()) {
continue;
}

return true;
}

for (CommandRoute<SENDER> child : route.getChildren()) {
if (this.isAnyExecutorValid(invocation, child)) {
return true;
}
}

return false;
}

public <MATCHER extends SuggestionInputMatcher<MATCHER>> SuggestionResult suggestExecutor(
Invocation<SENDER> invocation,
MATCHER matcher,
Expand Down

0 comments on commit d0e69d5

Please sign in to comment.