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.
This commit is contained in:
Zach Levis 2021-01-15 12:14:41 -08:00 committed by Alexander Söderberg
parent 09f8dbd956
commit 7d46e64ed3
7 changed files with 240 additions and 27 deletions

View file

@ -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)) - More abstract description concept ([#207](https://github.com/Incendo/cloud/pull/207))
- Predicate permissions ([#210](https://github.com/Incendo/cloud/pull/210)) - Predicate permissions ([#210](https://github.com/Incendo/cloud/pull/210))
- Injection services ([#211](https://github.com/Incendo/cloud/pull/211)) - 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 ### Changed
- Allow command argument names to include `_` and `-` ([#186](https://github.com/Incendo/cloud/pull/186)) - Allow command argument names to include `_` and `-` ([#186](https://github.com/Incendo/cloud/pull/186))

View file

@ -52,6 +52,7 @@ import cloud.commandframework.execution.preprocessor.CommandPreprocessor;
import cloud.commandframework.internal.CommandInputTokenizer; import cloud.commandframework.internal.CommandInputTokenizer;
import cloud.commandframework.internal.CommandRegistrationHandler; import cloud.commandframework.internal.CommandRegistrationHandler;
import cloud.commandframework.meta.CommandMeta; import cloud.commandframework.meta.CommandMeta;
import cloud.commandframework.permission.AndPermission;
import cloud.commandframework.permission.CommandPermission; import cloud.commandframework.permission.CommandPermission;
import cloud.commandframework.permission.OrPermission; import cloud.commandframework.permission.OrPermission;
import cloud.commandframework.permission.Permission; import cloud.commandframework.permission.Permission;
@ -283,24 +284,30 @@ public abstract class CommandManager<C> {
final @NonNull C sender, final @NonNull C sender,
final @NonNull CommandPermission permission final @NonNull CommandPermission permission
) { ) {
if (permission instanceof Permission) {
if (permission.toString().isEmpty()) { if (permission.toString().isEmpty()) {
return true; return true;
} }
if (permission instanceof Permission) { return this.hasPermission(sender, permission.toString());
return hasPermission(sender, permission.toString()); } else if (permission instanceof PredicatePermission) {
}
if (permission instanceof PredicatePermission) {
return ((PredicatePermission<C>) permission).hasPermission(sender); return ((PredicatePermission<C>) permission).hasPermission(sender);
} } else if (permission instanceof OrPermission) {
for (final CommandPermission innerPermission : permission.getPermissions()) { for (final CommandPermission innerPermission : permission.getPermissions()) {
final boolean hasPermission = this.hasPermission(sender, innerPermission); if (this.hasPermission(sender, innerPermission)) {
if (permission instanceof OrPermission) {
if (hasPermission) {
return true; return true;
} }
} }
}
return false; return false;
} else if (permission instanceof AndPermission) {
for (final CommandPermission innerPermission : permission.getPermissions()) {
if (!this.hasPermission(sender, innerPermission)) {
return false;
}
}
return true;
}
throw new IllegalArgumentException("Unknown permission type " + permission.getClass());
} }
/** /**

View file

@ -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<CommandPermission> permissions;
AndPermission(final @NonNull Set<CommandPermission> 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<CommandPermission> permissions) {
final Set<CommandPermission> 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<CommandPermission> 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());
}
}

View file

@ -25,10 +25,15 @@ package cloud.commandframework.permission;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.Arrays;
import java.util.Collection; 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 { public interface CommandPermission {
@ -47,4 +52,64 @@ public interface CommandPermission {
@Override @Override
String toString(); 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<CommandPermission> 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<CommandPermission> 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<CommandPermission> 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<CommandPermission> permission = new HashSet<>(other.length + 1);
permission.add(this);
permission.addAll(Arrays.asList(other));
return new AndPermission(permission);
}
} }

View file

@ -25,11 +25,10 @@ package cloud.commandframework.permission;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
@ -38,10 +37,10 @@ import java.util.Set;
*/ */
public final class OrPermission implements CommandPermission { public final class OrPermission implements CommandPermission {
private final Collection<CommandPermission> permissions; private final Set<CommandPermission> permissions;
private OrPermission(final @NonNull Collection<CommandPermission> permissions) { OrPermission(final @NonNull Set<CommandPermission> permissions) {
this.permissions = permissions; this.permissions = Collections.unmodifiableSet(permissions);
} }
/** /**
@ -82,18 +81,16 @@ public final class OrPermission implements CommandPermission {
if (this == o) { if (this == o) {
return true; return true;
} }
if (o == null || getClass() != o.getClass()) { if (o == null || this.getClass() != o.getClass()) {
return false; return false;
} }
final OrPermission that = (OrPermission) o; final OrPermission that = (OrPermission) o;
final List<CommandPermission> local = new ArrayList<>(this.getPermissions()); return this.permissions.equals(that.permissions);
final List<CommandPermission> foreign = new ArrayList<>(that.getPermissions());
return local.equals(foreign);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(getPermissions()); return Objects.hash(this.getPermissions());
} }
} }

View file

@ -28,6 +28,8 @@ import cloud.commandframework.execution.CommandExecutionCoordinator;
import cloud.commandframework.keys.SimpleCloudKey; import cloud.commandframework.keys.SimpleCloudKey;
import cloud.commandframework.meta.CommandMeta; import cloud.commandframework.meta.CommandMeta;
import cloud.commandframework.meta.SimpleCommandMeta; import cloud.commandframework.meta.SimpleCommandMeta;
import cloud.commandframework.permission.CommandPermission;
import cloud.commandframework.permission.Permission;
import cloud.commandframework.permission.PredicatePermission; import cloud.commandframework.permission.PredicatePermission;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll; 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.CompletionException;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class CommandPermissionTest { class CommandPermissionTest {
private final static CommandManager<TestCommandSender> manager = new PermissionOutputtingCommandManager(); private final static CommandManager<TestCommandSender> manager = new PermissionOutputtingCommandManager();
private static boolean acceptOne = false;
@BeforeAll @BeforeAll
static void setup() { static void setup() {
@ -52,8 +56,7 @@ class CommandPermissionTest {
@Test @Test
void testCompoundPermission() { void testCompoundPermission() {
Assertions.assertTrue(manager.suggest(new TestCommandSender(), "t").isEmpty()); Assertions.assertTrue(manager.suggest(new TestCommandSender(), "t").isEmpty());
acceptOne = true; assertFalse(manager.suggest(new TestCommandSender("test.permission.four"), "t").isEmpty());
Assertions.assertFalse(manager.suggest(new TestCommandSender(), "t").isEmpty());
} }
@Test @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 @Test
void testPredicatePermissions() { void testPredicatePermissions() {
final AtomicBoolean condition = new AtomicBoolean(true); final AtomicBoolean condition = new AtomicBoolean(true);
@ -101,7 +123,7 @@ class CommandPermissionTest {
if (permission.equalsIgnoreCase("second")) { if (permission.equalsIgnoreCase("second")) {
return false; return false;
} }
return acceptOne && permission.equalsIgnoreCase("test.permission.four"); return sender.hasPermisison(permission);
} }
@Override @Override

View file

@ -23,6 +23,31 @@
// //
package cloud.commandframework; package cloud.commandframework;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class TestCommandSender { public class TestCommandSender {
private final Set<String> 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);
}
} }