From d11c9e92a53e4c871c118f294a73d6d02f8132c1 Mon Sep 17 00:00:00 2001 From: AlexCatze Date: Sun, 13 Nov 2022 19:52:20 +0100 Subject: [PATCH] [FEATURE] Add permissions support to PostgreSQL This commit adds permissions support to PostgreSQL auth method. --- .../auth/core/PostgresSQLCoreProvider.java | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/PostgresSQLCoreProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/PostgresSQLCoreProvider.java index 9a7c731a..4fa7394c 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/PostgresSQLCoreProvider.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/PostgresSQLCoreProvider.java @@ -35,16 +35,22 @@ public class PostgresSQLCoreProvider extends AuthCoreProvider { public String serverIDColumn; public String table; + public String permissionsTable; + public String permissionsPermissionColumn; + public String permissionsUUIDColumn; + public PasswordVerifier passwordVerifier; public String customQueryByUUIDSQL; public String customQueryByUsernameSQL; public String customQueryByLoginSQL; + public String customQueryPermissionsByUUIDSQL; public String customUpdateAuthSQL; public String customUpdateServerIdSQL; // Prepared SQL queries private transient String queryByUUIDSQL; private transient String queryByUsernameSQL; private transient String queryByLoginSQL; + private transient String queryPermissionsByUUIDSQL; private transient String updateAuthSQL; private transient String updateServerIDSQL; @@ -160,6 +166,9 @@ public void init(LaunchServer server) { userInfoCols, table, usernameColumn); queryByLoginSQL = customQueryByLoginSQL != null ? customQueryByLoginSQL : queryByUsernameSQL; + queryPermissionsByUUIDSQL = customQueryPermissionsByUUIDSQL != null ? customQueryPermissionsByUUIDSQL : String.format("SELECT (%s) FROM %s WHERE %s=?", + permissionsPermissionColumn, permissionsTable, permissionsUUIDColumn); + updateAuthSQL = customUpdateAuthSQL != null ? customUpdateAuthSQL : String.format("UPDATE %s SET %s=?, %s=NULL WHERE %s=?", table, accessTokenColumn, serverIDColumn, uuidColumn); updateServerIDSQL = customUpdateServerIdSQL != null ? customUpdateServerIdSQL : String.format("UPDATE %s SET %s=? WHERE %s=?", @@ -202,7 +211,24 @@ public void close() throws IOException { private PostgresSQLUser constructUser(ResultSet set) throws SQLException { return set.next() ? new PostgresSQLUser(UUID.fromString(set.getString(uuidColumn)), set.getString(usernameColumn), - set.getString(accessTokenColumn), set.getString(serverIDColumn), set.getString(passwordColumn), new ClientPermissions()) : null; + set.getString(accessTokenColumn), set.getString(serverIDColumn), set.getString(passwordColumn), requestPermissions(set.getString(uuidColumn))) : null; + } + + private ClientPermissions requestPermissions (String uuid) throws SQLException + { + try (Connection c = postgresSQLHolder.getConnection()) { + PreparedStatement s = c.prepareStatement(queryPermissionsByUUIDSQL); + s.setString(1, uuid); + s.setQueryTimeout(MySQLSourceConfig.TIMEOUT); + ResultSet set = s.executeQuery(); + ClientPermissions perms = new ClientPermissions(); + while (set.next()) { + perms.addPerm(set.getString(permissionsPermissionColumn)); + } + return perms; + } catch (SQLException e) { + throw new SQLException(e); + } } private User query(String sql, String value) throws IOException {