diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/ConfigPermissionsHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/ConfigPermissionsHandler.java index 44f12eff..0ce83335 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/ConfigPermissionsHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/ConfigPermissionsHandler.java @@ -19,6 +19,11 @@ public ClientPermissions getPermissions(String username) { return permissions; } + @Override + public void setPermissions(String username, ClientPermissions permissions) { + //Unsupported + } + @Override public void close() { diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/DefaultPermissionsHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/DefaultPermissionsHandler.java index 4b14873b..b9689b38 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/DefaultPermissionsHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/DefaultPermissionsHandler.java @@ -13,6 +13,11 @@ public ClientPermissions getPermissions(String username) { return ClientPermissions.DEFAULT; } + @Override + public void setPermissions(String username, ClientPermissions permissions) { + //Unsupported + } + @Override public void close() throws Exception { diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/JsonFilePermissionsHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/JsonFilePermissionsHandler.java index bd3903f2..43778464 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/JsonFilePermissionsHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/JsonFilePermissionsHandler.java @@ -68,6 +68,11 @@ public ClientPermissions getPermissions(String username) { return map.getOrDefault(username, ClientPermissions.DEFAULT); } + @Override + public void setPermissions(String username, ClientPermissions permissions) { + map.put(username, permissions); + } + public JsonFilePermissionsHandler() { } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/JsonLongFilePermissionsHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/JsonLongFilePermissionsHandler.java index ada99916..3cfb3262 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/JsonLongFilePermissionsHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/JsonLongFilePermissionsHandler.java @@ -69,6 +69,11 @@ public ClientPermissions getPermissions(String username) { return new ClientPermissions(map.getOrDefault(username, defaultPerms)); } + @Override + public void setPermissions(String username, ClientPermissions permissions) { + map.put(username, permissions.toLong()); + } + public JsonLongFilePermissionsHandler() { } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/PermissionsHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/PermissionsHandler.java index d3898138..cf8dad23 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/PermissionsHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/PermissionsHandler.java @@ -41,4 +41,6 @@ public static void registerHandlers() { public abstract void init(); public abstract ClientPermissions getPermissions(String username); + + public abstract void setPermissions(String username, ClientPermissions permissions); } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/command/handler/CommandHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/command/handler/CommandHandler.java index fe387086..7b307ea1 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/command/handler/CommandHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/command/handler/CommandHandler.java @@ -66,5 +66,7 @@ public static void registerCommands(ru.gravit.utils.command.CommandHandler handl handler.registerCommand("multi", new MultiCommand(server)); handler.registerCommand("getModulus", new GetModulusCommand(server)); handler.registerCommand("component", new ComponentCommand(server)); + handler.registerCommand("givePermission", new GivePermissionsCommand(server)); + handler.registerCommand("getPermissions", new GetPermissionsCommand(server)); } } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/command/service/GetPermissionsCommand.java b/LaunchServer/src/main/java/ru/gravit/launchserver/command/service/GetPermissionsCommand.java new file mode 100644 index 00000000..f0bb6ebf --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/command/service/GetPermissionsCommand.java @@ -0,0 +1,30 @@ +package ru.gravit.launchserver.command.service; + +import ru.gravit.launcher.ClientPermissions; +import ru.gravit.launchserver.LaunchServer; +import ru.gravit.launchserver.command.Command; +import ru.gravit.utils.helper.LogHelper; + +public class GetPermissionsCommand extends Command { + public GetPermissionsCommand(LaunchServer server) { + super(server); + } + + @Override + public String getArgsDescription() { + return "[username]"; + } + + @Override + public String getUsageDescription() { + return "print username permissions"; + } + + @Override + public void invoke(String... args) throws Exception { + verifyArgs(args,1); + String username = args[0]; + ClientPermissions permissions = server.config.permissionsHandler.getPermissions(username); + LogHelper.info("Permissions %s: %s (long: %d)", username, permissions.toString(), permissions.toLong()); + } +} diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/command/service/GivePermissionsCommand.java b/LaunchServer/src/main/java/ru/gravit/launchserver/command/service/GivePermissionsCommand.java new file mode 100644 index 00000000..cc0b1083 --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/command/service/GivePermissionsCommand.java @@ -0,0 +1,56 @@ +package ru.gravit.launchserver.command.service; + +import ru.gravit.launcher.ClientPermissions; +import ru.gravit.launchserver.LaunchServer; +import ru.gravit.launchserver.command.Command; +import ru.gravit.utils.helper.LogHelper; + +public class GivePermissionsCommand extends Command { + public GivePermissionsCommand(LaunchServer server) { + super(server); + } + + @Override + public String getArgsDescription() { + return "[username] [permission] [true/false]"; + } + + @Override + public String getUsageDescription() { + return "give permissions"; + } + + @Override + public void invoke(String... args) throws Exception { + verifyArgs(args, 3); + String username = args[0]; + ClientPermissions permissions = server.config.permissionsHandler.getPermissions(username); + String permission = args[1]; + boolean isEnabled = Boolean.valueOf(args[2]); + switch (permission) + { + case "admin": + { + permissions.canAdmin = isEnabled; + break; + } + case "server": + { + permissions.canServer = isEnabled; + break; + } + case "bot": + { + permissions.canBot = isEnabled; + break; + } + default: + { + LogHelper.error("Unknown permission: %s", permission); + return; + } + } + LogHelper.info("Write new permissions for %s", username); + server.config.permissionsHandler.setPermissions(username, permissions); + } +} diff --git a/libLauncher/src/main/java/ru/gravit/launcher/ClientPermissions.java b/libLauncher/src/main/java/ru/gravit/launcher/ClientPermissions.java index 75957772..81ee4ae1 100644 --- a/libLauncher/src/main/java/ru/gravit/launcher/ClientPermissions.java +++ b/libLauncher/src/main/java/ru/gravit/launcher/ClientPermissions.java @@ -4,6 +4,7 @@ import ru.gravit.launcher.serialize.HOutput; import java.io.IOException; +import java.util.StringJoiner; public class ClientPermissions { public static final ClientPermissions DEFAULT = new ClientPermissions(); @@ -64,4 +65,16 @@ public static ClientPermissions getSuperuserAccount() { public void write(HOutput output) throws IOException { output.writeLong(toLong()); } + + @Override + public String toString() { + return new StringJoiner(", ", ClientPermissions.class.getSimpleName() + "[", "]") + .add("canAdmin=" + canAdmin) + .add("canServer=" + canServer) + .add("canUSR1=" + canUSR1) + .add("canUSR2=" + canUSR2) + .add("canUSR3=" + canUSR3) + .add("canBot=" + canBot) + .toString(); + } }