From d919ac0db8ff221e4d4d4aa8bc4d2847a64ab051 Mon Sep 17 00:00:00 2001 From: Muhammad Tamir Date: Fri, 20 Jun 2025 11:09:59 +0700 Subject: [PATCH] Permission Checker --- .../org/yuemi/commands/SubcommandHandler.java | 38 ++++++++++++------- app/src/main/resources/plugin.yml | 28 ++++++++++++++ 2 files changed, 53 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/org/yuemi/commands/SubcommandHandler.java b/app/src/main/java/org/yuemi/commands/SubcommandHandler.java index 272e20a..6f0304d 100644 --- a/app/src/main/java/org/yuemi/commands/SubcommandHandler.java +++ b/app/src/main/java/org/yuemi/commands/SubcommandHandler.java @@ -23,23 +23,24 @@ public class SubcommandHandler { private final Map commands = new HashMap<>(); private final String filePassword; private final String dbPassword; + private final Map permissions = new HashMap<>(); public SubcommandHandler(JavaPlugin plugin, String filePassword, String dbPassword) { this.filePassword = filePassword; this.dbPassword = dbPassword; - commands.put("add", new GitAddSubcommand(plugin)); - commands.put("commit", new GitCommitSubcommand(plugin)); - commands.put("reset", new GitResetSubcommand(plugin)); - commands.put("init", new GitInitSubcommand(plugin)); - commands.put("remote", new GitRemoteSubcommand(plugin)); - commands.put("status", new GitStatusSubcommand(plugin)); - commands.put("help", new GitHelpSubcommand(plugin)); - commands.put("push", new GitPushSubcommand(plugin, filePassword, dbPassword)); - commands.put("fetch", new GitFetchSubcommand(plugin, filePassword, dbPassword)); - commands.put("pull", new GitPullSubcommand(plugin, filePassword, dbPassword)); - commands.put("login", new GitLoginSubcommand(plugin, filePassword, dbPassword)); - commands.put("whoami", new GitWhoamiSubcommand(plugin, filePassword, dbPassword)); - commands.put("logout", new GitLogoutSubcommand(plugin, filePassword, dbPassword)); + register("add", new GitAddSubcommand(plugin)); + register("commit", new GitCommitSubcommand(plugin)); + register("reset", new GitResetSubcommand(plugin)); + register("init", new GitInitSubcommand(plugin)); + register("remote", new GitRemoteSubcommand(plugin)); + register("status", new GitStatusSubcommand(plugin)); + register("help", new GitHelpSubcommand(plugin)); + register("push", new GitPushSubcommand(plugin, filePassword, dbPassword)); + register("fetch", new GitFetchSubcommand(plugin, filePassword, dbPassword)); + register("pull", new GitPullSubcommand(plugin, filePassword, dbPassword)); + register("login", new GitLoginSubcommand(plugin, filePassword, dbPassword)); + register("whoami", new GitWhoamiSubcommand(plugin, filePassword, dbPassword)); + register("logout", new GitLogoutSubcommand(plugin, filePassword, dbPassword)); } public void handle(CommandSender sender, String name, String[] args) { @@ -49,6 +50,17 @@ public class SubcommandHandler { return; } + String permission = permissions.get(name.toLowerCase()); + if (permission != null && !sender.hasPermission(permission)) { + sender.sendMessage("§cYou do not have permission to run /git " + name); + return; + } + executor.execute(sender, args); } + + private void register(String name, SubcommandExecutor executor) { + commands.put(name, executor); + permissions.put(name, "gitcraft.command." + name); + } } diff --git a/app/src/main/resources/plugin.yml b/app/src/main/resources/plugin.yml index 0491cb8..68eda3d 100644 --- a/app/src/main/resources/plugin.yml +++ b/app/src/main/resources/plugin.yml @@ -8,3 +8,31 @@ commands: git: description: Git subcommand dispatcher usage: /git +permissions: + gitcraft.command.add: + default: op + gitcraft.command.commit: + default: op + gitcraft.command.reset: + default: op + gitcraft.command.init: + default: op + gitcraft.command.remote: + default: op + gitcraft.command.status: + default: op + gitcraft.command.help: + default: op + gitcraft.command.push: + default: op + gitcraft.command.fetch: + default: op + gitcraft.command.pull: + default: op + gitcraft.command.login: + default: op + gitcraft.command.whoami: + default: op + gitcraft.command.logout: + default: op +