From 57da09dee9e436bdb3c98ae9baecfdfd8654df3c Mon Sep 17 00:00:00 2001 From: Gravit Date: Fri, 7 Jun 2019 05:03:42 +0700 Subject: [PATCH] =?UTF-8?q?[FEATURE][FIX]=20=D0=98=D1=81=D0=BF=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=D0=B0=20=D1=81=D0=B5=D1=80=D0=B8=D0=B0?= =?UTF-8?q?=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D1=8F=20ClientPermissions?= =?UTF-8?q?=20=D0=B8=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=20?= =?UTF-8?q?HibernatePermissionsHandler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pro/gravit/launchserver/LaunchServer.java | 4 +-- .../permissions/ConfigPermissionsHandler.java | 5 ---- .../DefaultPermissionsHandler.java | 4 --- .../HibernatePermissionsHandler.java | 27 +++++++++++++++++++ .../JsonFilePermissionsHandler.java | 4 ++- .../JsonLongFilePermissionsHandler.java | 4 ++- .../auth/permissions/PermissionsHandler.java | 8 +++++- .../pro/gravit/launchserver/dao/User.java | 10 +++++++ .../gravit/launcher/ClientPermissions.java | 12 ++++----- 9 files changed, 58 insertions(+), 20 deletions(-) create mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/auth/permissions/HibernatePermissionsHandler.java diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java b/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java index 98035a73..4f427507 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java @@ -241,7 +241,7 @@ public void init() { for (AuthProviderPair provider : auth) { provider.init(server); } - permissionsHandler.init(); + permissionsHandler.init(server); hwidHandler.init(); if (protectHandler != null) { protectHandler.checkLaunchServerLicense(); @@ -590,7 +590,7 @@ public LaunchServer(Path dir, boolean testEnv, String[] args) throws IOException for (AuthProviderPair provider : config.auth) { provider.init(this); } - config.permissionsHandler.init(); + config.permissionsHandler.init(this); config.hwidHandler.init(); if (config.protectHandler != null) { config.protectHandler.checkLaunchServerLicense(); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/permissions/ConfigPermissionsHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/permissions/ConfigPermissionsHandler.java index d9866ba7..8b892f85 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/permissions/ConfigPermissionsHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/permissions/ConfigPermissionsHandler.java @@ -6,11 +6,6 @@ public class ConfigPermissionsHandler extends PermissionsHandler { public boolean isAdmin = false; public boolean isServer = false; - @Override - public void init() { - - } - @Override public ClientPermissions getPermissions(String username) { ClientPermissions permissions = new ClientPermissions(); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/permissions/DefaultPermissionsHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/permissions/DefaultPermissionsHandler.java index b91b9632..cca7fc72 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/permissions/DefaultPermissionsHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/permissions/DefaultPermissionsHandler.java @@ -3,10 +3,6 @@ import pro.gravit.launcher.ClientPermissions; public class DefaultPermissionsHandler extends PermissionsHandler { - @Override - public void init() { - - } @Override public ClientPermissions getPermissions(String username) { diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/permissions/HibernatePermissionsHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/permissions/HibernatePermissionsHandler.java new file mode 100644 index 00000000..8298b76a --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/permissions/HibernatePermissionsHandler.java @@ -0,0 +1,27 @@ +package pro.gravit.launchserver.auth.permissions; + +import pro.gravit.launcher.ClientPermissions; +import pro.gravit.launchserver.dao.User; + +public class HibernatePermissionsHandler extends PermissionsHandler { + + @Override + public ClientPermissions getPermissions(String username) { + User user = srv.userService.findUserByUsername(username); + if(user == null) return ClientPermissions.DEFAULT; + return user.getPermissions(); + } + + @Override + public void setPermissions(String username, ClientPermissions permissions) { + User user = srv.userService.findUserByUsername(username); + if(user == null) return; + user.setPermissions(permissions); + srv.userService.updateUser(user); + } + + @Override + public void close() throws Exception { + + } +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/permissions/JsonFilePermissionsHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/permissions/JsonFilePermissionsHandler.java index 76f190f5..a6a4f4d4 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/permissions/JsonFilePermissionsHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/permissions/JsonFilePermissionsHandler.java @@ -13,6 +13,7 @@ import pro.gravit.launcher.ClientPermissions; import pro.gravit.launcher.Launcher; +import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.Reloadable; import pro.gravit.utils.helper.IOHelper; import pro.gravit.utils.helper.LogHelper; @@ -45,7 +46,8 @@ public static class Enity { } @Override - public void init() { + public void init(LaunchServer server) { + super.init(server); Type type = new TypeToken>() { }.getType(); Path path = Paths.get(filename); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/permissions/JsonLongFilePermissionsHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/permissions/JsonLongFilePermissionsHandler.java index b33a5740..324777b6 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/permissions/JsonLongFilePermissionsHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/permissions/JsonLongFilePermissionsHandler.java @@ -13,6 +13,7 @@ import pro.gravit.launcher.ClientPermissions; import pro.gravit.launcher.Launcher; +import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.Reloadable; import pro.gravit.utils.helper.IOHelper; import pro.gravit.utils.helper.LogHelper; @@ -46,7 +47,8 @@ public static class Enity { } @Override - public void init() { + public void init(LaunchServer server) { + super.init(server); Type type = new TypeToken>() { }.getType(); Path path = Paths.get(filename); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/permissions/PermissionsHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/permissions/PermissionsHandler.java index a074869e..9062da26 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/permissions/PermissionsHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/permissions/PermissionsHandler.java @@ -1,10 +1,12 @@ package pro.gravit.launchserver.auth.permissions; import pro.gravit.launcher.ClientPermissions; +import pro.gravit.launchserver.LaunchServer; import pro.gravit.utils.ProviderMap; public abstract class PermissionsHandler implements AutoCloseable { public static ProviderMap providers = new ProviderMap<>("PermissionsHandler"); + protected transient LaunchServer srv; private static boolean registredHandl = false; public static void registerHandlers() { @@ -13,11 +15,15 @@ public static void registerHandlers() { providers.register("json-long", JsonLongFilePermissionsHandler.class); providers.register("config", ConfigPermissionsHandler.class); providers.register("default", DefaultPermissionsHandler.class); + providers.register("hibernate", HibernatePermissionsHandler.class); registredHandl = true; } } - public abstract void init(); + public void init(LaunchServer server) + { + this.srv = server; + } public abstract ClientPermissions getPermissions(String username); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/User.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/User.java index 6a12e571..2d0f6f91 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/User.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/User.java @@ -13,6 +13,7 @@ import javax.persistence.Id; import javax.persistence.Table; +import pro.gravit.launcher.ClientPermissions; import pro.gravit.utils.helper.LogHelper; import pro.gravit.utils.helper.SecurityHelper; @@ -31,6 +32,7 @@ public class User { private String accessToken; public String serverID; private String password_salt; + public long permissions; public void setPassword(String password) { password_salt = SecurityHelper.randomStringAESKey(); @@ -57,6 +59,14 @@ public boolean verifyPassword(String password) LogHelper.info(Arrays.toString(this.password)); return Arrays.equals(enpassword, this.password); } + public ClientPermissions getPermissions() + { + return new ClientPermissions(permissions); + } + public void setPermissions(ClientPermissions permissions) + { + this.permissions = permissions.toLong(); + } public String getAccessToken() { return accessToken; diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/ClientPermissions.java b/LauncherAPI/src/main/java/pro/gravit/launcher/ClientPermissions.java index 90e8ac0a..cd055343 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/ClientPermissions.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/ClientPermissions.java @@ -48,12 +48,12 @@ public ClientPermissions(long data) { @LauncherAPI public long toLong() { long result = 0; - result |= canAdmin ? 0 : 1; - result |= canServer ? 0 : (1 << 1); - result |= canUSR1 ? 0 : (1 << 2); - result |= canUSR2 ? 0 : (1 << 3); - result |= canUSR3 ? 0 : (1 << 4); - result |= canBot ? 0 : (1 << 5); + result |= !canAdmin ? 0 : 1; + result |= !canServer ? 0 : (1 << 1); + result |= !canUSR1 ? 0 : (1 << 2); + result |= !canUSR2 ? 0 : (1 << 3); + result |= !canUSR3 ? 0 : (1 << 4); + result |= !canBot ? 0 : (1 << 5); return result; }