Skip to content

Commit 4bf90e0

Browse files
authored
fix: fixes storage handling for non-auth recipes (#203)
* fix: tests * fix: user role table constraint * fix: pr comments * fix: according to updated interface * fix: user roles * fix: version and changelog * fix: plugin interface version
1 parent 969b945 commit 4bf90e0

File tree

9 files changed

+63
-31
lines changed

9 files changed

+63
-31
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
99

10+
## [6.0.0] - 2024-03-05
11+
12+
- Implements `deleteAllUserRoleAssociationsForRole`
13+
- Drops `(app_id, role)` foreign key constraint on `user_roles` table
14+
15+
### Migration
16+
17+
```sql
18+
ALTER TABLE user_roles DROP CONSTRAINT IF EXISTS user_roles_role_fkey;
19+
```
20+
1021
## [5.0.8] - 2024-02-19
1122

1223
- Fixes vulnerabilities in dependencies

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ plugins {
22
id 'java-library'
33
}
44

5-
version = "5.0.8"
5+
version = "6.0.0"
66

77
repositories {
88
mavenCentral()

pluginInterfaceSupported.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"_comment": "contains a list of plugin interfaces branch names that this core supports",
33
"versions": [
4-
"4.0"
4+
"5.0"
55
]
66
}

src/main/java/io/supertokens/storage/postgresql/Start.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1930,17 +1930,14 @@ public int deleteUserMetadata(AppIdentifier appIdentifier, String userId) throws
19301930

19311931
@Override
19321932
public void addRoleToUser(TenantIdentifier tenantIdentifier, String userId, String role)
1933-
throws StorageQueryException, UnknownRoleException, DuplicateUserRoleMappingException,
1933+
throws StorageQueryException, DuplicateUserRoleMappingException,
19341934
TenantOrAppNotFoundException {
19351935
try {
19361936
UserRolesQueries.addRoleToUser(this, tenantIdentifier, userId, role);
19371937
} catch (SQLException e) {
19381938
if (e instanceof PSQLException) {
19391939
PostgreSQLConfig config = Config.getConfig(this);
19401940
ServerErrorMessage serverErrorMessage = ((PSQLException) e).getServerErrorMessage();
1941-
if (isForeignKeyConstraintError(serverErrorMessage, config.getUserRolesTable(), "role")) {
1942-
throw new UnknownRoleException();
1943-
}
19441941
if (isPrimaryKeyError(serverErrorMessage, config.getUserRolesTable())) {
19451942
throw new DuplicateUserRoleMappingException();
19461943
}
@@ -2012,6 +2009,16 @@ public boolean deleteRole(AppIdentifier appIdentifier, String role) throws Stora
20122009
}
20132010
}
20142011

2012+
@Override
2013+
public boolean deleteAllUserRoleAssociationsForRole(AppIdentifier appIdentifier, String role)
2014+
throws StorageQueryException {
2015+
try {
2016+
return UserRolesQueries.deleteAllUserRoleAssociationsForRole(this, appIdentifier, role);
2017+
} catch (SQLException e) {
2018+
throw new StorageQueryException(e);
2019+
}
2020+
}
2021+
20152022
@Override
20162023
public String[] getRoles(AppIdentifier appIdentifier) throws StorageQueryException {
20172024
try {

src/main/java/io/supertokens/storage/postgresql/queries/UserRolesQueries.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
package io.supertokens.storage.postgresql.queries;
1818

1919
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
20+
import io.supertokens.pluginInterface.exceptions.StorageTransactionLogicException;
2021
import io.supertokens.pluginInterface.multitenancy.AppIdentifier;
2122
import io.supertokens.pluginInterface.multitenancy.TenantIdentifier;
23+
import io.supertokens.pluginInterface.sqlStorage.TransactionConnection;
2224
import io.supertokens.storage.postgresql.Start;
2325
import io.supertokens.storage.postgresql.config.Config;
2426
import io.supertokens.storage.postgresql.utils.Utils;
@@ -91,9 +93,6 @@ public static String getQueryToCreateUserRolesTable(Start start) {
9193
+ "role VARCHAR(255) NOT NULL,"
9294
+ "CONSTRAINT " + Utils.getConstraintName(schema, tableName, null, "pkey")
9395
+ " PRIMARY KEY(app_id, tenant_id, user_id, role),"
94-
+ "CONSTRAINT " + Utils.getConstraintName(schema, tableName, "role", "fkey")
95-
+ " FOREIGN KEY(app_id, role)"
96-
+ " REFERENCES " + getConfig(start).getRolesTable() + "(app_id, role) ON DELETE CASCADE,"
9796
+ "CONSTRAINT " + Utils.getConstraintName(schema, tableName, "tenant_id", "fkey")
9897
+ " FOREIGN KEY (app_id, tenant_id)"
9998
+ " REFERENCES " + Config.getConfig(start).getTenantsTable() + "(app_id, tenant_id) ON DELETE CASCADE"
@@ -142,7 +141,8 @@ public static void addPermissionToRoleOrDoNothingIfExists_Transaction(Start star
142141
});
143142
}
144143

145-
public static boolean deleteRole(Start start, AppIdentifier appIdentifier, String role)
144+
public static boolean deleteRole(Start start, AppIdentifier appIdentifier,
145+
String role)
146146
throws SQLException, StorageQueryException {
147147
String QUERY = "DELETE FROM " + getConfig(start).getRolesTable()
148148
+ " WHERE app_id = ? AND role = ? ;";
@@ -353,4 +353,14 @@ public static int deleteAllRolesForUser_Transaction(Connection con, Start start,
353353
pst.setString(2, userId);
354354
});
355355
}
356+
357+
public static boolean deleteAllUserRoleAssociationsForRole(Start start, AppIdentifier appIdentifier, String role)
358+
throws SQLException, StorageQueryException {
359+
String QUERY = "DELETE FROM " + getConfig(start).getUserRolesTable()
360+
+ " WHERE app_id = ? AND role = ? ;";
361+
return update(start, QUERY, pst -> {
362+
pst.setString(1, appIdentifier.getAppId());
363+
pst.setString(2, role);
364+
}) >= 1;
365+
}
356366
}

src/test/java/io/supertokens/storage/postgresql/test/AccountLinkingTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public void canLinkFailsIfTryingToLinkUsersAcrossDifferentStorageLayers() throws
9797
AuthRecipe.createPrimaryUser(process.main, user1.getSupertokensUserId());
9898

9999
AuthRecipeUserInfo user2 = EmailPassword.signUp(
100-
tenantIdentifier.withStorage(StorageLayer.getStorage(tenantIdentifier, process.main)),
100+
tenantIdentifier, (StorageLayer.getStorage(tenantIdentifier, process.main)),
101101
process.getProcess(), "[email protected]", "abcd1234");
102102

103103
try {
@@ -135,7 +135,7 @@ public void canLinkFailsIfTryingToLinkUsersAcrossDifferentStorageLayers() throws
135135
);
136136

137137
AuthRecipeUserInfo user3 = EmailPassword.signUp(
138-
tenantIdentifier.withStorage(StorageLayer.getStorage(tenantIdentifier, process.main)),
138+
tenantIdentifier, (StorageLayer.getStorage(tenantIdentifier, process.main)),
139139
process.getProcess(), "[email protected]", "abcd1234");
140140

141141
Map<String, String> params = new HashMap<>();

src/test/java/io/supertokens/storage/postgresql/test/DbConnectionPoolTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.concurrent.atomic.AtomicInteger;
2525
import java.util.concurrent.atomic.AtomicLong;
2626

27+
import io.supertokens.pluginInterface.Storage;
2728
import io.supertokens.pluginInterface.multitenancy.*;
2829
import org.junit.AfterClass;
2930
import org.junit.Before;
@@ -152,8 +153,8 @@ public void testDownTimeWhenChangingConnectionPoolSize() throws Exception {
152153
es.execute(() -> {
153154
try {
154155
TenantIdentifier t1 = new TenantIdentifier(null, null, "t1");
155-
TenantIdentifierWithStorage t1WithStorage = t1.withStorage(StorageLayer.getStorage(t1, process.getProcess()));
156-
ThirdParty.signInUp(t1WithStorage, process.getProcess(), "google", "googleid"+ finalI, "user" +
156+
Storage t1Storage = (StorageLayer.getStorage(t1, process.getProcess()));
157+
ThirdParty.signInUp(t1, t1Storage, process.getProcess(), "google", "googleid"+ finalI, "user" +
157158
finalI + "@example.com");
158159

159160
if (firstErrorTime.get() != -1 && successAfterErrorTime.get() == -1) {
@@ -353,8 +354,8 @@ public void testIdleConnectionTimeout() throws Exception {
353354
es.execute(() -> {
354355
try {
355356
TenantIdentifier t1 = new TenantIdentifier(null, null, "t1");
356-
TenantIdentifierWithStorage t1WithStorage = t1.withStorage(StorageLayer.getStorage(t1, process.getProcess()));
357-
ThirdParty.signInUp(t1WithStorage, process.getProcess(), "google", "googleid"+ finalI, "user" +
357+
Storage t1Storage = (StorageLayer.getStorage(t1, process.getProcess()));
358+
ThirdParty.signInUp(t1, t1Storage, process.getProcess(), "google", "googleid"+ finalI, "user" +
358359
finalI + "@example.com");
359360

360361
} catch (StorageQueryException e) {

src/test/java/io/supertokens/storage/postgresql/test/multitenancy/StorageLayerTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ public void testTenantCreationAndThenDbDownDbThrowsErrorInRecipesAndDoesntAffect
788788
MultitenancyHelper.getInstance(process.getProcess()).refreshTenantsInCoreBasedOnChangesInCoreConfigOrIfTenantListChanged(true);
789789

790790
try {
791-
EmailPassword.signIn(tid.withStorage(StorageLayer.getStorage(tid, process.getProcess())),
791+
EmailPassword.signIn(tid, (StorageLayer.getStorage(tid, process.getProcess())),
792792
process.getProcess(), "", "");
793793
fail();
794794
} catch (StorageQueryException e) {
@@ -801,7 +801,7 @@ public void testTenantCreationAndThenDbDownDbThrowsErrorInRecipesAndDoesntAffect
801801
// we do this again just to check that if this function is called again, it fails again and there is no
802802
// side effect of calling the above function
803803
try {
804-
EmailPassword.signIn(tid.withStorage(StorageLayer.getStorage(tid, process.getProcess())),
804+
EmailPassword.signIn(tid, (StorageLayer.getStorage(tid, process.getProcess())),
805805
process.getProcess(), "", "");
806806
fail();
807807
} catch (StorageQueryException e) {
@@ -830,7 +830,7 @@ public void testTenantCreationAndThenDbDownDbThrowsErrorInRecipesAndDoesntAffect
830830

831831
TenantIdentifier tid = new TenantIdentifier("abc", null, null);
832832
try {
833-
EmailPassword.signIn(tid.withStorage(StorageLayer.getStorage(tid, process.getProcess())),
833+
EmailPassword.signIn(tid, (StorageLayer.getStorage(tid, process.getProcess())),
834834
process.getProcess(), "", "");
835835
fail();
836836
} catch (StorageQueryException e) {

src/test/java/io/supertokens/storage/postgresql/test/multitenancy/TestUserPoolIdChangeBehaviour.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import io.supertokens.multitenancy.Multitenancy;
2626
import io.supertokens.multitenancy.exception.BadPermissionException;
2727
import io.supertokens.multitenancy.exception.CannotModifyBaseConfigException;
28+
import io.supertokens.pluginInterface.Storage;
2829
import io.supertokens.pluginInterface.authRecipe.AuthRecipeUserInfo;
2930
import io.supertokens.pluginInterface.exceptions.InvalidConfigException;
3031
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
@@ -86,13 +87,13 @@ public void testUsersWorkAfterUserPoolIdChanges() throws Exception {
8687
coreConfig
8788
), false);
8889

89-
TenantIdentifierWithStorage tenantIdentifierWithStorage = tenantIdentifier.withStorage(
90+
Storage storage = (
9091
StorageLayer.getStorage(tenantIdentifier, process.getProcess()));
9192

92-
String userPoolId = tenantIdentifierWithStorage.getStorage().getUserPoolId();
93+
String userPoolId = storage.getUserPoolId();
9394

9495
AuthRecipeUserInfo userInfo = EmailPassword.signUp(
95-
tenantIdentifierWithStorage, process.getProcess(), "[email protected]", "password");
96+
tenantIdentifier, storage, process.getProcess(), "[email protected]", "password");
9697

9798
coreConfig.addProperty("postgresql_host", "127.0.0.1");
9899
Multitenancy.addNewOrUpdateAppOrTenant(process.getProcess(), new TenantConfig(
@@ -103,12 +104,13 @@ public void testUsersWorkAfterUserPoolIdChanges() throws Exception {
103104
coreConfig
104105
), false);
105106

106-
tenantIdentifierWithStorage = tenantIdentifier.withStorage(
107+
storage = (
107108
StorageLayer.getStorage(tenantIdentifier, process.getProcess()));
108-
String userPoolId2 = tenantIdentifierWithStorage.getStorage().getUserPoolId();
109+
String userPoolId2 = storage.getUserPoolId();
109110
assertNotEquals(userPoolId, userPoolId2);
110111

111-
AuthRecipeUserInfo user2 = EmailPassword.signIn(tenantIdentifierWithStorage, process.getProcess(),
112+
AuthRecipeUserInfo user2 = EmailPassword.signIn(
113+
tenantIdentifier, storage, process.getProcess(),
112114
"[email protected]", "password");
113115

114116
assertEquals(userInfo, user2);
@@ -130,13 +132,13 @@ public void testUsersWorkAfterUserPoolIdChangesAndServerRestart() throws Excepti
130132
coreConfig
131133
), false);
132134

133-
TenantIdentifierWithStorage tenantIdentifierWithStorage = tenantIdentifier.withStorage(
135+
Storage storage = (
134136
StorageLayer.getStorage(tenantIdentifier, process.getProcess()));
135137

136-
String userPoolId = tenantIdentifierWithStorage.getStorage().getUserPoolId();
138+
String userPoolId = storage.getUserPoolId();
137139

138140
AuthRecipeUserInfo userInfo = EmailPassword.signUp(
139-
tenantIdentifierWithStorage, process.getProcess(), "[email protected]", "password");
141+
tenantIdentifier, storage, process.getProcess(), "[email protected]", "password");
140142

141143
coreConfig.addProperty("postgresql_host", "127.0.0.1");
142144
Multitenancy.addNewOrUpdateAppOrTenant(process.getProcess(), new TenantConfig(
@@ -153,12 +155,13 @@ public void testUsersWorkAfterUserPoolIdChangesAndServerRestart() throws Excepti
153155
this.process = TestingProcessManager.start(args);
154156
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));
155157

156-
tenantIdentifierWithStorage = tenantIdentifier.withStorage(
158+
storage = (
157159
StorageLayer.getStorage(tenantIdentifier, process.getProcess()));
158-
String userPoolId2 = tenantIdentifierWithStorage.getStorage().getUserPoolId();
160+
String userPoolId2 = storage.getUserPoolId();
159161
assertNotEquals(userPoolId, userPoolId2);
160162

161-
AuthRecipeUserInfo user2 = EmailPassword.signIn(tenantIdentifierWithStorage, process.getProcess(),
163+
AuthRecipeUserInfo user2 = EmailPassword.signIn(
164+
tenantIdentifier, storage, process.getProcess(),
162165
163166
"password");
164167

0 commit comments

Comments
 (0)