[FEATURE][FIX] Исправлена сериализация ClientPermissions и добавлен HibernatePermissionsHandler

This commit is contained in:
Gravit 2019-06-07 05:03:42 +07:00
parent 1f10bab874
commit 57da09dee9
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
9 changed files with 58 additions and 20 deletions

View file

@ -241,7 +241,7 @@ public void init() {
for (AuthProviderPair provider : auth) { for (AuthProviderPair provider : auth) {
provider.init(server); provider.init(server);
} }
permissionsHandler.init(); permissionsHandler.init(server);
hwidHandler.init(); hwidHandler.init();
if (protectHandler != null) { if (protectHandler != null) {
protectHandler.checkLaunchServerLicense(); protectHandler.checkLaunchServerLicense();
@ -590,7 +590,7 @@ public LaunchServer(Path dir, boolean testEnv, String[] args) throws IOException
for (AuthProviderPair provider : config.auth) { for (AuthProviderPair provider : config.auth) {
provider.init(this); provider.init(this);
} }
config.permissionsHandler.init(); config.permissionsHandler.init(this);
config.hwidHandler.init(); config.hwidHandler.init();
if (config.protectHandler != null) { if (config.protectHandler != null) {
config.protectHandler.checkLaunchServerLicense(); config.protectHandler.checkLaunchServerLicense();

View file

@ -6,11 +6,6 @@ public class ConfigPermissionsHandler extends PermissionsHandler {
public boolean isAdmin = false; public boolean isAdmin = false;
public boolean isServer = false; public boolean isServer = false;
@Override
public void init() {
}
@Override @Override
public ClientPermissions getPermissions(String username) { public ClientPermissions getPermissions(String username) {
ClientPermissions permissions = new ClientPermissions(); ClientPermissions permissions = new ClientPermissions();

View file

@ -3,10 +3,6 @@
import pro.gravit.launcher.ClientPermissions; import pro.gravit.launcher.ClientPermissions;
public class DefaultPermissionsHandler extends PermissionsHandler { public class DefaultPermissionsHandler extends PermissionsHandler {
@Override
public void init() {
}
@Override @Override
public ClientPermissions getPermissions(String username) { public ClientPermissions getPermissions(String username) {

View file

@ -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 {
}
}

View file

@ -13,6 +13,7 @@
import pro.gravit.launcher.ClientPermissions; import pro.gravit.launcher.ClientPermissions;
import pro.gravit.launcher.Launcher; import pro.gravit.launcher.Launcher;
import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.Reloadable; import pro.gravit.launchserver.Reloadable;
import pro.gravit.utils.helper.IOHelper; import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.LogHelper; import pro.gravit.utils.helper.LogHelper;
@ -45,7 +46,8 @@ public static class Enity {
} }
@Override @Override
public void init() { public void init(LaunchServer server) {
super.init(server);
Type type = new TypeToken<Map<String, ClientPermissions>>() { Type type = new TypeToken<Map<String, ClientPermissions>>() {
}.getType(); }.getType();
Path path = Paths.get(filename); Path path = Paths.get(filename);

View file

@ -13,6 +13,7 @@
import pro.gravit.launcher.ClientPermissions; import pro.gravit.launcher.ClientPermissions;
import pro.gravit.launcher.Launcher; import pro.gravit.launcher.Launcher;
import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.Reloadable; import pro.gravit.launchserver.Reloadable;
import pro.gravit.utils.helper.IOHelper; import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.LogHelper; import pro.gravit.utils.helper.LogHelper;
@ -46,7 +47,8 @@ public static class Enity {
} }
@Override @Override
public void init() { public void init(LaunchServer server) {
super.init(server);
Type type = new TypeToken<Map<String, ClientPermissions>>() { Type type = new TypeToken<Map<String, ClientPermissions>>() {
}.getType(); }.getType();
Path path = Paths.get(filename); Path path = Paths.get(filename);

View file

@ -1,10 +1,12 @@
package pro.gravit.launchserver.auth.permissions; package pro.gravit.launchserver.auth.permissions;
import pro.gravit.launcher.ClientPermissions; import pro.gravit.launcher.ClientPermissions;
import pro.gravit.launchserver.LaunchServer;
import pro.gravit.utils.ProviderMap; import pro.gravit.utils.ProviderMap;
public abstract class PermissionsHandler implements AutoCloseable { public abstract class PermissionsHandler implements AutoCloseable {
public static ProviderMap<PermissionsHandler> providers = new ProviderMap<>("PermissionsHandler"); public static ProviderMap<PermissionsHandler> providers = new ProviderMap<>("PermissionsHandler");
protected transient LaunchServer srv;
private static boolean registredHandl = false; private static boolean registredHandl = false;
public static void registerHandlers() { public static void registerHandlers() {
@ -13,11 +15,15 @@ public static void registerHandlers() {
providers.register("json-long", JsonLongFilePermissionsHandler.class); providers.register("json-long", JsonLongFilePermissionsHandler.class);
providers.register("config", ConfigPermissionsHandler.class); providers.register("config", ConfigPermissionsHandler.class);
providers.register("default", DefaultPermissionsHandler.class); providers.register("default", DefaultPermissionsHandler.class);
providers.register("hibernate", HibernatePermissionsHandler.class);
registredHandl = true; registredHandl = true;
} }
} }
public abstract void init(); public void init(LaunchServer server)
{
this.srv = server;
}
public abstract ClientPermissions getPermissions(String username); public abstract ClientPermissions getPermissions(String username);

View file

@ -13,6 +13,7 @@
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.Table; import javax.persistence.Table;
import pro.gravit.launcher.ClientPermissions;
import pro.gravit.utils.helper.LogHelper; import pro.gravit.utils.helper.LogHelper;
import pro.gravit.utils.helper.SecurityHelper; import pro.gravit.utils.helper.SecurityHelper;
@ -31,6 +32,7 @@ public class User {
private String accessToken; private String accessToken;
public String serverID; public String serverID;
private String password_salt; private String password_salt;
public long permissions;
public void setPassword(String password) public void setPassword(String password)
{ {
password_salt = SecurityHelper.randomStringAESKey(); password_salt = SecurityHelper.randomStringAESKey();
@ -57,6 +59,14 @@ public boolean verifyPassword(String password)
LogHelper.info(Arrays.toString(this.password)); LogHelper.info(Arrays.toString(this.password));
return Arrays.equals(enpassword, 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() { public String getAccessToken() {
return accessToken; return accessToken;

View file

@ -48,12 +48,12 @@ public ClientPermissions(long data) {
@LauncherAPI @LauncherAPI
public long toLong() { public long toLong() {
long result = 0; long result = 0;
result |= canAdmin ? 0 : 1; result |= !canAdmin ? 0 : 1;
result |= canServer ? 0 : (1 << 1); result |= !canServer ? 0 : (1 << 1);
result |= canUSR1 ? 0 : (1 << 2); result |= !canUSR1 ? 0 : (1 << 2);
result |= canUSR2 ? 0 : (1 << 3); result |= !canUSR2 ? 0 : (1 << 3);
result |= canUSR3 ? 0 : (1 << 4); result |= !canUSR3 ? 0 : (1 << 4);
result |= canBot ? 0 : (1 << 5); result |= !canBot ? 0 : (1 << 5);
return result; return result;
} }