Skip to content

Commit

Permalink
fix: check flag permissions before adding them to the sytnax formatter
Browse files Browse the repository at this point in the history
pretty ugly solution
  • Loading branch information
broccolai committed Aug 8, 2024
1 parent 8961d73 commit 5e189e9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.apiguardian.api.API;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
Expand Down Expand Up @@ -76,7 +77,7 @@ public StandardCommandSyntaxFormatter(final @NonNull CommandManager<C> manager)
final @NonNull List<@NonNull CommandComponent<C>> commandComponents,
final @Nullable CommandNode<C> node
) {
return this.apply(commandComponents, node, n -> {
return this.apply(sender, commandComponents, node, n -> {
if (sender == null) {
return true;
}
Expand All @@ -95,7 +96,9 @@ public StandardCommandSyntaxFormatter(final @NonNull CommandManager<C> manager)
});
}

@SuppressWarnings("unchecked")
private @NonNull String apply(
final @Nullable C sender,
final @NonNull List<@NonNull CommandComponent<C>> commandComponents,
final @Nullable CommandNode<C> node,
final @NonNull Predicate<@NonNull CommandNode<C>> filter
Expand All @@ -110,7 +113,7 @@ public StandardCommandSyntaxFormatter(final @NonNull CommandManager<C> manager)
final AggregateParser<?, ?> aggregateParser = (AggregateParser<?, ?>) commandComponent.parser();
formattingInstance.appendAggregate(commandComponent, aggregateParser);
} else if (commandComponent.type() == CommandComponent.ComponentType.FLAG) {
formattingInstance.appendFlag((CommandFlagParser<?>) commandComponent.parser());
formattingInstance.appendFlag(this.filterFlagsByPermission(sender, (CommandFlagParser<C>) commandComponent.parser()));
} else {
if (commandComponent.required()) {
formattingInstance.appendRequired(commandComponent);
Expand Down Expand Up @@ -163,8 +166,12 @@ public StandardCommandSyntaxFormatter(final @NonNull CommandManager<C> manager)
formattingInstance.appendBlankSpace();
formattingInstance.appendAggregate(component, aggregateParser);
} else if (component.type() == CommandComponent.ComponentType.FLAG) {
formattingInstance.appendBlankSpace();
formattingInstance.appendFlag((CommandFlagParser<?>) component.parser());
final List<CommandFlag<?>> flags = this.filterFlagsByPermission(sender, (CommandFlagParser<C>) component.parser());

if (!flags.isEmpty()) {
formattingInstance.appendBlankSpace();
formattingInstance.appendFlag(flags);
}
} else if (component.type() == CommandComponent.ComponentType.LITERAL) {
formattingInstance.appendBlankSpace();
formattingInstance.appendLiteral(component);
Expand All @@ -190,6 +197,16 @@ public StandardCommandSyntaxFormatter(final @NonNull CommandManager<C> manager)
return new FormattingInstance();
}

private List<CommandFlag<?>> filterFlagsByPermission(
final @Nullable C sender,
final @NonNull CommandFlagParser<C> flagParser
) {
return flagParser
.flags()
.stream()
.filter(flag -> sender == null || this.manager.testPermission(sender, flag.permission()).allowed())
.collect(Collectors.toList());
}

/**
* Instance that is used when building command syntax
Expand Down Expand Up @@ -251,14 +268,12 @@ public void appendAggregate(
/**
* Appends a flag argument
*
* @param flagParser flag parser
* @param flags the flags to append
*/
public void appendFlag(final @NonNull CommandFlagParser<?> flagParser) {
public void appendFlag(final @NonNull Iterable<CommandFlag<?>> flags) {
this.builder.append(this.optionalPrefix());

final Iterator<CommandFlag<?>> flagIterator = flagParser
.flags()
.iterator();
final Iterator<CommandFlag<?>> flagIterator = flags.iterator();

while (flagIterator.hasNext()) {
final CommandFlag<?> flag = flagIterator.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.incendo.cloud.execution.ExecutionCoordinator;
import org.incendo.cloud.internal.CommandNode;
import org.incendo.cloud.internal.CommandRegistrationHandler;
import org.incendo.cloud.parser.flag.CommandFlag;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -106,6 +107,25 @@ void respectsPermissions() {
assertThat(formatted).isEqualTo(" all");
}

@Test
void respectsFlagPermissions() {
final Command.Builder<TestCommandSender> root = this.manager.commandBuilder("root");
this.manager.command(
root.flag(CommandFlag.builder("some_flag").withPermission("some_permission").build())
.handler(ctx -> {})
);

final CommandNode<TestCommandSender> rootNode = this.manager.commandTree().getNamedNode("root");

final String permittedFormatted = this.formatter.apply(
new TestCommandSender("some_permission"), Collections.emptyList(), rootNode);
assertThat(permittedFormatted).isEqualTo(" [--some_flag]");

final String formatted = this.formatter.apply(
new TestCommandSender(), Collections.emptyList(), rootNode);
assertThat(formatted).isEqualTo("");
}

static final class SpecificTestCommandSender extends TestCommandSender {

}
Expand Down

0 comments on commit 5e189e9

Please sign in to comment.