mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-11-15 03:31:15 +03:00
Полноценный выбор PermissionsHandler
This commit is contained in:
parent
4c1fae008f
commit
a606cb23e1
4 changed files with 86 additions and 11 deletions
|
@ -15,6 +15,7 @@
|
|||
import ru.gravit.launchserver.auth.hwid.AcceptHWIDHandler;
|
||||
import ru.gravit.launchserver.auth.hwid.HWIDHandler;
|
||||
import ru.gravit.launchserver.auth.permissions.JsonFilePermissionsHandler;
|
||||
import ru.gravit.launchserver.auth.permissions.PermissionsHandler;
|
||||
import ru.gravit.launchserver.auth.provider.AuthProvider;
|
||||
import ru.gravit.launchserver.auth.provider.RejectAuthProvider;
|
||||
import ru.gravit.launchserver.binary.EXEL4JLauncherBinary;
|
||||
|
@ -24,10 +25,7 @@
|
|||
import ru.gravit.launchserver.command.handler.CommandHandler;
|
||||
import ru.gravit.launchserver.command.handler.JLineCommandHandler;
|
||||
import ru.gravit.launchserver.command.handler.StdCommandHandler;
|
||||
import ru.gravit.launchserver.config.AuthHandlerAdapter;
|
||||
import ru.gravit.launchserver.config.AuthProviderAdapter;
|
||||
import ru.gravit.launchserver.config.HWIDHandlerAdapter;
|
||||
import ru.gravit.launchserver.config.TextureProviderAdapter;
|
||||
import ru.gravit.launchserver.config.*;
|
||||
import ru.gravit.launchserver.manangers.*;
|
||||
import ru.gravit.launchserver.response.Response;
|
||||
import ru.gravit.launchserver.socket.ServerSocketHandler;
|
||||
|
@ -64,6 +62,8 @@ public static final class Config {
|
|||
|
||||
public AuthProvider[] authProvider;
|
||||
|
||||
public PermissionsHandler permissionsHandler;
|
||||
|
||||
public TextureProvider textureProvider;
|
||||
|
||||
public HWIDHandler hwidHandler;
|
||||
|
@ -280,6 +280,7 @@ public LaunchServer(Path dir) throws IOException, InvalidKeySpecException {
|
|||
AuthProvider.registerProviders();
|
||||
TextureProvider.registerProviders();
|
||||
HWIDHandler.registerHandlers();
|
||||
PermissionsHandler.registerHandlers();
|
||||
Response.registerResponses();
|
||||
LaunchServer.server = this;
|
||||
|
||||
|
@ -351,8 +352,6 @@ public LaunchServer(Path dir) throws IOException, InvalidKeySpecException {
|
|||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
JsonFilePermissionsHandler.init();
|
||||
PermissionsManager.registerPermissionsFunction(JsonFilePermissionsHandler::getPermissions);
|
||||
|
||||
// init modules
|
||||
modulesManager.initModules();
|
||||
|
@ -387,6 +386,7 @@ public static void initGson()
|
|||
Launcher.gsonBuilder.registerTypeAdapter(AuthProvider.class, new AuthProviderAdapter());
|
||||
Launcher.gsonBuilder.registerTypeAdapter(TextureProvider.class, new TextureProviderAdapter());
|
||||
Launcher.gsonBuilder.registerTypeAdapter(AuthHandler.class, new AuthHandlerAdapter());
|
||||
Launcher.gsonBuilder.registerTypeAdapter(PermissionsHandler.class, new PermissionsHandlerAdapter());
|
||||
Launcher.gsonBuilder.registerTypeAdapter(HWIDHandler.class, new HWIDHandlerAdapter());
|
||||
Launcher.gson = Launcher.gsonBuilder.create();
|
||||
|
||||
|
@ -396,6 +396,7 @@ public static void initGson()
|
|||
LaunchServer.gsonBuilder.registerTypeAdapter(AuthProvider.class, new AuthProviderAdapter());
|
||||
LaunchServer.gsonBuilder.registerTypeAdapter(TextureProvider.class, new TextureProviderAdapter());
|
||||
LaunchServer.gsonBuilder.registerTypeAdapter(AuthHandler.class, new AuthHandlerAdapter());
|
||||
LaunchServer.gsonBuilder.registerTypeAdapter(PermissionsHandler.class, new PermissionsHandlerAdapter());
|
||||
LaunchServer.gsonBuilder.registerTypeAdapter(HWIDHandler.class, new HWIDHandlerAdapter());
|
||||
LaunchServer.gson = LaunchServer.gsonBuilder.create();
|
||||
}
|
||||
|
@ -460,6 +461,7 @@ private void generateConfigIfNotExists() throws IOException {
|
|||
|
||||
newConfig.authProvider = new AuthProvider[]{new RejectAuthProvider("Настройте authProvider")};
|
||||
newConfig.textureProvider = new RequestTextureProvider("http://example.com/skins/%username%.png","http://example.com/cloaks/%username%.png");
|
||||
newConfig.permissionsHandler = new JsonFilePermissionsHandler();
|
||||
newConfig.port = 7420;
|
||||
newConfig.bindAddress = "0.0.0.0";
|
||||
newConfig.authRejectString = "Превышен лимит авторизаций";
|
||||
|
|
|
@ -3,7 +3,9 @@
|
|||
import com.google.gson.reflect.TypeToken;
|
||||
import ru.gravit.launcher.Launcher;
|
||||
import ru.gravit.launchserver.auth.ClientPermissions;
|
||||
import ru.gravit.launchserver.manangers.PermissionsManager;
|
||||
import ru.gravit.utils.helper.IOHelper;
|
||||
import ru.gravit.utils.helper.LogHelper;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
|
@ -14,32 +16,37 @@
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class JsonFilePermissionsHandler {
|
||||
public static String FILE_NAME = "permissions.json";
|
||||
public class JsonFilePermissionsHandler extends PermissionsHandler {
|
||||
public String filename = "permissions.json";
|
||||
public static Map<String,ClientPermissions> map;
|
||||
public static class Enity
|
||||
{
|
||||
public String username;
|
||||
public ClientPermissions permissions;
|
||||
}
|
||||
public static ClientPermissions getPermissions(String username)
|
||||
@Override
|
||||
public ClientPermissions getPermissions(String username)
|
||||
{
|
||||
return map.getOrDefault(username,ClientPermissions.DEFAULT);
|
||||
}
|
||||
public static void init() throws IOException {
|
||||
public JsonFilePermissionsHandler() {
|
||||
Type type = new TypeToken<Map<String,ClientPermissions>>(){}.getType();
|
||||
Path path = Paths.get(FILE_NAME);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
package ru.gravit.launchserver.auth.permissions;
|
||||
|
||||
import ru.gravit.launchserver.auth.ClientPermissions;
|
||||
import ru.gravit.utils.helper.VerifyHelper;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public abstract class PermissionsHandler {
|
||||
private static final Map<String, Class> PERMISSIONS_HANDLERS = new ConcurrentHashMap<>(4);
|
||||
private static boolean registredHandl = false;
|
||||
public static void registerHandler(String name, Class adapter) {
|
||||
VerifyHelper.verifyIDName(name);
|
||||
VerifyHelper.putIfAbsent(PERMISSIONS_HANDLERS, name, Objects.requireNonNull(adapter, "adapter"),
|
||||
String.format("Auth handler has been already registered: '%s'", name));
|
||||
}
|
||||
public static Class getHandlerClass(String name)
|
||||
{
|
||||
return PERMISSIONS_HANDLERS.get(name);
|
||||
}
|
||||
public static String getHandlerName(Class clazz)
|
||||
{
|
||||
for(Map.Entry<String,Class> e: PERMISSIONS_HANDLERS.entrySet())
|
||||
{
|
||||
if(e.getValue().equals(clazz)) return e.getKey();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public static void registerHandlers() {
|
||||
if (!registredHandl) {
|
||||
registerHandler("json", JsonFilePermissionsHandler.class);
|
||||
registredHandl = true;
|
||||
}
|
||||
}
|
||||
public abstract ClientPermissions getPermissions(String username);
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package ru.gravit.launchserver.config;
|
||||
|
||||
import com.google.gson.*;
|
||||
import ru.gravit.launchserver.auth.handler.AuthHandler;
|
||||
import ru.gravit.launchserver.auth.permissions.PermissionsHandler;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class PermissionsHandlerAdapter implements JsonSerializer<PermissionsHandler>, JsonDeserializer<PermissionsHandler> {
|
||||
private static final String PROP_NAME = "type";
|
||||
@Override
|
||||
public PermissionsHandler deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
||||
String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString();
|
||||
Class<PermissionsHandler> cls = PermissionsHandler.getHandlerClass(typename);
|
||||
|
||||
|
||||
return (PermissionsHandler) context.deserialize(json, cls);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(PermissionsHandler src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
JsonObject jo = context.serialize(src).getAsJsonObject();
|
||||
|
||||
String classPath = PermissionsHandler.getHandlerName(src.getClass());
|
||||
jo.add(PROP_NAME, new JsonPrimitive(classPath));
|
||||
|
||||
return jo;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue