diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/protect/StdProtectHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/protect/StdProtectHandler.java index 0815c230..80e758f1 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/protect/StdProtectHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/protect/StdProtectHandler.java @@ -50,11 +50,11 @@ public boolean canGetUpdates(String updatesDirName, Client client) { private boolean isWhitelisted(String property, ClientProfile profile, Client client) { if(client.permissions != null) { String permByUUID = String.format(property, profile.getUUID()); - if(client.permissions.hasAction(permByUUID)) { + if(client.permissions.hasPerm(permByUUID)) { return true; } String permByTitle = String.format(property, profile.getTitle().toLowerCase(Locale.ROOT)); - if(client.permissions.hasAction(permByTitle)) { + if(client.permissions.hasPerm(permByTitle)) { return true; } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/AuthManager.java b/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/AuthManager.java index 042ad606..b157a09a 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/AuthManager.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/AuthManager.java @@ -85,8 +85,8 @@ public boolean accept(Client client, AuthProviderPair pair, String extendedToken client.auth_id = info.authId; client.auth = server.config.getAuthProviderPair(info.authId); if(client.permissions == null) client.permissions = new ClientPermissions(); - client.permissions.addAction("launchserver.checkserver"); - client.permissions.addAction(String.format("launchserver.profiles.%s.show", info.serverName)); + client.permissions.addPerm("launchserver.checkserver"); + client.permissions.addPerm(String.format("launchserver.profiles.%s.show", info.serverName)); return true; } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/CheckServerResponse.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/CheckServerResponse.java index 02342053..82767345 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/CheckServerResponse.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/CheckServerResponse.java @@ -5,7 +5,6 @@ import org.apache.logging.log4j.Logger; import pro.gravit.launcher.events.request.CheckServerRequestEvent; import pro.gravit.launchserver.auth.AuthException; -import pro.gravit.launchserver.auth.AuthProviderPair; import pro.gravit.launchserver.manangers.AuthManager; import pro.gravit.launchserver.socket.Client; import pro.gravit.launchserver.socket.response.SimpleResponse; @@ -24,7 +23,7 @@ public String getType() { @Override public void execute(ChannelHandlerContext ctx, Client pClient) { - if (pClient.permissions == null || !pClient.permissions.hasAction("launchserver.checkserver")) { + if (pClient.permissions == null || !pClient.permissions.hasPerm("launchserver.checkserver")) { sendError("Permissions denied"); return; } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/ExitResponse.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/ExitResponse.java index 7b196f1d..94effdc4 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/ExitResponse.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/ExitResponse.java @@ -34,7 +34,7 @@ public String getType() { @Override public void execute(ChannelHandlerContext ctx, Client client) { - if (username != null && (!client.isAuth || client.permissions == null || !client.permissions.hasAction("launchserver\\.management\\.kick"))) { + if (username != null && (!client.isAuth || client.permissions == null || !client.permissions.hasPerm("launchserver\\.management\\.kick"))) { sendError("Permissions denied"); return; } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/management/PingServerReportResponse.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/management/PingServerReportResponse.java index 88b18396..f7c5fcdf 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/management/PingServerReportResponse.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/management/PingServerReportResponse.java @@ -17,7 +17,7 @@ public String getType() { @Override public void execute(ChannelHandlerContext ctx, Client client) { - if (!client.isAuth || client.permissions == null || !client.permissions.hasAction("launchserver\\.management\\.pingserver")) { + if (!client.isAuth || client.permissions == null || !client.permissions.hasPerm("launchserver\\.management\\.pingserver")) { sendError("Access denied"); return; } diff --git a/Launcher/src/main/java/pro/gravit/launcher/api/AuthService.java b/Launcher/src/main/java/pro/gravit/launcher/api/AuthService.java index 1a1c0818..93b90de9 100644 --- a/Launcher/src/main/java/pro/gravit/launcher/api/AuthService.java +++ b/Launcher/src/main/java/pro/gravit/launcher/api/AuthService.java @@ -12,6 +12,6 @@ public class AuthService { public static ClientProfile profile; public static boolean hasPermission(String permission) { - return permissions.hasAction(permission); + return permissions.hasPerm(permission); } } diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/ClientPermissions.java b/LauncherAPI/src/main/java/pro/gravit/launcher/ClientPermissions.java index b7772452..96938403 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/ClientPermissions.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/ClientPermissions.java @@ -16,7 +16,7 @@ public class ClientPermissions { @LauncherNetworkAPI private List roles; @LauncherNetworkAPI - private List actions; + private List perms; private transient List available; @@ -40,7 +40,7 @@ public ClientPermissions(long permissions, long flags) { public static ClientPermissions getSuperuserAccount() { ClientPermissions perm = new ClientPermissions(); perm.setPermission(PermissionConsts.ADMIN, true); - perm.addAction("*"); + perm.addPerm("*"); return perm; } @@ -56,8 +56,8 @@ public synchronized void compile() { if (available != null) { return; } - available = new ArrayList<>(actions.size()); - for (String a : actions) { + available = new ArrayList<>(perms.size()); + for (String a : perms) { available.add(new PermissionPattern(a)); } if (permissions != 0) { @@ -68,7 +68,7 @@ public synchronized void compile() { } } - public boolean hasAction(String action) { + public boolean hasPerm(String action) { if (available == null) { compile(); } @@ -87,23 +87,34 @@ public void addRole(String role) { roles.add(role); } - public void addAction(String action) { - if (actions == null) { - actions = new ArrayList<>(1); + public void addPerm(String perm) { + if (perms == null) { + perms = new ArrayList<>(1); } - actions.add(action); + perms.add(perm); if(available == null) { available = new ArrayList<>(1); } - available.add(new PermissionPattern(action)); + available.add(new PermissionPattern(perm)); + } + + public void removePerm(String action) { + if (perms == null) { + return; + } + if(available == null) { + return; + } + perms.remove(action); + available.remove(new PermissionPattern(action)); } public List getRoles() { return roles; } - public List getActions() { - return actions; + public List getPerms() { + return perms; } //Read methods @@ -156,7 +167,7 @@ public final void setFlag(long mask, boolean value) { public String toString() { return "ClientPermissions{" + "roles=" + String.join(", ", roles == null ? Collections.emptyList() : roles) + - ", actions=" + String.join(", ", actions == null ? Collections.emptyList() : actions) + + ", actions=" + String.join(", ", perms == null ? Collections.emptyList() : perms) + '}'; } @@ -232,5 +243,20 @@ public boolean match(String str) { } return true; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + PermissionPattern that = (PermissionPattern) o; + return priority == that.priority && Arrays.equals(parts, that.parts); + } + + @Override + public int hashCode() { + int result = Objects.hash(priority); + result = 31 * result + Arrays.hashCode(parts); + return result; + } } } diff --git a/LauncherAPI/src/test/java/pro/gravit/launcher/PermissionTest.java b/LauncherAPI/src/test/java/pro/gravit/launcher/PermissionTest.java index 048a0cfd..74baf10c 100644 --- a/LauncherAPI/src/test/java/pro/gravit/launcher/PermissionTest.java +++ b/LauncherAPI/src/test/java/pro/gravit/launcher/PermissionTest.java @@ -8,43 +8,43 @@ public class PermissionTest { public void testPermission() { { ClientPermissions permissions = new ClientPermissions(); - permissions.addAction("*"); - Assertions.assertTrue(permissions.hasAction("abcd")); - Assertions.assertTrue(permissions.hasAction("t.a.c.d.f.*")); - Assertions.assertTrue(permissions.hasAction("*")); + permissions.addPerm("*"); + Assertions.assertTrue(permissions.hasPerm("abcd")); + Assertions.assertTrue(permissions.hasPerm("t.a.c.d.f.*")); + Assertions.assertTrue(permissions.hasPerm("*")); } { ClientPermissions permissions = new ClientPermissions(); - permissions.addAction("launchserver.*"); - Assertions.assertTrue(permissions.hasAction("launchserver.*")); - Assertions.assertTrue(permissions.hasAction("launchserver.abcd")); - Assertions.assertFalse(permissions.hasAction("default.abcd")); - Assertions.assertFalse(permissions.hasAction("nolaunchserver.abcd")); + permissions.addPerm("launchserver.*"); + Assertions.assertTrue(permissions.hasPerm("launchserver.*")); + Assertions.assertTrue(permissions.hasPerm("launchserver.abcd")); + Assertions.assertFalse(permissions.hasPerm("default.abcd")); + Assertions.assertFalse(permissions.hasPerm("nolaunchserver.abcd")); } { ClientPermissions permissions = new ClientPermissions(); - permissions.addAction("launchserver.*.prop"); - Assertions.assertTrue(permissions.hasAction("launchserver.ii.prop")); - Assertions.assertTrue(permissions.hasAction("launchserver.ia.prop")); - Assertions.assertFalse(permissions.hasAction("default.abcd")); - Assertions.assertFalse(permissions.hasAction("launchserver.ia")); - Assertions.assertFalse(permissions.hasAction("launchserver.ia.prop2")); + permissions.addPerm("launchserver.*.prop"); + Assertions.assertTrue(permissions.hasPerm("launchserver.ii.prop")); + Assertions.assertTrue(permissions.hasPerm("launchserver.ia.prop")); + Assertions.assertFalse(permissions.hasPerm("default.abcd")); + Assertions.assertFalse(permissions.hasPerm("launchserver.ia")); + Assertions.assertFalse(permissions.hasPerm("launchserver.ia.prop2")); } { ClientPermissions permissions = new ClientPermissions(); - permissions.addAction("launchserver.*.def.*.prop"); - Assertions.assertTrue(permissions.hasAction("launchserver.1.def.2.prop")); - Assertions.assertTrue(permissions.hasAction("launchserver.none.def.none.prop")); - Assertions.assertTrue(permissions.hasAction("launchserver.def.def.def.prop")); - Assertions.assertFalse(permissions.hasAction("launchserver.*.*.prop")); - Assertions.assertFalse(permissions.hasAction("launchserver.*.undef.*.prop")); + permissions.addPerm("launchserver.*.def.*.prop"); + Assertions.assertTrue(permissions.hasPerm("launchserver.1.def.2.prop")); + Assertions.assertTrue(permissions.hasPerm("launchserver.none.def.none.prop")); + Assertions.assertTrue(permissions.hasPerm("launchserver.def.def.def.prop")); + Assertions.assertFalse(permissions.hasPerm("launchserver.*.*.prop")); + Assertions.assertFalse(permissions.hasPerm("launchserver.*.undef.*.prop")); } { ClientPermissions permissions = new ClientPermissions(); - permissions.addAction("launchserver.*.e.*.i.*.prop"); - Assertions.assertTrue(permissions.hasAction("launchserver.2.e.3.i.4.prop")); - Assertions.assertTrue(permissions.hasAction("launchserver.12212.e.233455.i.2356436346346345345345345.prop")); - Assertions.assertFalse(permissions.hasAction("launchserver.prop")); + permissions.addPerm("launchserver.*.e.*.i.*.prop"); + Assertions.assertTrue(permissions.hasPerm("launchserver.2.e.3.i.4.prop")); + Assertions.assertTrue(permissions.hasPerm("launchserver.12212.e.233455.i.2356436346346345345345345.prop")); + Assertions.assertFalse(permissions.hasPerm("launchserver.prop")); } } } diff --git a/modules b/modules index a6c43731..bfcab2ff 160000 --- a/modules +++ b/modules @@ -1 +1 @@ -Subproject commit a6c43731df536111b76e065426b085742ffbbd68 +Subproject commit bfcab2ff667d9a53fdee2a9d2fd8d5246eb619b2