From 2c54e2bf917f1c983959fde2ad8437008cc32963 Mon Sep 17 00:00:00 2001 From: Sattvik Chakravarthy Date: Fri, 1 Mar 2024 18:02:39 +0530 Subject: [PATCH 1/7] fix: tests --- .../postgresql/test/AccountLinkingTests.java | 4 +-- .../postgresql/test/DbConnectionPoolTest.java | 9 ++++--- .../test/multitenancy/StorageLayerTest.java | 6 ++--- .../TestUserPoolIdChangeBehaviour.java | 27 ++++++++++--------- 4 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/test/java/io/supertokens/storage/postgresql/test/AccountLinkingTests.java b/src/test/java/io/supertokens/storage/postgresql/test/AccountLinkingTests.java index 4f26a52c..580cc049 100644 --- a/src/test/java/io/supertokens/storage/postgresql/test/AccountLinkingTests.java +++ b/src/test/java/io/supertokens/storage/postgresql/test/AccountLinkingTests.java @@ -97,7 +97,7 @@ public void canLinkFailsIfTryingToLinkUsersAcrossDifferentStorageLayers() throws AuthRecipe.createPrimaryUser(process.main, user1.getSupertokensUserId()); AuthRecipeUserInfo user2 = EmailPassword.signUp( - tenantIdentifier.withStorage(StorageLayer.getStorage(tenantIdentifier, process.main)), + tenantIdentifier, (StorageLayer.getStorage(tenantIdentifier, process.main)), process.getProcess(), "test2@example.com", "abcd1234"); try { @@ -135,7 +135,7 @@ public void canLinkFailsIfTryingToLinkUsersAcrossDifferentStorageLayers() throws ); AuthRecipeUserInfo user3 = EmailPassword.signUp( - tenantIdentifier.withStorage(StorageLayer.getStorage(tenantIdentifier, process.main)), + tenantIdentifier, (StorageLayer.getStorage(tenantIdentifier, process.main)), process.getProcess(), "test2@example.com", "abcd1234"); Map params = new HashMap<>(); diff --git a/src/test/java/io/supertokens/storage/postgresql/test/DbConnectionPoolTest.java b/src/test/java/io/supertokens/storage/postgresql/test/DbConnectionPoolTest.java index cdf0c28c..9fd5d942 100644 --- a/src/test/java/io/supertokens/storage/postgresql/test/DbConnectionPoolTest.java +++ b/src/test/java/io/supertokens/storage/postgresql/test/DbConnectionPoolTest.java @@ -24,6 +24,7 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; +import io.supertokens.pluginInterface.Storage; import io.supertokens.pluginInterface.multitenancy.*; import org.junit.AfterClass; import org.junit.Before; @@ -152,8 +153,8 @@ public void testDownTimeWhenChangingConnectionPoolSize() throws Exception { es.execute(() -> { try { TenantIdentifier t1 = new TenantIdentifier(null, null, "t1"); - TenantIdentifierWithStorage t1WithStorage = t1.withStorage(StorageLayer.getStorage(t1, process.getProcess())); - ThirdParty.signInUp(t1WithStorage, process.getProcess(), "google", "googleid"+ finalI, "user" + + Storage t1WithStorage = (StorageLayer.getStorage(t1, process.getProcess())); + ThirdParty.signInUp(t1, t1WithStorage, process.getProcess(), "google", "googleid"+ finalI, "user" + finalI + "@example.com"); if (firstErrorTime.get() != -1 && successAfterErrorTime.get() == -1) { @@ -353,8 +354,8 @@ public void testIdleConnectionTimeout() throws Exception { es.execute(() -> { try { TenantIdentifier t1 = new TenantIdentifier(null, null, "t1"); - TenantIdentifierWithStorage t1WithStorage = t1.withStorage(StorageLayer.getStorage(t1, process.getProcess())); - ThirdParty.signInUp(t1WithStorage, process.getProcess(), "google", "googleid"+ finalI, "user" + + Storage t1WithStorage = (StorageLayer.getStorage(t1, process.getProcess())); + ThirdParty.signInUp(t1, t1WithStorage, process.getProcess(), "google", "googleid"+ finalI, "user" + finalI + "@example.com"); } catch (StorageQueryException e) { diff --git a/src/test/java/io/supertokens/storage/postgresql/test/multitenancy/StorageLayerTest.java b/src/test/java/io/supertokens/storage/postgresql/test/multitenancy/StorageLayerTest.java index afce4e11..7bca0a99 100644 --- a/src/test/java/io/supertokens/storage/postgresql/test/multitenancy/StorageLayerTest.java +++ b/src/test/java/io/supertokens/storage/postgresql/test/multitenancy/StorageLayerTest.java @@ -788,7 +788,7 @@ public void testTenantCreationAndThenDbDownDbThrowsErrorInRecipesAndDoesntAffect MultitenancyHelper.getInstance(process.getProcess()).refreshTenantsInCoreBasedOnChangesInCoreConfigOrIfTenantListChanged(true); try { - EmailPassword.signIn(tid.withStorage(StorageLayer.getStorage(tid, process.getProcess())), + EmailPassword.signIn(tid, (StorageLayer.getStorage(tid, process.getProcess())), process.getProcess(), "", ""); fail(); } catch (StorageQueryException e) { @@ -801,7 +801,7 @@ public void testTenantCreationAndThenDbDownDbThrowsErrorInRecipesAndDoesntAffect // we do this again just to check that if this function is called again, it fails again and there is no // side effect of calling the above function try { - EmailPassword.signIn(tid.withStorage(StorageLayer.getStorage(tid, process.getProcess())), + EmailPassword.signIn(tid, (StorageLayer.getStorage(tid, process.getProcess())), process.getProcess(), "", ""); fail(); } catch (StorageQueryException e) { @@ -830,7 +830,7 @@ public void testTenantCreationAndThenDbDownDbThrowsErrorInRecipesAndDoesntAffect TenantIdentifier tid = new TenantIdentifier("abc", null, null); try { - EmailPassword.signIn(tid.withStorage(StorageLayer.getStorage(tid, process.getProcess())), + EmailPassword.signIn(tid, (StorageLayer.getStorage(tid, process.getProcess())), process.getProcess(), "", ""); fail(); } catch (StorageQueryException e) { diff --git a/src/test/java/io/supertokens/storage/postgresql/test/multitenancy/TestUserPoolIdChangeBehaviour.java b/src/test/java/io/supertokens/storage/postgresql/test/multitenancy/TestUserPoolIdChangeBehaviour.java index 5a1d7a1f..3d1be792 100644 --- a/src/test/java/io/supertokens/storage/postgresql/test/multitenancy/TestUserPoolIdChangeBehaviour.java +++ b/src/test/java/io/supertokens/storage/postgresql/test/multitenancy/TestUserPoolIdChangeBehaviour.java @@ -25,6 +25,7 @@ import io.supertokens.multitenancy.Multitenancy; import io.supertokens.multitenancy.exception.BadPermissionException; import io.supertokens.multitenancy.exception.CannotModifyBaseConfigException; +import io.supertokens.pluginInterface.Storage; import io.supertokens.pluginInterface.authRecipe.AuthRecipeUserInfo; import io.supertokens.pluginInterface.exceptions.InvalidConfigException; import io.supertokens.pluginInterface.exceptions.StorageQueryException; @@ -86,13 +87,13 @@ public void testUsersWorkAfterUserPoolIdChanges() throws Exception { coreConfig ), false); - TenantIdentifierWithStorage tenantIdentifierWithStorage = tenantIdentifier.withStorage( + Storage tenantIdentifierWithStorage = ( StorageLayer.getStorage(tenantIdentifier, process.getProcess())); - String userPoolId = tenantIdentifierWithStorage.getStorage().getUserPoolId(); + String userPoolId = tenantIdentifierWithStorage.getUserPoolId(); AuthRecipeUserInfo userInfo = EmailPassword.signUp( - tenantIdentifierWithStorage, process.getProcess(), "user@example.com", "password"); + tenantIdentifier, tenantIdentifierWithStorage, process.getProcess(), "user@example.com", "password"); coreConfig.addProperty("postgresql_host", "127.0.0.1"); Multitenancy.addNewOrUpdateAppOrTenant(process.getProcess(), new TenantConfig( @@ -103,12 +104,13 @@ public void testUsersWorkAfterUserPoolIdChanges() throws Exception { coreConfig ), false); - tenantIdentifierWithStorage = tenantIdentifier.withStorage( + tenantIdentifierWithStorage = ( StorageLayer.getStorage(tenantIdentifier, process.getProcess())); - String userPoolId2 = tenantIdentifierWithStorage.getStorage().getUserPoolId(); + String userPoolId2 = tenantIdentifierWithStorage.getUserPoolId(); assertNotEquals(userPoolId, userPoolId2); - AuthRecipeUserInfo user2 = EmailPassword.signIn(tenantIdentifierWithStorage, process.getProcess(), + AuthRecipeUserInfo user2 = EmailPassword.signIn( + tenantIdentifier, tenantIdentifierWithStorage, process.getProcess(), "user@example.com", "password"); assertEquals(userInfo, user2); @@ -130,13 +132,13 @@ public void testUsersWorkAfterUserPoolIdChangesAndServerRestart() throws Excepti coreConfig ), false); - TenantIdentifierWithStorage tenantIdentifierWithStorage = tenantIdentifier.withStorage( + Storage tenantIdentifierWithStorage = ( StorageLayer.getStorage(tenantIdentifier, process.getProcess())); - String userPoolId = tenantIdentifierWithStorage.getStorage().getUserPoolId(); + String userPoolId = tenantIdentifierWithStorage.getUserPoolId(); AuthRecipeUserInfo userInfo = EmailPassword.signUp( - tenantIdentifierWithStorage, process.getProcess(), "user@example.com", "password"); + tenantIdentifier, tenantIdentifierWithStorage, process.getProcess(), "user@example.com", "password"); coreConfig.addProperty("postgresql_host", "127.0.0.1"); Multitenancy.addNewOrUpdateAppOrTenant(process.getProcess(), new TenantConfig( @@ -153,12 +155,13 @@ public void testUsersWorkAfterUserPoolIdChangesAndServerRestart() throws Excepti this.process = TestingProcessManager.start(args); assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED)); - tenantIdentifierWithStorage = tenantIdentifier.withStorage( + tenantIdentifierWithStorage = ( StorageLayer.getStorage(tenantIdentifier, process.getProcess())); - String userPoolId2 = tenantIdentifierWithStorage.getStorage().getUserPoolId(); + String userPoolId2 = tenantIdentifierWithStorage.getUserPoolId(); assertNotEquals(userPoolId, userPoolId2); - AuthRecipeUserInfo user2 = EmailPassword.signIn(tenantIdentifierWithStorage, process.getProcess(), + AuthRecipeUserInfo user2 = EmailPassword.signIn( + tenantIdentifier, tenantIdentifierWithStorage, process.getProcess(), "user@example.com", "password"); From adfbb41f724f091c3b7b9a8a689deae603de9ad8 Mon Sep 17 00:00:00 2001 From: Sattvik Chakravarthy Date: Fri, 1 Mar 2024 19:50:06 +0530 Subject: [PATCH 2/7] fix: user role table constraint --- .../storage/postgresql/queries/UserRolesQueries.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/io/supertokens/storage/postgresql/queries/UserRolesQueries.java b/src/main/java/io/supertokens/storage/postgresql/queries/UserRolesQueries.java index 549cac86..4038f500 100644 --- a/src/main/java/io/supertokens/storage/postgresql/queries/UserRolesQueries.java +++ b/src/main/java/io/supertokens/storage/postgresql/queries/UserRolesQueries.java @@ -91,9 +91,6 @@ public static String getQueryToCreateUserRolesTable(Start start) { + "role VARCHAR(255) NOT NULL," + "CONSTRAINT " + Utils.getConstraintName(schema, tableName, null, "pkey") + " PRIMARY KEY(app_id, tenant_id, user_id, role)," - + "CONSTRAINT " + Utils.getConstraintName(schema, tableName, "role", "fkey") - + " FOREIGN KEY(app_id, role)" - + " REFERENCES " + getConfig(start).getRolesTable() + "(app_id, role) ON DELETE CASCADE," + "CONSTRAINT " + Utils.getConstraintName(schema, tableName, "tenant_id", "fkey") + " FOREIGN KEY (app_id, tenant_id)" + " REFERENCES " + Config.getConfig(start).getTenantsTable() + "(app_id, tenant_id) ON DELETE CASCADE" From 02cd41973cb360e109ed2859d1bb73a8c602d0f9 Mon Sep 17 00:00:00 2001 From: Sattvik Chakravarthy Date: Mon, 4 Mar 2024 11:12:59 +0530 Subject: [PATCH 3/7] fix: pr comments --- .../supertokens/storage/postgresql/Start.java | 5 +-- .../postgresql/queries/UserRolesQueries.java | 42 ++++++++++++++++--- .../postgresql/test/DbConnectionPoolTest.java | 8 ++-- .../TestUserPoolIdChangeBehaviour.java | 24 +++++------ 4 files changed, 53 insertions(+), 26 deletions(-) diff --git a/src/main/java/io/supertokens/storage/postgresql/Start.java b/src/main/java/io/supertokens/storage/postgresql/Start.java index 86a7e876..0a82e165 100644 --- a/src/main/java/io/supertokens/storage/postgresql/Start.java +++ b/src/main/java/io/supertokens/storage/postgresql/Start.java @@ -1930,7 +1930,7 @@ public int deleteUserMetadata(AppIdentifier appIdentifier, String userId) throws @Override public void addRoleToUser(TenantIdentifier tenantIdentifier, String userId, String role) - throws StorageQueryException, UnknownRoleException, DuplicateUserRoleMappingException, + throws StorageQueryException, DuplicateUserRoleMappingException, TenantOrAppNotFoundException { try { UserRolesQueries.addRoleToUser(this, tenantIdentifier, userId, role); @@ -1938,9 +1938,6 @@ public void addRoleToUser(TenantIdentifier tenantIdentifier, String userId, Stri if (e instanceof PSQLException) { PostgreSQLConfig config = Config.getConfig(this); ServerErrorMessage serverErrorMessage = ((PSQLException) e).getServerErrorMessage(); - if (isForeignKeyConstraintError(serverErrorMessage, config.getUserRolesTable(), "role")) { - throw new UnknownRoleException(); - } if (isPrimaryKeyError(serverErrorMessage, config.getUserRolesTable())) { throw new DuplicateUserRoleMappingException(); } diff --git a/src/main/java/io/supertokens/storage/postgresql/queries/UserRolesQueries.java b/src/main/java/io/supertokens/storage/postgresql/queries/UserRolesQueries.java index 4038f500..c06e9ceb 100644 --- a/src/main/java/io/supertokens/storage/postgresql/queries/UserRolesQueries.java +++ b/src/main/java/io/supertokens/storage/postgresql/queries/UserRolesQueries.java @@ -17,6 +17,7 @@ package io.supertokens.storage.postgresql.queries; import io.supertokens.pluginInterface.exceptions.StorageQueryException; +import io.supertokens.pluginInterface.exceptions.StorageTransactionLogicException; import io.supertokens.pluginInterface.multitenancy.AppIdentifier; import io.supertokens.pluginInterface.multitenancy.TenantIdentifier; import io.supertokens.storage.postgresql.Start; @@ -141,12 +142,41 @@ public static void addPermissionToRoleOrDoNothingIfExists_Transaction(Start star public static boolean deleteRole(Start start, AppIdentifier appIdentifier, String role) throws SQLException, StorageQueryException { - String QUERY = "DELETE FROM " + getConfig(start).getRolesTable() - + " WHERE app_id = ? AND role = ? ;"; - return update(start, QUERY, pst -> { - pst.setString(1, appIdentifier.getAppId()); - pst.setString(2, role); - }) == 1; + try { + return start.startTransaction(con -> { + boolean deleted = false; + Connection sqlCon = (Connection) con.getConnection(); + try { + { + String QUERY = "DELETE FROM " + getConfig(start).getUserRolesTable() + + " WHERE app_id = ? AND role = ? ;"; + deleted = update(sqlCon, QUERY, pst -> { + pst.setString(1, appIdentifier.getAppId()); + pst.setString(2, role); + }) == 1; + } + { + String QUERY = "DELETE FROM " + getConfig(start).getRolesTable() + + " WHERE app_id = ? AND role = ? ;"; + return update(sqlCon, QUERY, pst -> { + pst.setString(1, appIdentifier.getAppId()); + pst.setString(2, role); + }) == 1 || deleted; + } + } catch (StorageQueryException e) { + throw new StorageTransactionLogicException(e); + } catch (SQLException e) { + throw new StorageTransactionLogicException(e); + } + }); + } catch (StorageTransactionLogicException e) { + if (e.actualException instanceof SQLException) { + throw (SQLException) e.actualException; + } else if (e.actualException instanceof StorageQueryException) { + throw (StorageQueryException) e.actualException; + } + throw new IllegalStateException(e.actualException); + } } public static boolean doesRoleExist(Start start, AppIdentifier appIdentifier, String role) diff --git a/src/test/java/io/supertokens/storage/postgresql/test/DbConnectionPoolTest.java b/src/test/java/io/supertokens/storage/postgresql/test/DbConnectionPoolTest.java index 9fd5d942..c89a5ac4 100644 --- a/src/test/java/io/supertokens/storage/postgresql/test/DbConnectionPoolTest.java +++ b/src/test/java/io/supertokens/storage/postgresql/test/DbConnectionPoolTest.java @@ -153,8 +153,8 @@ public void testDownTimeWhenChangingConnectionPoolSize() throws Exception { es.execute(() -> { try { TenantIdentifier t1 = new TenantIdentifier(null, null, "t1"); - Storage t1WithStorage = (StorageLayer.getStorage(t1, process.getProcess())); - ThirdParty.signInUp(t1, t1WithStorage, process.getProcess(), "google", "googleid"+ finalI, "user" + + Storage t1Storage = (StorageLayer.getStorage(t1, process.getProcess())); + ThirdParty.signInUp(t1, t1Storage, process.getProcess(), "google", "googleid"+ finalI, "user" + finalI + "@example.com"); if (firstErrorTime.get() != -1 && successAfterErrorTime.get() == -1) { @@ -354,8 +354,8 @@ public void testIdleConnectionTimeout() throws Exception { es.execute(() -> { try { TenantIdentifier t1 = new TenantIdentifier(null, null, "t1"); - Storage t1WithStorage = (StorageLayer.getStorage(t1, process.getProcess())); - ThirdParty.signInUp(t1, t1WithStorage, process.getProcess(), "google", "googleid"+ finalI, "user" + + Storage t1Storage = (StorageLayer.getStorage(t1, process.getProcess())); + ThirdParty.signInUp(t1, t1Storage, process.getProcess(), "google", "googleid"+ finalI, "user" + finalI + "@example.com"); } catch (StorageQueryException e) { diff --git a/src/test/java/io/supertokens/storage/postgresql/test/multitenancy/TestUserPoolIdChangeBehaviour.java b/src/test/java/io/supertokens/storage/postgresql/test/multitenancy/TestUserPoolIdChangeBehaviour.java index 3d1be792..bc5a791e 100644 --- a/src/test/java/io/supertokens/storage/postgresql/test/multitenancy/TestUserPoolIdChangeBehaviour.java +++ b/src/test/java/io/supertokens/storage/postgresql/test/multitenancy/TestUserPoolIdChangeBehaviour.java @@ -87,13 +87,13 @@ public void testUsersWorkAfterUserPoolIdChanges() throws Exception { coreConfig ), false); - Storage tenantIdentifierWithStorage = ( + Storage storage = ( StorageLayer.getStorage(tenantIdentifier, process.getProcess())); - String userPoolId = tenantIdentifierWithStorage.getUserPoolId(); + String userPoolId = storage.getUserPoolId(); AuthRecipeUserInfo userInfo = EmailPassword.signUp( - tenantIdentifier, tenantIdentifierWithStorage, process.getProcess(), "user@example.com", "password"); + tenantIdentifier, storage, process.getProcess(), "user@example.com", "password"); coreConfig.addProperty("postgresql_host", "127.0.0.1"); Multitenancy.addNewOrUpdateAppOrTenant(process.getProcess(), new TenantConfig( @@ -104,13 +104,13 @@ public void testUsersWorkAfterUserPoolIdChanges() throws Exception { coreConfig ), false); - tenantIdentifierWithStorage = ( + storage = ( StorageLayer.getStorage(tenantIdentifier, process.getProcess())); - String userPoolId2 = tenantIdentifierWithStorage.getUserPoolId(); + String userPoolId2 = storage.getUserPoolId(); assertNotEquals(userPoolId, userPoolId2); AuthRecipeUserInfo user2 = EmailPassword.signIn( - tenantIdentifier, tenantIdentifierWithStorage, process.getProcess(), + tenantIdentifier, storage, process.getProcess(), "user@example.com", "password"); assertEquals(userInfo, user2); @@ -132,13 +132,13 @@ public void testUsersWorkAfterUserPoolIdChangesAndServerRestart() throws Excepti coreConfig ), false); - Storage tenantIdentifierWithStorage = ( + Storage storage = ( StorageLayer.getStorage(tenantIdentifier, process.getProcess())); - String userPoolId = tenantIdentifierWithStorage.getUserPoolId(); + String userPoolId = storage.getUserPoolId(); AuthRecipeUserInfo userInfo = EmailPassword.signUp( - tenantIdentifier, tenantIdentifierWithStorage, process.getProcess(), "user@example.com", "password"); + tenantIdentifier, storage, process.getProcess(), "user@example.com", "password"); coreConfig.addProperty("postgresql_host", "127.0.0.1"); Multitenancy.addNewOrUpdateAppOrTenant(process.getProcess(), new TenantConfig( @@ -155,13 +155,13 @@ public void testUsersWorkAfterUserPoolIdChangesAndServerRestart() throws Excepti this.process = TestingProcessManager.start(args); assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED)); - tenantIdentifierWithStorage = ( + storage = ( StorageLayer.getStorage(tenantIdentifier, process.getProcess())); - String userPoolId2 = tenantIdentifierWithStorage.getUserPoolId(); + String userPoolId2 = storage.getUserPoolId(); assertNotEquals(userPoolId, userPoolId2); AuthRecipeUserInfo user2 = EmailPassword.signIn( - tenantIdentifier, tenantIdentifierWithStorage, process.getProcess(), + tenantIdentifier, storage, process.getProcess(), "user@example.com", "password"); From ccf6a12447d5bfab19bf38a863dec10fcd5f85c5 Mon Sep 17 00:00:00 2001 From: Sattvik Chakravarthy Date: Mon, 4 Mar 2024 16:16:40 +0530 Subject: [PATCH 4/7] fix: according to updated interface --- .../supertokens/storage/postgresql/Start.java | 16 +++++- .../postgresql/queries/UserRolesQueries.java | 55 +++++++------------ 2 files changed, 33 insertions(+), 38 deletions(-) diff --git a/src/main/java/io/supertokens/storage/postgresql/Start.java b/src/main/java/io/supertokens/storage/postgresql/Start.java index 0a82e165..7e2899e5 100644 --- a/src/main/java/io/supertokens/storage/postgresql/Start.java +++ b/src/main/java/io/supertokens/storage/postgresql/Start.java @@ -2001,9 +2001,21 @@ public String[] getRolesThatHavePermission(AppIdentifier appIdentifier, String p } @Override - public boolean deleteRole(AppIdentifier appIdentifier, String role) throws StorageQueryException { + public boolean deleteRole_Transaction(TransactionConnection con, AppIdentifier appIdentifier, String role) throws StorageQueryException { try { - return UserRolesQueries.deleteRole(this, appIdentifier, role); + return UserRolesQueries.deleteRole_Transaction(this, (Connection) con.getConnection(), appIdentifier, role); + } catch (SQLException e) { + throw new StorageQueryException(e); + } + } + + @Override + public boolean deleteAllUserRoleAssociationsForRole_Transaction(TransactionConnection con, + AppIdentifier appIdentifier, String role) + throws StorageQueryException { + try { + return UserRolesQueries.deleteAllUserRoleAssociationsForRole_Transaction(this, + (Connection) con.getConnection(), appIdentifier, role); } catch (SQLException e) { throw new StorageQueryException(e); } diff --git a/src/main/java/io/supertokens/storage/postgresql/queries/UserRolesQueries.java b/src/main/java/io/supertokens/storage/postgresql/queries/UserRolesQueries.java index c06e9ceb..03a74259 100644 --- a/src/main/java/io/supertokens/storage/postgresql/queries/UserRolesQueries.java +++ b/src/main/java/io/supertokens/storage/postgresql/queries/UserRolesQueries.java @@ -20,6 +20,7 @@ import io.supertokens.pluginInterface.exceptions.StorageTransactionLogicException; import io.supertokens.pluginInterface.multitenancy.AppIdentifier; import io.supertokens.pluginInterface.multitenancy.TenantIdentifier; +import io.supertokens.pluginInterface.sqlStorage.TransactionConnection; import io.supertokens.storage.postgresql.Start; import io.supertokens.storage.postgresql.config.Config; import io.supertokens.storage.postgresql.utils.Utils; @@ -140,43 +141,15 @@ public static void addPermissionToRoleOrDoNothingIfExists_Transaction(Start star }); } - public static boolean deleteRole(Start start, AppIdentifier appIdentifier, String role) + public static boolean deleteRole_Transaction(Start start, Connection sqlCon, AppIdentifier appIdentifier, + String role) throws SQLException, StorageQueryException { - try { - return start.startTransaction(con -> { - boolean deleted = false; - Connection sqlCon = (Connection) con.getConnection(); - try { - { - String QUERY = "DELETE FROM " + getConfig(start).getUserRolesTable() - + " WHERE app_id = ? AND role = ? ;"; - deleted = update(sqlCon, QUERY, pst -> { - pst.setString(1, appIdentifier.getAppId()); - pst.setString(2, role); - }) == 1; - } - { - String QUERY = "DELETE FROM " + getConfig(start).getRolesTable() - + " WHERE app_id = ? AND role = ? ;"; - return update(sqlCon, QUERY, pst -> { - pst.setString(1, appIdentifier.getAppId()); - pst.setString(2, role); - }) == 1 || deleted; - } - } catch (StorageQueryException e) { - throw new StorageTransactionLogicException(e); - } catch (SQLException e) { - throw new StorageTransactionLogicException(e); - } - }); - } catch (StorageTransactionLogicException e) { - if (e.actualException instanceof SQLException) { - throw (SQLException) e.actualException; - } else if (e.actualException instanceof StorageQueryException) { - throw (StorageQueryException) e.actualException; - } - throw new IllegalStateException(e.actualException); - } + String QUERY = "DELETE FROM " + getConfig(start).getRolesTable() + + " WHERE app_id = ? AND role = ? ;"; + return update(sqlCon, QUERY, pst -> { + pst.setString(1, appIdentifier.getAppId()); + pst.setString(2, role); + }) == 1; } public static boolean doesRoleExist(Start start, AppIdentifier appIdentifier, String role) @@ -380,4 +353,14 @@ public static int deleteAllRolesForUser_Transaction(Connection con, Start start, pst.setString(2, userId); }); } + + public static boolean deleteAllUserRoleAssociationsForRole_Transaction(Start start, Connection sqlCon, AppIdentifier appIdentifier, String role) + throws SQLException, StorageQueryException { + String QUERY = "DELETE FROM " + getConfig(start).getUserRolesTable() + + " WHERE app_id = ? AND role = ? ;"; + return update(sqlCon, QUERY, pst -> { + pst.setString(1, appIdentifier.getAppId()); + pst.setString(2, role); + }) >= 1; + } } From b3d3023a64ccf560d5f411e23bbd0edaa43e4aad Mon Sep 17 00:00:00 2001 From: Sattvik Chakravarthy Date: Mon, 4 Mar 2024 16:53:13 +0530 Subject: [PATCH 5/7] fix: user roles --- .../java/io/supertokens/storage/postgresql/Start.java | 10 ++++------ .../storage/postgresql/queries/UserRolesQueries.java | 8 ++++---- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/main/java/io/supertokens/storage/postgresql/Start.java b/src/main/java/io/supertokens/storage/postgresql/Start.java index 7e2899e5..796d3027 100644 --- a/src/main/java/io/supertokens/storage/postgresql/Start.java +++ b/src/main/java/io/supertokens/storage/postgresql/Start.java @@ -2001,21 +2001,19 @@ public String[] getRolesThatHavePermission(AppIdentifier appIdentifier, String p } @Override - public boolean deleteRole_Transaction(TransactionConnection con, AppIdentifier appIdentifier, String role) throws StorageQueryException { + public boolean deleteRole(AppIdentifier appIdentifier, String role) throws StorageQueryException { try { - return UserRolesQueries.deleteRole_Transaction(this, (Connection) con.getConnection(), appIdentifier, role); + return UserRolesQueries.deleteRole(this, appIdentifier, role); } catch (SQLException e) { throw new StorageQueryException(e); } } @Override - public boolean deleteAllUserRoleAssociationsForRole_Transaction(TransactionConnection con, - AppIdentifier appIdentifier, String role) + public boolean deleteAllUserRoleAssociationsForRole(AppIdentifier appIdentifier, String role) throws StorageQueryException { try { - return UserRolesQueries.deleteAllUserRoleAssociationsForRole_Transaction(this, - (Connection) con.getConnection(), appIdentifier, role); + return UserRolesQueries.deleteAllUserRoleAssociationsForRole(this, appIdentifier, role); } catch (SQLException e) { throw new StorageQueryException(e); } diff --git a/src/main/java/io/supertokens/storage/postgresql/queries/UserRolesQueries.java b/src/main/java/io/supertokens/storage/postgresql/queries/UserRolesQueries.java index 03a74259..10fcb1a7 100644 --- a/src/main/java/io/supertokens/storage/postgresql/queries/UserRolesQueries.java +++ b/src/main/java/io/supertokens/storage/postgresql/queries/UserRolesQueries.java @@ -141,12 +141,12 @@ public static void addPermissionToRoleOrDoNothingIfExists_Transaction(Start star }); } - public static boolean deleteRole_Transaction(Start start, Connection sqlCon, AppIdentifier appIdentifier, + public static boolean deleteRole(Start start, AppIdentifier appIdentifier, String role) throws SQLException, StorageQueryException { String QUERY = "DELETE FROM " + getConfig(start).getRolesTable() + " WHERE app_id = ? AND role = ? ;"; - return update(sqlCon, QUERY, pst -> { + return update(start, QUERY, pst -> { pst.setString(1, appIdentifier.getAppId()); pst.setString(2, role); }) == 1; @@ -354,11 +354,11 @@ public static int deleteAllRolesForUser_Transaction(Connection con, Start start, }); } - public static boolean deleteAllUserRoleAssociationsForRole_Transaction(Start start, Connection sqlCon, AppIdentifier appIdentifier, String role) + public static boolean deleteAllUserRoleAssociationsForRole(Start start, AppIdentifier appIdentifier, String role) throws SQLException, StorageQueryException { String QUERY = "DELETE FROM " + getConfig(start).getUserRolesTable() + " WHERE app_id = ? AND role = ? ;"; - return update(sqlCon, QUERY, pst -> { + return update(start, QUERY, pst -> { pst.setString(1, appIdentifier.getAppId()); pst.setString(2, role); }) >= 1; From 520648767e2f91e5f1d6853e3873ae8789939d34 Mon Sep 17 00:00:00 2001 From: Sattvik Chakravarthy Date: Tue, 5 Mar 2024 11:19:10 +0530 Subject: [PATCH 6/7] fix: version and changelog --- CHANGELOG.md | 11 +++++++++++ build.gradle | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f3aa6c8..8508712a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,17 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +## [6.0.0] - 2024-03-05 + +- Implements `deleteAllUserRoleAssociationsForRole` +- Drops `(app_id, role)` foreign key constraint on `user_roles` table + +### Migration + +```sql +ALTER TABLE user_roles DROP CONSTRAINT IF EXISTS user_roles_role_fkey; +``` + ## [5.0.8] - 2024-02-19 - Fixes vulnerabilities in dependencies diff --git a/build.gradle b/build.gradle index 713fefbe..baafed34 100644 --- a/build.gradle +++ b/build.gradle @@ -2,7 +2,7 @@ plugins { id 'java-library' } -version = "5.0.8" +version = "6.0.0" repositories { mavenCentral() From d479a69d5e2cf171e63d4e5b6ff5a1eba32a6216 Mon Sep 17 00:00:00 2001 From: Sattvik Chakravarthy Date: Tue, 5 Mar 2024 13:00:47 +0530 Subject: [PATCH 7/7] fix: plugin interface version --- pluginInterfaceSupported.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pluginInterfaceSupported.json b/pluginInterfaceSupported.json index a5fdc62c..e9d4c148 100644 --- a/pluginInterfaceSupported.json +++ b/pluginInterfaceSupported.json @@ -1,6 +1,6 @@ { "_comment": "contains a list of plugin interfaces branch names that this core supports", "versions": [ - "4.0" + "5.0" ] } \ No newline at end of file