From 6ec82670019a7767360c7eeab3d2c29b5d43d424 Mon Sep 17 00:00:00 2001 From: Gravit Date: Mon, 21 Jan 2019 20:54:50 +0700 Subject: [PATCH] [FEATURE] JsonLongFilePermissionsHandler --- .../JsonLongFilePermissionsHandler.java | 65 +++++++++++++++++++ .../auth/permissions/PermissionsHandler.java | 1 + 2 files changed, 66 insertions(+) create mode 100644 LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/JsonLongFilePermissionsHandler.java 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 new file mode 100644 index 00000000..cb6aa234 --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/JsonLongFilePermissionsHandler.java @@ -0,0 +1,65 @@ +package ru.gravit.launchserver.auth.permissions; + +import com.google.gson.reflect.TypeToken; +import ru.gravit.launcher.ClientPermissions; +import ru.gravit.launcher.Launcher; +import ru.gravit.launchserver.Reloadable; +import ru.gravit.utils.helper.IOHelper; +import ru.gravit.utils.helper.LogHelper; + +import java.io.IOException; +import java.io.Reader; +import java.io.Writer; +import java.lang.reflect.Type; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; + +public class JsonLongFilePermissionsHandler extends PermissionsHandler implements Reloadable { + public String filename = "permissions.json"; + public long defaultPerms = 0L; + public static Map map; + + @Override + public void reload() { + map.clear(); + Path path = Paths.get(filename); + Type type = new TypeToken>() { + }.getType(); + try (Reader reader = IOHelper.newReader(path)) { + map = Launcher.gson.fromJson(reader, type); + } catch (IOException e) { + LogHelper.error(e); + } + } + + public static class Enity { + public String username; + public ClientPermissions permissions; + } + + @Override + public ClientPermissions getPermissions(String username) { + return new ClientPermissions(map.getOrDefault(username, defaultPerms)); + } + + public JsonLongFilePermissionsHandler() { + Type type = new TypeToken>() { + }.getType(); + Path path = Paths.get(filename); + if (!IOHelper.exists(path)) { + map = new HashMap<>(); + try (Writer writer = IOHelper.newWriter(path)) { + Launcher.gson.toJson(map, writer); + } catch (IOException e) { + LogHelper.error(e); + } + } + try (Reader reader = IOHelper.newReader(path)) { + map = Launcher.gson.fromJson(reader, type); + } catch (IOException e) { + LogHelper.error(e); + } + } +} 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 db00382a..d3d379e9 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 @@ -31,6 +31,7 @@ public static String getHandlerName(Class clazz) { public static void registerHandlers() { if (!registredHandl) { registerHandler("json", JsonFilePermissionsHandler.class); + registerHandler("json-long", JsonLongFilePermissionsHandler.class); registerHandler("config", ConfigPermissionsHandler.class); registerHandler("default", DefaultPermissionsHandler.class); registredHandl = true;