Move op handling into a permission processor

This commit is contained in:
Luck 2019-12-28 23:41:02 +00:00
parent ca33cee79d
commit ec2cb5007c
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
6 changed files with 143 additions and 43 deletions

View File

@ -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());

View File

@ -0,0 +1,44 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* 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;
}
}

View File

@ -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();
}
/**

View File

@ -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());

View File

@ -0,0 +1,44 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* 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;
}
}

View File

@ -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();
}
/**