Skip to content

Commit

Permalink
fix: pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Mar 4, 2024
1 parent adfbb41 commit 02cd419
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 26 deletions.
5 changes: 1 addition & 4 deletions src/main/java/io/supertokens/storage/postgresql/Start.java
Original file line number Diff line number Diff line change
Expand Up @@ -1930,17 +1930,14 @@ 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);
} catch (SQLException e) {
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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(), "[email protected]", "password");
tenantIdentifier, storage, process.getProcess(), "[email protected]", "password");

coreConfig.addProperty("postgresql_host", "127.0.0.1");
Multitenancy.addNewOrUpdateAppOrTenant(process.getProcess(), new TenantConfig(
Expand All @@ -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(),
"[email protected]", "password");

assertEquals(userInfo, user2);
Expand All @@ -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(), "[email protected]", "password");
tenantIdentifier, storage, process.getProcess(), "[email protected]", "password");

coreConfig.addProperty("postgresql_host", "127.0.0.1");
Multitenancy.addNewOrUpdateAppOrTenant(process.getProcess(), new TenantConfig(
Expand All @@ -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(),
"[email protected]",
"password");

Expand Down

0 comments on commit 02cd419

Please sign in to comment.