From ec2cb5007c87d9e8e0f8b83022e24c0ffb41b0a4 Mon Sep 17 00:00:00 2001 From: Luck Date: Sat, 28 Dec 2019 23:41:02 +0000 Subject: [PATCH] Move op handling into a permission processor --- .../calculator/BukkitCalculatorFactory.java | 11 ++++- .../bukkit/calculator/OpProcessor.java | 44 +++++++++++++++++++ .../inject/permissible/LPPermissible.java | 36 +++++++-------- .../calculator/NukkitCalculatorFactory.java | 11 ++++- .../nukkit/calculator/OpProcessor.java | 44 +++++++++++++++++++ .../inject/permissible/LPPermissible.java | 40 ++++++++--------- 6 files changed, 143 insertions(+), 43 deletions(-) create mode 100644 bukkit/src/main/java/me/lucko/luckperms/bukkit/calculator/OpProcessor.java create mode 100644 nukkit/src/main/java/me/lucko/luckperms/nukkit/calculator/OpProcessor.java diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/calculator/BukkitCalculatorFactory.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/calculator/BukkitCalculatorFactory.java index 9d679e981..b24f55e47 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/calculator/BukkitCalculatorFactory.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/calculator/BukkitCalculatorFactory.java @@ -66,8 +66,15 @@ public class BukkitCalculatorFactory implements CalculatorFactory { processors.add(new WildcardProcessor()); } - if (this.plugin.getConfiguration().get(ConfigKeys.APPLY_BUKKIT_DEFAULT_PERMISSIONS) && metadata.getHolderType() == HolderType.USER) { - processors.add(new DefaultsProcessor(this.plugin, queryOptions.option(BukkitContextManager.OP_OPTION).orElse(false))); + if (metadata.getHolderType() == HolderType.USER) { + boolean op = queryOptions.option(BukkitContextManager.OP_OPTION).orElse(false); + if (this.plugin.getConfiguration().get(ConfigKeys.APPLY_BUKKIT_DEFAULT_PERMISSIONS)) { + processors.add(new DefaultsProcessor(this.plugin, op)); + } + + if (op) { + processors.add(new OpProcessor()); + } } return new PermissionCalculator(this.plugin, metadata, processors.build()); diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/calculator/OpProcessor.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/calculator/OpProcessor.java new file mode 100644 index 000000000..6ff30b01e --- /dev/null +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/calculator/OpProcessor.java @@ -0,0 +1,44 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * Copyright (c) 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 me.lucko.luckperms.bukkit.calculator; + +import me.lucko.luckperms.common.calculator.processor.PermissionProcessor; +import me.lucko.luckperms.common.calculator.result.TristateResult; + +import net.luckperms.api.util.Tristate; + +/** + * Permission Processor which is added for opped users, to simply return true if + * no other processors match. + */ +public class OpProcessor implements PermissionProcessor { + private static final TristateResult TRUE_RESULT = new TristateResult.Factory(OpProcessor.class).result(Tristate.TRUE); + + @Override + public TristateResult hasPermission(String permission) { + return TRUE_RESULT; + } +} diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/inject/permissible/LPPermissible.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/inject/permissible/LPPermissible.java index 20fd60c74..ed83c889c 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/inject/permissible/LPPermissible.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/inject/permissible/LPPermissible.java @@ -29,7 +29,7 @@ import com.google.common.collect.ImmutableList; import me.lucko.luckperms.bukkit.LPBukkitPlugin; import me.lucko.luckperms.bukkit.calculator.DefaultsProcessor; -import me.lucko.luckperms.bukkit.context.BukkitContextManager; +import me.lucko.luckperms.bukkit.calculator.OpProcessor; import me.lucko.luckperms.common.calculator.result.TristateResult; import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.context.QueryOptionsCache; @@ -141,13 +141,19 @@ public class LPPermissible extends PermissibleBase { throw new NullPointerException("permission"); } - TristateResult result = this.user.getCachedData().getPermissionData(this.queryOptionsSupplier.getQueryOptions()).checkPermission(permission, PermissionCheckEvent.Origin.PLATFORM_LOOKUP_CHECK); + QueryOptions queryOptions = this.queryOptionsSupplier.getQueryOptions(); + TristateResult result = this.user.getCachedData().getPermissionData(queryOptions).checkPermission(permission, PermissionCheckEvent.Origin.PLATFORM_LOOKUP_CHECK); if (result.result() == Tristate.UNDEFINED) { return false; } // ignore matches made from looking up in the permission map (replicate bukkit behaviour) - return !(result.processorClass() == DefaultsProcessor.class && "permission map".equals(result.cause())); + if (result.processorClass() == DefaultsProcessor.class && "permission map".equals(result.cause())) { + return false; + } + + // ignore the op processor + return result.processorClass() != OpProcessor.class; } @Override @@ -166,13 +172,7 @@ public class LPPermissible extends PermissibleBase { } QueryOptions queryOptions = this.queryOptionsSupplier.getQueryOptions(); - Tristate ts = this.user.getCachedData().getPermissionData(queryOptions).checkPermission(permission, PermissionCheckEvent.Origin.PLATFORM_PERMISSION_CHECK).result(); - if (ts != Tristate.UNDEFINED) { - return ts.asBoolean(); - } - - boolean isOp = queryOptions.option(BukkitContextManager.OP_OPTION).orElse(false); - return Permission.DEFAULT_PERMISSION.getValue(isOp); + return this.user.getCachedData().getPermissionData(queryOptions).checkPermission(permission, PermissionCheckEvent.Origin.PLATFORM_PERMISSION_CHECK).result().asBoolean(); } @Override @@ -182,17 +182,15 @@ public class LPPermissible extends PermissibleBase { } QueryOptions queryOptions = this.queryOptionsSupplier.getQueryOptions(); - Tristate ts = this.user.getCachedData().getPermissionData(queryOptions).checkPermission(permission.getName(), PermissionCheckEvent.Origin.PLATFORM_PERMISSION_CHECK).result(); - if (ts != Tristate.UNDEFINED) { - return ts.asBoolean(); + TristateResult result = this.user.getCachedData().getPermissionData(queryOptions).checkPermission(permission.getName(), PermissionCheckEvent.Origin.PLATFORM_PERMISSION_CHECK); + + // override default op handling using the Permission class we have + if (result.processorClass() == OpProcessor.class && this.plugin.getConfiguration().get(ConfigKeys.APPLY_BUKKIT_DEFAULT_PERMISSIONS)) { + // 'op == true' is implied by the presence of the OpProcessor class + return permission.getDefault().getValue(true); } - boolean isOp = queryOptions.option(BukkitContextManager.OP_OPTION).orElse(false); - if (this.plugin.getConfiguration().get(ConfigKeys.APPLY_BUKKIT_DEFAULT_PERMISSIONS)) { - return permission.getDefault().getValue(isOp); - } else { - return Permission.DEFAULT_PERMISSION.getValue(isOp); - } + return result.result().asBoolean(); } /** diff --git a/nukkit/src/main/java/me/lucko/luckperms/nukkit/calculator/NukkitCalculatorFactory.java b/nukkit/src/main/java/me/lucko/luckperms/nukkit/calculator/NukkitCalculatorFactory.java index d121bd62b..bf2380524 100644 --- a/nukkit/src/main/java/me/lucko/luckperms/nukkit/calculator/NukkitCalculatorFactory.java +++ b/nukkit/src/main/java/me/lucko/luckperms/nukkit/calculator/NukkitCalculatorFactory.java @@ -66,8 +66,15 @@ public class NukkitCalculatorFactory implements CalculatorFactory { processors.add(new WildcardProcessor()); } - if (this.plugin.getConfiguration().get(ConfigKeys.APPLY_NUKKIT_DEFAULT_PERMISSIONS) && metadata.getHolderType() == HolderType.USER) { - processors.add(new DefaultsProcessor(this.plugin, queryOptions.option(NukkitContextManager.OP_OPTION).orElse(false))); + if (metadata.getHolderType() == HolderType.USER) { + boolean op = queryOptions.option(NukkitContextManager.OP_OPTION).orElse(false); + if (this.plugin.getConfiguration().get(ConfigKeys.APPLY_NUKKIT_DEFAULT_PERMISSIONS)) { + processors.add(new DefaultsProcessor(this.plugin, op)); + } + + if (op) { + processors.add(new OpProcessor()); + } } return new PermissionCalculator(this.plugin, metadata, processors.build()); diff --git a/nukkit/src/main/java/me/lucko/luckperms/nukkit/calculator/OpProcessor.java b/nukkit/src/main/java/me/lucko/luckperms/nukkit/calculator/OpProcessor.java new file mode 100644 index 000000000..1d5b811b8 --- /dev/null +++ b/nukkit/src/main/java/me/lucko/luckperms/nukkit/calculator/OpProcessor.java @@ -0,0 +1,44 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * Copyright (c) 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 me.lucko.luckperms.nukkit.calculator; + +import me.lucko.luckperms.common.calculator.processor.PermissionProcessor; +import me.lucko.luckperms.common.calculator.result.TristateResult; + +import net.luckperms.api.util.Tristate; + +/** + * Permission Processor which is added for opped users, to simply return true if + * no other processors match. + */ +public class OpProcessor implements PermissionProcessor { + private static final TristateResult TRUE_RESULT = new TristateResult.Factory(OpProcessor.class).result(Tristate.TRUE); + + @Override + public TristateResult hasPermission(String permission) { + return TRUE_RESULT; + } +} diff --git a/nukkit/src/main/java/me/lucko/luckperms/nukkit/inject/permissible/LPPermissible.java b/nukkit/src/main/java/me/lucko/luckperms/nukkit/inject/permissible/LPPermissible.java index 49464e094..4172c2d39 100644 --- a/nukkit/src/main/java/me/lucko/luckperms/nukkit/inject/permissible/LPPermissible.java +++ b/nukkit/src/main/java/me/lucko/luckperms/nukkit/inject/permissible/LPPermissible.java @@ -35,7 +35,7 @@ import me.lucko.luckperms.common.util.ImmutableCollectors; import me.lucko.luckperms.common.verbose.event.PermissionCheckEvent; import me.lucko.luckperms.nukkit.LPNukkitPlugin; import me.lucko.luckperms.nukkit.calculator.DefaultsProcessor; -import me.lucko.luckperms.nukkit.context.NukkitContextManager; +import me.lucko.luckperms.nukkit.calculator.OpProcessor; import me.lucko.luckperms.nukkit.inject.PermissionDefault; import net.luckperms.api.query.QueryOptions; @@ -142,13 +142,19 @@ public class LPPermissible extends PermissibleBase { throw new NullPointerException("permission"); } - TristateResult result = this.user.getCachedData().getPermissionData(this.queryOptionsSupplier.getQueryOptions()).checkPermission(permission, PermissionCheckEvent.Origin.PLATFORM_LOOKUP_CHECK); + QueryOptions queryOptions = this.queryOptionsSupplier.getQueryOptions(); + TristateResult result = this.user.getCachedData().getPermissionData(queryOptions).checkPermission(permission, PermissionCheckEvent.Origin.PLATFORM_LOOKUP_CHECK); if (result.result() == Tristate.UNDEFINED) { return false; } // ignore matches made from looking up in the permission map (replicate nukkit behaviour) - return !(result.processorClass() == DefaultsProcessor.class && "permission map".equals(result.cause())); + if (result.processorClass() == DefaultsProcessor.class && "permission map".equals(result.cause())) { + return false; + } + + // ignore the op processor + return result.processorClass() != OpProcessor.class; } @Override @@ -167,13 +173,7 @@ public class LPPermissible extends PermissibleBase { } QueryOptions queryOptions = this.queryOptionsSupplier.getQueryOptions(); - Tristate ts = this.user.getCachedData().getPermissionData(queryOptions).checkPermission(permission, PermissionCheckEvent.Origin.PLATFORM_PERMISSION_CHECK).result(); - if (ts != Tristate.UNDEFINED) { - return ts.asBoolean(); - } - - boolean isOp = queryOptions.option(NukkitContextManager.OP_OPTION).orElse(false); - return PermissionDefault.OP.getValue(isOp); + return this.user.getCachedData().getPermissionData(queryOptions).checkPermission(permission, PermissionCheckEvent.Origin.PLATFORM_PERMISSION_CHECK).result().asBoolean(); } @Override @@ -183,18 +183,18 @@ public class LPPermissible extends PermissibleBase { } QueryOptions queryOptions = this.queryOptionsSupplier.getQueryOptions(); - Tristate ts = this.user.getCachedData().getPermissionData(queryOptions).checkPermission(permission.getName(), PermissionCheckEvent.Origin.PLATFORM_PERMISSION_CHECK).result(); - if (ts != Tristate.UNDEFINED) { - return ts.asBoolean(); + TristateResult result = this.user.getCachedData().getPermissionData(queryOptions).checkPermission(permission.getName(), PermissionCheckEvent.Origin.PLATFORM_PERMISSION_CHECK); + + // override default op handling using the Permission class we have + if (result.processorClass() == OpProcessor.class && this.plugin.getConfiguration().get(ConfigKeys.APPLY_BUKKIT_DEFAULT_PERMISSIONS)) { + // 'op == true' is implied by the presence of the OpProcessor class + PermissionDefault def = PermissionDefault.fromPermission(permission); + if (def != null) { + return def.getValue(true); + } } - boolean isOp = queryOptions.option(NukkitContextManager.OP_OPTION).orElse(false); - if (this.plugin.getConfiguration().get(ConfigKeys.APPLY_NUKKIT_DEFAULT_PERMISSIONS)) { - PermissionDefault def = PermissionDefault.fromPermission(permission); - return def != null && def.getValue(isOp); - } else { - return PermissionDefault.OP.getValue(isOp); - } + return result.result().asBoolean(); } /**