From 7d46e64ed37f51886092f52baa6fbacf64ff46b9 Mon Sep 17 00:00:00 2001 From: Zach Levis Date: Fri, 15 Jan 2021 12:14:41 -0800 Subject: [PATCH] :sparkles: Add logical operators to permissions These operators allow some basic combination of permissions to occur, which expands what can be easily done with permissions definitions. --- CHANGELOG.md | 1 + .../commandframework/CommandManager.java | 31 +++--- .../permission/AndPermission.java | 96 +++++++++++++++++++ .../permission/CommandPermission.java | 67 ++++++++++++- .../permission/OrPermission.java | 17 ++-- .../CommandPermissionTest.java | 30 +++++- .../commandframework/TestCommandSender.java | 25 +++++ 7 files changed, 240 insertions(+), 27 deletions(-) create mode 100644 cloud-core/src/main/java/cloud/commandframework/permission/AndPermission.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 164c15cd..ab709951 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - More abstract description concept ([#207](https://github.com/Incendo/cloud/pull/207)) - Predicate permissions ([#210](https://github.com/Incendo/cloud/pull/210)) - Injection services ([#211](https://github.com/Incendo/cloud/pull/211)) + - Logical `AND` and `OR` operations for `CommandPermission`s ([#213](https://github.com/Incendo/cloud/pull/213)) ### Changed - Allow command argument names to include `_` and `-` ([#186](https://github.com/Incendo/cloud/pull/186)) diff --git a/cloud-core/src/main/java/cloud/commandframework/CommandManager.java b/cloud-core/src/main/java/cloud/commandframework/CommandManager.java index cc142263..db07fd57 100644 --- a/cloud-core/src/main/java/cloud/commandframework/CommandManager.java +++ b/cloud-core/src/main/java/cloud/commandframework/CommandManager.java @@ -52,6 +52,7 @@ import cloud.commandframework.execution.preprocessor.CommandPreprocessor; import cloud.commandframework.internal.CommandInputTokenizer; import cloud.commandframework.internal.CommandRegistrationHandler; import cloud.commandframework.meta.CommandMeta; +import cloud.commandframework.permission.AndPermission; import cloud.commandframework.permission.CommandPermission; import cloud.commandframework.permission.OrPermission; import cloud.commandframework.permission.Permission; @@ -283,24 +284,30 @@ public abstract class CommandManager { final @NonNull C sender, final @NonNull CommandPermission permission ) { - if (permission.toString().isEmpty()) { - return true; - } if (permission instanceof Permission) { - return hasPermission(sender, permission.toString()); - } - if (permission instanceof PredicatePermission) { + if (permission.toString().isEmpty()) { + return true; + } + return this.hasPermission(sender, permission.toString()); + } else if (permission instanceof PredicatePermission) { return ((PredicatePermission) permission).hasPermission(sender); - } - for (final CommandPermission innerPermission : permission.getPermissions()) { - final boolean hasPermission = this.hasPermission(sender, innerPermission); - if (permission instanceof OrPermission) { - if (hasPermission) { + } else if (permission instanceof OrPermission) { + for (final CommandPermission innerPermission : permission.getPermissions()) { + if (this.hasPermission(sender, innerPermission)) { return true; } } + return false; + } else if (permission instanceof AndPermission) { + for (final CommandPermission innerPermission : permission.getPermissions()) { + if (!this.hasPermission(sender, innerPermission)) { + return false; + } + } + return true; } - return false; + + throw new IllegalArgumentException("Unknown permission type " + permission.getClass()); } /** diff --git a/cloud-core/src/main/java/cloud/commandframework/permission/AndPermission.java b/cloud-core/src/main/java/cloud/commandframework/permission/AndPermission.java new file mode 100644 index 00000000..3c2ac068 --- /dev/null +++ b/cloud-core/src/main/java/cloud/commandframework/permission/AndPermission.java @@ -0,0 +1,96 @@ +// +// MIT License +// +// Copyright (c) 2020 Alexander Söderberg & Contributors +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// +package cloud.commandframework.permission; + +import org.checkerframework.checker.nullness.qual.NonNull; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Objects; +import java.util.Set; + +/** + * Accepts if every single permission is accepted. + */ +public final class AndPermission implements CommandPermission { + + private final Set permissions; + + AndPermission(final @NonNull Set permissions) { + this.permissions = Collections.unmodifiableSet(permissions); + } + + /** + * Create a new OR permission + * + * @param permissions Permissions to join + * @return Constructed permission + */ + public static @NonNull CommandPermission of(final @NonNull Collection permissions) { + final Set permissionSet = new HashSet<>(); + for (final CommandPermission permission : permissions) { + permissionSet.addAll(permission.getPermissions()); + } + return new AndPermission(permissionSet); + } + + @Override + public @NonNull Collection<@NonNull CommandPermission> getPermissions() { + return this.permissions; + } + + @Override + public String toString() { + final StringBuilder stringBuilder = new StringBuilder(); + final Iterator iterator = this.permissions.iterator(); + while (iterator.hasNext()) { + final CommandPermission permission = iterator.next(); + stringBuilder.append('(').append(permission.toString()).append(')'); + if (iterator.hasNext()) { + stringBuilder.append(" & "); + } + } + return stringBuilder.toString(); + } + + @Override + public boolean equals(final Object o) { + if (this == o) { + return true; + } + if (o == null || this.getClass() != o.getClass()) { + return false; + } + final AndPermission that = (AndPermission) o; + return this.permissions.equals(that.permissions); + } + + @Override + public int hashCode() { + return Objects.hash(this.getPermissions()); + } + +} diff --git a/cloud-core/src/main/java/cloud/commandframework/permission/CommandPermission.java b/cloud-core/src/main/java/cloud/commandframework/permission/CommandPermission.java index 83e440b4..afe93b0f 100644 --- a/cloud-core/src/main/java/cloud/commandframework/permission/CommandPermission.java +++ b/cloud-core/src/main/java/cloud/commandframework/permission/CommandPermission.java @@ -25,10 +25,15 @@ package cloud.commandframework.permission; import org.checkerframework.checker.nullness.qual.NonNull; +import java.util.Arrays; import java.util.Collection; +import java.util.HashSet; +import java.util.Set; + +import static java.util.Objects.requireNonNull; /** - * A command permission string + * A command permission representation. */ public interface CommandPermission { @@ -47,4 +52,64 @@ public interface CommandPermission { @Override String toString(); + /** + * Return a permission that matches either this permission or the {@code other} permission. + * + * @param other the other permission to test + * @return a new {@code or} permission + * @since 1.4.0 + */ + default CommandPermission or(final CommandPermission other) { + requireNonNull(other, "other"); + final Set permission = new HashSet<>(2); + permission.add(this); + permission.add(other); + return new OrPermission(permission); + } + + /** + * Return a permission that matches either this permission or any of the {@code other} permissions. + * + * @param other the other permission to test + * @return a new {@code or} permission + * @since 1.4.0 + */ + default CommandPermission or(final CommandPermission... other) { + requireNonNull(other, "other"); + final Set permission = new HashSet<>(other.length + 1); + permission.add(this); + permission.addAll(Arrays.asList(other)); + return new OrPermission(permission); + } + + /** + * Return a permission that matches this permission and the {@code other} permission. + * + * @param other the other permission to test + * @return a new {@code and} permission + * @since 1.4.0 + */ + default CommandPermission and(final CommandPermission other) { + requireNonNull(other, "other"); + final Set permission = new HashSet<>(2); + permission.add(this); + permission.add(other); + return new AndPermission(permission); + } + + /** + * Return a permission that matches this permission and all of the {@code other} permissions. + * + * @param other the other permission to test + * @return a new {@code and} permission + * @since 1.4.0 + */ + default CommandPermission and(final CommandPermission... other) { + requireNonNull(other, "other"); + final Set permission = new HashSet<>(other.length + 1); + permission.add(this); + permission.addAll(Arrays.asList(other)); + return new AndPermission(permission); + } + } diff --git a/cloud-core/src/main/java/cloud/commandframework/permission/OrPermission.java b/cloud-core/src/main/java/cloud/commandframework/permission/OrPermission.java index ac600591..f2b908cb 100644 --- a/cloud-core/src/main/java/cloud/commandframework/permission/OrPermission.java +++ b/cloud-core/src/main/java/cloud/commandframework/permission/OrPermission.java @@ -25,11 +25,10 @@ package cloud.commandframework.permission; import org.checkerframework.checker.nullness.qual.NonNull; -import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashSet; import java.util.Iterator; -import java.util.List; import java.util.Objects; import java.util.Set; @@ -38,10 +37,10 @@ import java.util.Set; */ public final class OrPermission implements CommandPermission { - private final Collection permissions; + private final Set permissions; - private OrPermission(final @NonNull Collection permissions) { - this.permissions = permissions; + OrPermission(final @NonNull Set permissions) { + this.permissions = Collections.unmodifiableSet(permissions); } /** @@ -82,18 +81,16 @@ public final class OrPermission implements CommandPermission { if (this == o) { return true; } - if (o == null || getClass() != o.getClass()) { + if (o == null || this.getClass() != o.getClass()) { return false; } final OrPermission that = (OrPermission) o; - final List local = new ArrayList<>(this.getPermissions()); - final List foreign = new ArrayList<>(that.getPermissions()); - return local.equals(foreign); + return this.permissions.equals(that.permissions); } @Override public int hashCode() { - return Objects.hash(getPermissions()); + return Objects.hash(this.getPermissions()); } } diff --git a/cloud-core/src/test/java/cloud/commandframework/CommandPermissionTest.java b/cloud-core/src/test/java/cloud/commandframework/CommandPermissionTest.java index 545a5b05..6933317a 100644 --- a/cloud-core/src/test/java/cloud/commandframework/CommandPermissionTest.java +++ b/cloud-core/src/test/java/cloud/commandframework/CommandPermissionTest.java @@ -28,6 +28,8 @@ import cloud.commandframework.execution.CommandExecutionCoordinator; import cloud.commandframework.keys.SimpleCloudKey; import cloud.commandframework.meta.CommandMeta; import cloud.commandframework.meta.SimpleCommandMeta; +import cloud.commandframework.permission.CommandPermission; +import cloud.commandframework.permission.Permission; import cloud.commandframework.permission.PredicatePermission; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; @@ -36,10 +38,12 @@ import org.junit.jupiter.api.Test; import java.util.concurrent.CompletionException; import java.util.concurrent.atomic.AtomicBoolean; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + class CommandPermissionTest { private final static CommandManager manager = new PermissionOutputtingCommandManager(); - private static boolean acceptOne = false; @BeforeAll static void setup() { @@ -52,8 +56,7 @@ class CommandPermissionTest { @Test void testCompoundPermission() { Assertions.assertTrue(manager.suggest(new TestCommandSender(), "t").isEmpty()); - acceptOne = true; - Assertions.assertFalse(manager.suggest(new TestCommandSender(), "t").isEmpty()); + assertFalse(manager.suggest(new TestCommandSender("test.permission.four"), "t").isEmpty()); } @Test @@ -67,6 +70,25 @@ class CommandPermissionTest { ); } + @Test + void testAndPermissions() { + final CommandPermission test = Permission.of("one").and(Permission.of("two")); + final TestCommandSender sender = new TestCommandSender("one"); + assertFalse(manager.hasPermission(sender, test)); + assertFalse(manager.hasPermission(new TestCommandSender("two"), test)); + sender.addPermission("two"); + assertTrue(manager.hasPermission(sender, test)); + } + + @Test + void testOrPermissions() { + final CommandPermission test = Permission.of("one").or(Permission.of("two")); + assertFalse(manager.hasPermission(new TestCommandSender(), test)); + assertTrue(manager.hasPermission(new TestCommandSender("one"), test)); + assertTrue(manager.hasPermission(new TestCommandSender("two"), test)); + assertTrue(manager.hasPermission(new TestCommandSender("one", "two"), test)); + } + @Test void testPredicatePermissions() { final AtomicBoolean condition = new AtomicBoolean(true); @@ -101,7 +123,7 @@ class CommandPermissionTest { if (permission.equalsIgnoreCase("second")) { return false; } - return acceptOne && permission.equalsIgnoreCase("test.permission.four"); + return sender.hasPermisison(permission); } @Override diff --git a/cloud-core/src/test/java/cloud/commandframework/TestCommandSender.java b/cloud-core/src/test/java/cloud/commandframework/TestCommandSender.java index f1bededf..c610651b 100644 --- a/cloud-core/src/test/java/cloud/commandframework/TestCommandSender.java +++ b/cloud-core/src/test/java/cloud/commandframework/TestCommandSender.java @@ -23,6 +23,31 @@ // package cloud.commandframework; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + public class TestCommandSender { + private final Set permissions = new HashSet<>(); + + public TestCommandSender() { + } + + public TestCommandSender(final String... permissions) { + this.permissions.addAll(Arrays.asList(permissions)); + } + + public boolean hasPermisison(final String permission) { + return this.permissions.contains(permission); + } + + + public void addPermission(final String permission) { + this.permissions.add(permission); + } + + public void removePermission(final String permission) { + this.permissions.remove(permission); + } }