diff options
author | Petr Ilin <hevav@hevav.dev> | 2023-01-23 22:55:42 +0300 |
---|---|---|
committer | Petr Ilin <hevav@hevav.dev> | 2023-01-23 22:55:42 +0300 |
commit | 5709af5c0ade33aff357650fd4e57f79c4844649 (patch) | |
tree | ebb3444d471fa3a5922cabec9735fd693dab2611 /src/main/java/net/elytrium | |
parent | 5493d0efb8cc31fe630a5b02e84dcd0378810b40 (diff) | |
download | LimboAuth-5709af5c0ade33aff357650fd4e57f79c4844649.tar.gz LimboAuth-5709af5c0ade33aff357650fd4e57f79c4844649.tar.bz2 LimboAuth-5709af5c0ade33aff357650fd4e57f79c4844649.zip |
Ability to deny/allow commands without permission plugins
Diffstat (limited to 'src/main/java/net/elytrium')
10 files changed, 99 insertions, 18 deletions
diff --git a/src/main/java/net/elytrium/limboauth/Settings.java b/src/main/java/net/elytrium/limboauth/Settings.java index b83341a..17d5782 100644 --- a/src/main/java/net/elytrium/limboauth/Settings.java +++ b/src/main/java/net/elytrium/limboauth/Settings.java @@ -28,6 +28,7 @@ import net.elytrium.commons.config.YamlConfig; import net.elytrium.commons.kyori.serialization.Serializers; import net.elytrium.limboapi.api.chunk.Dimension; import net.elytrium.limboapi.api.player.GameMode; +import net.elytrium.limboauth.command.CommandPermissionState; import net.elytrium.limboauth.dependencies.DatabaseLibrary; import net.elytrium.limboauth.migration.MigrationHash; import net.kyori.adventure.bossbar.BossBar; @@ -286,6 +287,37 @@ public class Settings extends YamlConfig { } } + @Create + public MAIN.COMMAND_PERMISSION_STATE COMMAND_PERMISSION_STATE; + + @Comment({ + "Available values: FALSE, TRUE, PERMISSION", + " FALSE - the command will be disallowed", + " TRUE - the command will be allowed if player has false permission state", + " PERMISSION - the command will be allowed if player has true permission state" + }) + public static class COMMAND_PERMISSION_STATE { + @Comment("Permission: limboauth.commands.changepassword") + public CommandPermissionState CHANGE_PASSWORD = CommandPermissionState.PERMISSION; + @Comment("Permission: limboauth.commands.destroysession") + public CommandPermissionState DESTROY_SESSION = CommandPermissionState.PERMISSION; + @Comment("Permission: limboauth.commands.premium") + public CommandPermissionState PREMIUM = CommandPermissionState.PERMISSION; + @Comment("Permission: limboauth.commands.totp") + public CommandPermissionState TOTP = CommandPermissionState.PERMISSION; + @Comment("Permission: limboauth.commands.unregister") + public CommandPermissionState UNREGISTER = CommandPermissionState.PERMISSION; + + @Comment("Permission: limboauth.admin.forcechangepassword") + public CommandPermissionState FORCE_CHANGE_PASSWORD = CommandPermissionState.PERMISSION; + @Comment("Permission: limboauth.admin.forceunregister") + public CommandPermissionState FORCE_UNREGISTER = CommandPermissionState.PERMISSION; + @Comment("Permission: limboauth.admin.reload") + public CommandPermissionState RELOAD = CommandPermissionState.PERMISSION; + @Comment("Permission: limboauth.admin.help") + public CommandPermissionState HELP = CommandPermissionState.TRUE; + } + /* @Create public Settings.MAIN.EVENTS_PRIORITIES EVENTS_PRIORITIES; diff --git a/src/main/java/net/elytrium/limboauth/command/ChangePasswordCommand.java b/src/main/java/net/elytrium/limboauth/command/ChangePasswordCommand.java index ebe9032..d903c32 100644 --- a/src/main/java/net/elytrium/limboauth/command/ChangePasswordCommand.java +++ b/src/main/java/net/elytrium/limboauth/command/ChangePasswordCommand.java @@ -21,7 +21,6 @@ import com.j256.ormlite.dao.Dao; import com.j256.ormlite.stmt.UpdateBuilder; import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.SimpleCommand; -import com.velocitypowered.api.permission.Tristate; import com.velocitypowered.api.proxy.Player; import java.sql.SQLException; import net.elytrium.commons.kyori.serialization.Serializer; @@ -110,6 +109,7 @@ public class ChangePasswordCommand implements SimpleCommand { @Override public boolean hasPermission(SimpleCommand.Invocation invocation) { - return invocation.source().getPermissionValue("limboauth.commands.changepassword") == Tristate.TRUE; + return Settings.IMP.MAIN.COMMAND_PERMISSION_STATE.CHANGE_PASSWORD + .hasPermission(invocation.source(), "limboauth.commands.changepassword"); } } diff --git a/src/main/java/net/elytrium/limboauth/command/CommandPermissionState.java b/src/main/java/net/elytrium/limboauth/command/CommandPermissionState.java new file mode 100644 index 0000000..47e9208 --- /dev/null +++ b/src/main/java/net/elytrium/limboauth/command/CommandPermissionState.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2021 - 2023 Elytrium + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package net.elytrium.limboauth.command; + +import com.velocitypowered.api.permission.PermissionSubject; +import com.velocitypowered.api.permission.Tristate; +import java.util.function.BiFunction; + +public enum CommandPermissionState { + FALSE((source, permission) -> false), + TRUE((source, permission) -> source.getPermissionValue(permission) != Tristate.FALSE), + PERMISSION(PermissionSubject::hasPermission); + + private final BiFunction<PermissionSubject, String, Boolean> hasPermissionFunction; + + CommandPermissionState(BiFunction<PermissionSubject, String, Boolean> hasPermissionFunction) { + this.hasPermissionFunction = hasPermissionFunction; + } + + public boolean hasPermission(PermissionSubject permissionSubject, String permission) { + return this.hasPermissionFunction.apply(permissionSubject, permission); + } +} diff --git a/src/main/java/net/elytrium/limboauth/command/DestroySessionCommand.java b/src/main/java/net/elytrium/limboauth/command/DestroySessionCommand.java index 604681c..f796f2e 100644 --- a/src/main/java/net/elytrium/limboauth/command/DestroySessionCommand.java +++ b/src/main/java/net/elytrium/limboauth/command/DestroySessionCommand.java @@ -19,7 +19,6 @@ package net.elytrium.limboauth.command; import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.SimpleCommand; -import com.velocitypowered.api.permission.Tristate; import com.velocitypowered.api.proxy.Player; import net.elytrium.commons.kyori.serialization.Serializer; import net.elytrium.limboauth.LimboAuth; @@ -55,6 +54,7 @@ public class DestroySessionCommand implements SimpleCommand { @Override public boolean hasPermission(SimpleCommand.Invocation invocation) { - return invocation.source().getPermissionValue("limboauth.commands.destroysession") == Tristate.TRUE; + return Settings.IMP.MAIN.COMMAND_PERMISSION_STATE.DESTROY_SESSION + .hasPermission(invocation.source(), "imboauth.commands.destroysession"); } } diff --git a/src/main/java/net/elytrium/limboauth/command/ForceChangePasswordCommand.java b/src/main/java/net/elytrium/limboauth/command/ForceChangePasswordCommand.java index 9e769e8..ed6f292 100644 --- a/src/main/java/net/elytrium/limboauth/command/ForceChangePasswordCommand.java +++ b/src/main/java/net/elytrium/limboauth/command/ForceChangePasswordCommand.java @@ -93,6 +93,7 @@ public class ForceChangePasswordCommand implements SimpleCommand { @Override public boolean hasPermission(SimpleCommand.Invocation invocation) { - return invocation.source().hasPermission("limboauth.admin.forcechangepassword"); + return Settings.IMP.MAIN.COMMAND_PERMISSION_STATE.FORCE_CHANGE_PASSWORD + .hasPermission(invocation.source(), "limboauth.admin.forcechangepassword"); } } diff --git a/src/main/java/net/elytrium/limboauth/command/ForceUnregisterCommand.java b/src/main/java/net/elytrium/limboauth/command/ForceUnregisterCommand.java index 9d34d82..fff7120 100644 --- a/src/main/java/net/elytrium/limboauth/command/ForceUnregisterCommand.java +++ b/src/main/java/net/elytrium/limboauth/command/ForceUnregisterCommand.java @@ -88,6 +88,7 @@ public class ForceUnregisterCommand implements SimpleCommand { @Override public boolean hasPermission(SimpleCommand.Invocation invocation) { - return invocation.source().hasPermission("limboauth.admin.forceunregister"); + return Settings.IMP.MAIN.COMMAND_PERMISSION_STATE.FORCE_UNREGISTER + .hasPermission(invocation.source(), "limboauth.admin.forceunregister"); } } diff --git a/src/main/java/net/elytrium/limboauth/command/LimboAuthCommand.java b/src/main/java/net/elytrium/limboauth/command/LimboAuthCommand.java index d55b421..56a0638 100644 --- a/src/main/java/net/elytrium/limboauth/command/LimboAuthCommand.java +++ b/src/main/java/net/elytrium/limboauth/command/LimboAuthCommand.java @@ -92,6 +92,12 @@ public class LimboAuthCommand implements SimpleCommand { } } + @Override + public boolean hasPermission(Invocation invocation) { + return Settings.IMP.MAIN.COMMAND_PERMISSION_STATE.HELP + .hasPermission(invocation.source(), "limboauth.commands.help"); + } + private void showHelp(CommandSource source) { HELP_MESSAGE.forEach(source::sendMessage); @@ -108,23 +114,26 @@ public class LimboAuthCommand implements SimpleCommand { } private enum Subcommand { - RELOAD("Reload config.", (LimboAuthCommand parent, CommandSource source, String[] args) -> { - parent.plugin.reload(); - source.sendMessage(LimboAuth.getSerializer().deserialize(Settings.IMP.MAIN.STRINGS.RELOAD)); - }); + RELOAD("Reload config.", Settings.IMP.MAIN.COMMAND_PERMISSION_STATE.RELOAD, + (LimboAuthCommand parent, CommandSource source, String[] args) -> { + parent.plugin.reload(); + source.sendMessage(LimboAuth.getSerializer().deserialize(Settings.IMP.MAIN.STRINGS.RELOAD)); + }); private final String command; private final String description; + private final CommandPermissionState permissionState; private final SubcommandExecutor executor; - Subcommand(String description, SubcommandExecutor executor) { + Subcommand(String description, CommandPermissionState permissionState, SubcommandExecutor executor) { + this.permissionState = permissionState; this.command = this.name().toLowerCase(Locale.ROOT); this.description = description; this.executor = executor; } public boolean hasPermission(CommandSource source) { - return source.hasPermission("limboauth.admin." + this.command); + return this.permissionState.hasPermission(source, "limboauth.admin." + this.command); } public Component getMessageLine() { diff --git a/src/main/java/net/elytrium/limboauth/command/PremiumCommand.java b/src/main/java/net/elytrium/limboauth/command/PremiumCommand.java index fb42e92..7cc04cb 100644 --- a/src/main/java/net/elytrium/limboauth/command/PremiumCommand.java +++ b/src/main/java/net/elytrium/limboauth/command/PremiumCommand.java @@ -20,7 +20,6 @@ package net.elytrium.limboauth.command; import com.j256.ormlite.dao.Dao; import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.SimpleCommand; -import com.velocitypowered.api.permission.Tristate; import com.velocitypowered.api.proxy.Player; import java.sql.SQLException; import java.util.Locale; @@ -107,6 +106,7 @@ public class PremiumCommand implements SimpleCommand { @Override public boolean hasPermission(SimpleCommand.Invocation invocation) { - return invocation.source().getPermissionValue("limboauth.commands.premium") == Tristate.TRUE; + return Settings.IMP.MAIN.COMMAND_PERMISSION_STATE.PREMIUM + .hasPermission(invocation.source(), "limboauth.commands.premium"); } } diff --git a/src/main/java/net/elytrium/limboauth/command/TotpCommand.java b/src/main/java/net/elytrium/limboauth/command/TotpCommand.java index b5edb66..e78dbcc 100644 --- a/src/main/java/net/elytrium/limboauth/command/TotpCommand.java +++ b/src/main/java/net/elytrium/limboauth/command/TotpCommand.java @@ -21,7 +21,6 @@ import com.j256.ormlite.dao.Dao; import com.j256.ormlite.stmt.UpdateBuilder; import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.SimpleCommand; -import com.velocitypowered.api.permission.Tristate; import com.velocitypowered.api.proxy.Player; import dev.samstevens.totp.qr.QrData; import dev.samstevens.totp.recovery.RecoveryCodeGenerator; @@ -187,6 +186,7 @@ public class TotpCommand implements SimpleCommand { @Override public boolean hasPermission(SimpleCommand.Invocation invocation) { - return invocation.source().getPermissionValue("limboauth.commands.totp") == Tristate.TRUE; + return Settings.IMP.MAIN.COMMAND_PERMISSION_STATE.TOTP + .hasPermission(invocation.source(), "limboauth.commands.totp"); } } diff --git a/src/main/java/net/elytrium/limboauth/command/UnregisterCommand.java b/src/main/java/net/elytrium/limboauth/command/UnregisterCommand.java index 03f0cfc..fa8334c 100644 --- a/src/main/java/net/elytrium/limboauth/command/UnregisterCommand.java +++ b/src/main/java/net/elytrium/limboauth/command/UnregisterCommand.java @@ -20,7 +20,6 @@ package net.elytrium.limboauth.command; import com.j256.ormlite.dao.Dao; import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.SimpleCommand; -import com.velocitypowered.api.permission.Tristate; import com.velocitypowered.api.proxy.Player; import java.sql.SQLException; import java.util.Locale; @@ -102,6 +101,7 @@ public class UnregisterCommand implements SimpleCommand { @Override public boolean hasPermission(SimpleCommand.Invocation invocation) { - return invocation.source().getPermissionValue("limboauth.commands.unregister") == Tristate.TRUE; + return Settings.IMP.MAIN.COMMAND_PERMISSION_STATE.UNREGISTER + .hasPermission(invocation.source(), "limboauth.commands.unregister"); } } |