[FEATURE] JsonLongFilePermissionsHandler

This commit is contained in:
Gravit 2019-01-21 20:54:50 +07:00
parent 387c786eef
commit 6ec8267001
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
2 changed files with 66 additions and 0 deletions

View file

@ -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<String, Long> map;
@Override
public void reload() {
map.clear();
Path path = Paths.get(filename);
Type type = new TypeToken<Map<String, Long>>() {
}.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<Map<String, ClientPermissions>>() {
}.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);
}
}
}

View file

@ -31,6 +31,7 @@ public static String getHandlerName(Class<? extends PermissionsHandler> clazz) {
public static void registerHandlers() { public static void registerHandlers() {
if (!registredHandl) { if (!registredHandl) {
registerHandler("json", JsonFilePermissionsHandler.class); registerHandler("json", JsonFilePermissionsHandler.class);
registerHandler("json-long", JsonLongFilePermissionsHandler.class);
registerHandler("config", ConfigPermissionsHandler.class); registerHandler("config", ConfigPermissionsHandler.class);
registerHandler("default", DefaultPermissionsHandler.class); registerHandler("default", DefaultPermissionsHandler.class);
registredHandl = true; registredHandl = true;