Полноценный выбор PermissionsHandler

This commit is contained in:
Gravit 2018-12-26 18:04:45 +07:00
parent 4c1fae008f
commit a606cb23e1
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
4 changed files with 86 additions and 11 deletions

View file

@ -15,6 +15,7 @@
import ru.gravit.launchserver.auth.hwid.AcceptHWIDHandler; import ru.gravit.launchserver.auth.hwid.AcceptHWIDHandler;
import ru.gravit.launchserver.auth.hwid.HWIDHandler; import ru.gravit.launchserver.auth.hwid.HWIDHandler;
import ru.gravit.launchserver.auth.permissions.JsonFilePermissionsHandler; 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.AuthProvider;
import ru.gravit.launchserver.auth.provider.RejectAuthProvider; import ru.gravit.launchserver.auth.provider.RejectAuthProvider;
import ru.gravit.launchserver.binary.EXEL4JLauncherBinary; import ru.gravit.launchserver.binary.EXEL4JLauncherBinary;
@ -24,10 +25,7 @@
import ru.gravit.launchserver.command.handler.CommandHandler; import ru.gravit.launchserver.command.handler.CommandHandler;
import ru.gravit.launchserver.command.handler.JLineCommandHandler; import ru.gravit.launchserver.command.handler.JLineCommandHandler;
import ru.gravit.launchserver.command.handler.StdCommandHandler; import ru.gravit.launchserver.command.handler.StdCommandHandler;
import ru.gravit.launchserver.config.AuthHandlerAdapter; import ru.gravit.launchserver.config.*;
import ru.gravit.launchserver.config.AuthProviderAdapter;
import ru.gravit.launchserver.config.HWIDHandlerAdapter;
import ru.gravit.launchserver.config.TextureProviderAdapter;
import ru.gravit.launchserver.manangers.*; import ru.gravit.launchserver.manangers.*;
import ru.gravit.launchserver.response.Response; import ru.gravit.launchserver.response.Response;
import ru.gravit.launchserver.socket.ServerSocketHandler; import ru.gravit.launchserver.socket.ServerSocketHandler;
@ -64,6 +62,8 @@ public static final class Config {
public AuthProvider[] authProvider; public AuthProvider[] authProvider;
public PermissionsHandler permissionsHandler;
public TextureProvider textureProvider; public TextureProvider textureProvider;
public HWIDHandler hwidHandler; public HWIDHandler hwidHandler;
@ -280,6 +280,7 @@ public LaunchServer(Path dir) throws IOException, InvalidKeySpecException {
AuthProvider.registerProviders(); AuthProvider.registerProviders();
TextureProvider.registerProviders(); TextureProvider.registerProviders();
HWIDHandler.registerHandlers(); HWIDHandler.registerHandlers();
PermissionsHandler.registerHandlers();
Response.registerResponses(); Response.registerResponses();
LaunchServer.server = this; LaunchServer.server = this;
@ -351,8 +352,6 @@ public LaunchServer(Path dir) throws IOException, InvalidKeySpecException {
e.printStackTrace(); e.printStackTrace();
} }
}); });
JsonFilePermissionsHandler.init();
PermissionsManager.registerPermissionsFunction(JsonFilePermissionsHandler::getPermissions);
// init modules // init modules
modulesManager.initModules(); modulesManager.initModules();
@ -387,6 +386,7 @@ public static void initGson()
Launcher.gsonBuilder.registerTypeAdapter(AuthProvider.class, new AuthProviderAdapter()); Launcher.gsonBuilder.registerTypeAdapter(AuthProvider.class, new AuthProviderAdapter());
Launcher.gsonBuilder.registerTypeAdapter(TextureProvider.class, new TextureProviderAdapter()); Launcher.gsonBuilder.registerTypeAdapter(TextureProvider.class, new TextureProviderAdapter());
Launcher.gsonBuilder.registerTypeAdapter(AuthHandler.class, new AuthHandlerAdapter()); Launcher.gsonBuilder.registerTypeAdapter(AuthHandler.class, new AuthHandlerAdapter());
Launcher.gsonBuilder.registerTypeAdapter(PermissionsHandler.class, new PermissionsHandlerAdapter());
Launcher.gsonBuilder.registerTypeAdapter(HWIDHandler.class, new HWIDHandlerAdapter()); Launcher.gsonBuilder.registerTypeAdapter(HWIDHandler.class, new HWIDHandlerAdapter());
Launcher.gson = Launcher.gsonBuilder.create(); Launcher.gson = Launcher.gsonBuilder.create();
@ -396,6 +396,7 @@ public static void initGson()
LaunchServer.gsonBuilder.registerTypeAdapter(AuthProvider.class, new AuthProviderAdapter()); LaunchServer.gsonBuilder.registerTypeAdapter(AuthProvider.class, new AuthProviderAdapter());
LaunchServer.gsonBuilder.registerTypeAdapter(TextureProvider.class, new TextureProviderAdapter()); LaunchServer.gsonBuilder.registerTypeAdapter(TextureProvider.class, new TextureProviderAdapter());
LaunchServer.gsonBuilder.registerTypeAdapter(AuthHandler.class, new AuthHandlerAdapter()); LaunchServer.gsonBuilder.registerTypeAdapter(AuthHandler.class, new AuthHandlerAdapter());
LaunchServer.gsonBuilder.registerTypeAdapter(PermissionsHandler.class, new PermissionsHandlerAdapter());
LaunchServer.gsonBuilder.registerTypeAdapter(HWIDHandler.class, new HWIDHandlerAdapter()); LaunchServer.gsonBuilder.registerTypeAdapter(HWIDHandler.class, new HWIDHandlerAdapter());
LaunchServer.gson = LaunchServer.gsonBuilder.create(); LaunchServer.gson = LaunchServer.gsonBuilder.create();
} }
@ -460,6 +461,7 @@ private void generateConfigIfNotExists() throws IOException {
newConfig.authProvider = new AuthProvider[]{new RejectAuthProvider("Настройте authProvider")}; newConfig.authProvider = new AuthProvider[]{new RejectAuthProvider("Настройте authProvider")};
newConfig.textureProvider = new RequestTextureProvider("http://example.com/skins/%username%.png","http://example.com/cloaks/%username%.png"); newConfig.textureProvider = new RequestTextureProvider("http://example.com/skins/%username%.png","http://example.com/cloaks/%username%.png");
newConfig.permissionsHandler = new JsonFilePermissionsHandler();
newConfig.port = 7420; newConfig.port = 7420;
newConfig.bindAddress = "0.0.0.0"; newConfig.bindAddress = "0.0.0.0";
newConfig.authRejectString = "Превышен лимит авторизаций"; newConfig.authRejectString = "Превышен лимит авторизаций";

View file

@ -3,7 +3,9 @@
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import ru.gravit.launcher.Launcher; import ru.gravit.launcher.Launcher;
import ru.gravit.launchserver.auth.ClientPermissions; import ru.gravit.launchserver.auth.ClientPermissions;
import ru.gravit.launchserver.manangers.PermissionsManager;
import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.LogHelper;
import java.io.IOException; import java.io.IOException;
import java.io.Reader; import java.io.Reader;
@ -14,32 +16,37 @@
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class JsonFilePermissionsHandler { public class JsonFilePermissionsHandler extends PermissionsHandler {
public static String FILE_NAME = "permissions.json"; public String filename = "permissions.json";
public static Map<String,ClientPermissions> map; public static Map<String,ClientPermissions> map;
public static class Enity public static class Enity
{ {
public String username; public String username;
public ClientPermissions permissions; public ClientPermissions permissions;
} }
public static ClientPermissions getPermissions(String username) @Override
public ClientPermissions getPermissions(String username)
{ {
return map.getOrDefault(username,ClientPermissions.DEFAULT); return map.getOrDefault(username,ClientPermissions.DEFAULT);
} }
public static void init() throws IOException { public JsonFilePermissionsHandler() {
Type type = new TypeToken<Map<String,ClientPermissions>>(){}.getType(); Type type = new TypeToken<Map<String,ClientPermissions>>(){}.getType();
Path path = Paths.get(FILE_NAME); Path path = Paths.get(filename);
if(!IOHelper.exists(path)) if(!IOHelper.exists(path))
{ {
map = new HashMap<>(); map = new HashMap<>();
try(Writer writer = IOHelper.newWriter(path)) try(Writer writer = IOHelper.newWriter(path))
{ {
Launcher.gson.toJson(map,writer); Launcher.gson.toJson(map,writer);
} catch (IOException e) {
LogHelper.error(e);
} }
} }
try(Reader reader = IOHelper.newReader(path)) try(Reader reader = IOHelper.newReader(path))
{ {
map = Launcher.gson.fromJson(reader,type); map = Launcher.gson.fromJson(reader,type);
} catch (IOException e) {
LogHelper.error(e);
} }
} }
} }

View file

@ -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);
}

View file

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