From bb07d9ea4126705d6899ed2f44db4b3567fc463e Mon Sep 17 00:00:00 2001 From: 4Benj_ <5093878+4Benj@users.noreply.github.com> Date: Thu, 16 Jun 2022 23:00:03 +0800 Subject: [PATCH] Custom Permission Handler (#1282) Co-authored-by: Melledy <52122272+Melledy@users.noreply.github.com> --- .../java/emu/grasscutter/Grasscutter.java | 18 ++++++++++- .../emu/grasscutter/command/CommandMap.java | 19 +++-------- .../command/DefaultPermissionHandler.java | 32 +++++++++++++++++++ .../command/PermissionHandler.java | 8 +++++ .../command/commands/PermissionCommand.java | 5 +++ .../grasscutter/plugin/api/ServerHook.java | 9 ++++++ 6 files changed, 75 insertions(+), 16 deletions(-) create mode 100644 src/main/java/emu/grasscutter/command/DefaultPermissionHandler.java create mode 100644 src/main/java/emu/grasscutter/command/PermissionHandler.java diff --git a/src/main/java/emu/grasscutter/Grasscutter.java b/src/main/java/emu/grasscutter/Grasscutter.java index 2642caa20..472fc2e36 100644 --- a/src/main/java/emu/grasscutter/Grasscutter.java +++ b/src/main/java/emu/grasscutter/Grasscutter.java @@ -6,6 +6,8 @@ import java.util.Calendar; import emu.grasscutter.auth.AuthenticationSystem; import emu.grasscutter.auth.DefaultAuthentication; import emu.grasscutter.command.CommandMap; +import emu.grasscutter.command.DefaultPermissionHandler; +import emu.grasscutter.command.PermissionHandler; import emu.grasscutter.game.managers.energy.EnergyManager; import emu.grasscutter.game.managers.stamina.StaminaManager; import emu.grasscutter.plugin.PluginManager; @@ -58,6 +60,7 @@ public final class Grasscutter { private static GameServer gameServer; private static PluginManager pluginManager; private static AuthenticationSystem authenticationSystem; + private static PermissionHandler permissionHandler; public static final Reflections reflector = new Reflections("emu.grasscutter"); public static ConfigContainer config; @@ -114,8 +117,9 @@ public final class Grasscutter { // Initialize database. DatabaseManager.initialize(); - // Initialize the default authentication system. + // Initialize the default systems. authenticationSystem = new DefaultAuthentication(); + permissionHandler = new DefaultPermissionHandler(); // Create server instances. httpServer = new HttpServer(); @@ -287,6 +291,10 @@ public final class Grasscutter { return authenticationSystem; } + public static PermissionHandler getPermissionHandler() { + return permissionHandler; + } + public static int getCurrentDayOfWeek() { return day; } @@ -346,6 +354,14 @@ public final class Grasscutter { Grasscutter.authenticationSystem = authenticationSystem; } + /** + * Sets the permission handler for the server. + * @param permissionHandler The permission handler to use. + */ + public static void setPermissionHandler(PermissionHandler permissionHandler) { + Grasscutter.permissionHandler = permissionHandler; + } + /* * Enums for the configuration. */ diff --git a/src/main/java/emu/grasscutter/command/CommandMap.java b/src/main/java/emu/grasscutter/command/CommandMap.java index 2495bf4ad..05c099bcb 100644 --- a/src/main/java/emu/grasscutter/command/CommandMap.java +++ b/src/main/java/emu/grasscutter/command/CommandMap.java @@ -14,6 +14,7 @@ public final class CommandMap { private final Map annotations = new HashMap<>(); private final Map targetPlayerIds = new HashMap<>(); private static final String consoleId = "console"; + public CommandMap() { this(false); } @@ -202,21 +203,9 @@ public final class CommandMap { } } - // Check for permission. - if (player != null) { - String permissionNode = this.annotations.get(label).permission(); - String permissionNodeTargeted = this.annotations.get(label).permissionTargeted(); - Account account = player.getAccount(); - if (player != targetPlayer) { // Additional permission required for targeting another player - if (!permissionNodeTargeted.isEmpty() && !account.hasPermission(permissionNodeTargeted)) { - CommandHandler.sendTranslatedMessage(player, "commands.generic.permission_error"); - return; - } - } - if (!permissionNode.isEmpty() && !account.hasPermission(permissionNode)) { - CommandHandler.sendTranslatedMessage(player, "commands.generic.permission_error"); - return; - } + // Check for permissions. + if (!Grasscutter.getPermissionHandler().checkPermission(player, targetPlayer, this.annotations.get(label).permission(), this.annotations.get(label).permissionTargeted())) { + return; } // Check if command has unfulfilled constraints on targetPlayer diff --git a/src/main/java/emu/grasscutter/command/DefaultPermissionHandler.java b/src/main/java/emu/grasscutter/command/DefaultPermissionHandler.java new file mode 100644 index 000000000..10a325139 --- /dev/null +++ b/src/main/java/emu/grasscutter/command/DefaultPermissionHandler.java @@ -0,0 +1,32 @@ +package emu.grasscutter.command; + +import emu.grasscutter.game.Account; +import emu.grasscutter.game.player.Player; + +public class DefaultPermissionHandler implements PermissionHandler { + @Override + public boolean EnablePermissionCommand() { + return true; + } + + @Override + public boolean checkPermission(Player player, Player targetPlayer, String permissionNode, String permissionNodeTargeted) { + if(player == null) { + return true; + } + + Account account = player.getAccount(); + if (player != targetPlayer) { // Additional permission required for targeting another player + if (!permissionNodeTargeted.isEmpty() && !account.hasPermission(permissionNodeTargeted)) { + CommandHandler.sendTranslatedMessage(player, "commands.generic.permission_error"); + return false; + } + } + if (!permissionNode.isEmpty() && !account.hasPermission(permissionNode)) { + CommandHandler.sendTranslatedMessage(player, "commands.generic.permission_error"); + return false; + } + + return true; + } +} diff --git a/src/main/java/emu/grasscutter/command/PermissionHandler.java b/src/main/java/emu/grasscutter/command/PermissionHandler.java new file mode 100644 index 000000000..9d9eb26f4 --- /dev/null +++ b/src/main/java/emu/grasscutter/command/PermissionHandler.java @@ -0,0 +1,8 @@ +package emu.grasscutter.command; + +import emu.grasscutter.game.player.Player; + +public interface PermissionHandler { + public boolean EnablePermissionCommand(); + public boolean checkPermission(Player player, Player targetPlayer, String permissionNode, String permissionNodeTargeted); +} diff --git a/src/main/java/emu/grasscutter/command/commands/PermissionCommand.java b/src/main/java/emu/grasscutter/command/commands/PermissionCommand.java index 8efc8de43..fe945ce2c 100644 --- a/src/main/java/emu/grasscutter/command/commands/PermissionCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/PermissionCommand.java @@ -20,6 +20,11 @@ public final class PermissionCommand implements CommandHandler { return; } + if(!Grasscutter.getPermissionHandler().EnablePermissionCommand()) { + CommandHandler.sendTranslatedMessage(sender, "commands.generic.permission_error"); + return; + } + String action = args.get(0); String permission = args.get(1); diff --git a/src/main/java/emu/grasscutter/plugin/api/ServerHook.java b/src/main/java/emu/grasscutter/plugin/api/ServerHook.java index ffa19110d..feb22547c 100644 --- a/src/main/java/emu/grasscutter/plugin/api/ServerHook.java +++ b/src/main/java/emu/grasscutter/plugin/api/ServerHook.java @@ -4,6 +4,7 @@ import emu.grasscutter.Grasscutter; import emu.grasscutter.auth.AuthenticationSystem; import emu.grasscutter.command.Command; import emu.grasscutter.command.CommandHandler; +import emu.grasscutter.command.PermissionHandler; import emu.grasscutter.game.player.Player; import emu.grasscutter.server.game.GameServer; import emu.grasscutter.server.http.HttpServer; @@ -97,4 +98,12 @@ public final class ServerHook { public void setAuthSystem(AuthenticationSystem authSystem) { Grasscutter.setAuthenticationSystem(authSystem); } + + /** + * Sets the server's permission handler. + * @param permHandler An instance of the permission handler. + */ + public void setPermissionHandler(PermissionHandler permHandler) { + Grasscutter.setPermissionHandler(permHandler); + } } \ No newline at end of file