Skip to content

Commit

Permalink
fix: pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Aug 31, 2023
1 parent bea0627 commit 3a754bb
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 126 deletions.
108 changes: 45 additions & 63 deletions src/main/java/io/supertokens/storage/postgresql/Start.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import io.supertokens.pluginInterface.multitenancy.exceptions.DuplicateTenantException;
import io.supertokens.pluginInterface.multitenancy.exceptions.DuplicateThirdPartyIdException;
import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException;
import io.supertokens.pluginInterface.multitenancy.sqlStorage.MultitenancySQLStorage;
import io.supertokens.pluginInterface.passwordless.PasswordlessCode;
import io.supertokens.pluginInterface.passwordless.PasswordlessDevice;
import io.supertokens.pluginInterface.passwordless.exception.*;
Expand Down Expand Up @@ -107,7 +108,7 @@
public class Start
implements SessionSQLStorage, EmailPasswordSQLStorage, EmailVerificationSQLStorage, ThirdPartySQLStorage,
JWTRecipeSQLStorage, PasswordlessSQLStorage, UserMetadataSQLStorage, UserRolesSQLStorage, UserIdMappingStorage,
MultitenancyStorage, DashboardSQLStorage, TOTPSQLStorage, ActiveUsersStorage, AuthRecipeSQLStorage {
MultitenancyStorage, MultitenancySQLStorage, DashboardSQLStorage, TOTPSQLStorage, ActiveUsersStorage, AuthRecipeSQLStorage {

// these configs are protected from being modified / viewed by the dev using the SuperTokens
// SaaS. If the core is not running in SuperTokens SaaS, this array has no effect.
Expand Down Expand Up @@ -2337,76 +2338,57 @@ public TenantConfig[] getAllTenants() throws StorageQueryException {
}

@Override
public boolean addUserIdToTenant(TenantIdentifier tenantIdentifier, String userId)
public boolean addUserIdToTenant_Transaction(TenantIdentifier tenantIdentifier, TransactionConnection con, String userId)
throws TenantOrAppNotFoundException, UnknownUserIdException, StorageQueryException,
DuplicateEmailException, DuplicateThirdPartyUserException, DuplicatePhoneNumberException {
Connection sqlCon = (Connection) con.getConnection();
try {
return this.startTransaction(con -> {
Connection sqlCon = (Connection) con.getConnection();
try {
String recipeId = GeneralQueries.getRecipeIdForUser_Transaction(this, sqlCon, tenantIdentifier,
userId);

if (recipeId == null) {
throw new StorageTransactionLogicException(new UnknownUserIdException());
}
String recipeId = GeneralQueries.getRecipeIdForUser_Transaction(this, sqlCon, tenantIdentifier,
userId);

boolean added;
if (recipeId.equals("emailpassword")) {
added = EmailPasswordQueries.addUserIdToTenant_Transaction(this, sqlCon, tenantIdentifier,
userId);
} else if (recipeId.equals("thirdparty")) {
added = ThirdPartyQueries.addUserIdToTenant_Transaction(this, sqlCon, tenantIdentifier, userId);
} else if (recipeId.equals("passwordless")) {
added = PasswordlessQueries.addUserIdToTenant_Transaction(this, sqlCon, tenantIdentifier,
userId);
} else {
throw new IllegalStateException("Should never come here!");
}
if (recipeId == null) {
throw new UnknownUserIdException();
}

sqlCon.commit();
return added;
} catch (SQLException | DuplicateEmailException | DuplicatePhoneNumberException | DuplicateThirdPartyUserException throwables) {
throw new StorageTransactionLogicException(throwables);
}
});
} catch (StorageTransactionLogicException e) {
if (e.actualException instanceof SQLException) {
PostgreSQLConfig config = Config.getConfig(this);
ServerErrorMessage serverErrorMessage = ((PSQLException) e.actualException).getServerErrorMessage();
boolean added;
if (recipeId.equals("emailpassword")) {
added = EmailPasswordQueries.addUserIdToTenant_Transaction(this, sqlCon, tenantIdentifier,
userId);
} else if (recipeId.equals("thirdparty")) {
added = ThirdPartyQueries.addUserIdToTenant_Transaction(this, sqlCon, tenantIdentifier, userId);
} else if (recipeId.equals("passwordless")) {
added = PasswordlessQueries.addUserIdToTenant_Transaction(this, sqlCon, tenantIdentifier,
userId);
} else {
throw new IllegalStateException("Should never come here!");
}

if (isForeignKeyConstraintError(serverErrorMessage, config.getUsersTable(), "tenant_id")) {
throw new TenantOrAppNotFoundException(tenantIdentifier);
}
if (isUniqueConstraintError(serverErrorMessage, config.getEmailPasswordUserToTenantTable(), "email")) {
throw new DuplicateEmailException();
}
if (isUniqueConstraintError(serverErrorMessage, config.getThirdPartyUserToTenantTable(),
"third_party_user_id")) {
throw new DuplicateThirdPartyUserException();
}
if (isUniqueConstraintError(serverErrorMessage,
Config.getConfig(this).getPasswordlessUserToTenantTable(), "phone_number")) {
throw new DuplicatePhoneNumberException();
}
if (isUniqueConstraintError(serverErrorMessage,
Config.getConfig(this).getPasswordlessUserToTenantTable(), "email")) {
throw new DuplicateEmailException();
}
sqlCon.commit();
return added;
} catch (SQLException throwables) {
PostgreSQLConfig config = Config.getConfig(this);
ServerErrorMessage serverErrorMessage = ((PSQLException) throwables).getServerErrorMessage();

throw new StorageQueryException(e.actualException);
} else if (e.actualException instanceof UnknownUserIdException) {
throw (UnknownUserIdException) e.actualException;
} else if (e.actualException instanceof StorageQueryException) {
throw (StorageQueryException) e.actualException;
} else if (e.actualException instanceof DuplicateEmailException) {
throw (DuplicateEmailException) e.actualException;
} else if (e.actualException instanceof DuplicatePhoneNumberException) {
throw (DuplicatePhoneNumberException) e.actualException;
} else if (e.actualException instanceof DuplicateThirdPartyUserException) {
throw (DuplicateThirdPartyUserException) e.actualException;
if (isForeignKeyConstraintError(serverErrorMessage, config.getUsersTable(), "tenant_id")) {
throw new TenantOrAppNotFoundException(tenantIdentifier);
}
throw new StorageQueryException(e.actualException);
if (isUniqueConstraintError(serverErrorMessage, config.getEmailPasswordUserToTenantTable(), "email")) {
throw new DuplicateEmailException();
}
if (isUniqueConstraintError(serverErrorMessage, config.getThirdPartyUserToTenantTable(),
"third_party_user_id")) {
throw new DuplicateThirdPartyUserException();
}
if (isUniqueConstraintError(serverErrorMessage,
Config.getConfig(this).getPasswordlessUserToTenantTable(), "phone_number")) {
throw new DuplicatePhoneNumberException();
}
if (isUniqueConstraintError(serverErrorMessage,
Config.getConfig(this).getPasswordlessUserToTenantTable(), "email")) {
throw new DuplicateEmailException();
}

throw new StorageQueryException(throwables);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,21 +481,13 @@ public static List<String> getPrimaryUserIdsUsingEmail(Start start, Connection c
return userIds;
});
}

public static boolean addUserIdToTenant_Transaction(Start start, Connection sqlCon,
TenantIdentifier tenantIdentifier, String userId)
throws SQLException, StorageQueryException, DuplicateEmailException {
UserInfoPartial userInfo = EmailPasswordQueries.getUserInfoUsingId(start, sqlCon,
tenantIdentifier.toAppIdentifier(), userId);

AuthRecipeUserInfo[] primaryUsers = GeneralQueries.listPrimaryUsersByEmail_Transaction(start, sqlCon,
tenantIdentifier.toAppIdentifier(), userInfo.email);

for (AuthRecipeUserInfo primaryUser : primaryUsers) {
if (primaryUser.tenantIds.contains(tenantIdentifier.getTenantId()) && !primaryUser.getSupertokensUserId().equals(userInfo.id)) {
throw new DuplicateEmailException();
}
}

{ // all_auth_recipe_users
String QUERY = "INSERT INTO " + getConfig(start).getUsersTable()
+ "(app_id, tenant_id, user_id, primary_or_recipe_user_id, recipe_id, time_joined, primary_or_recipe_user_time_joined)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1197,17 +1197,11 @@ public static AuthRecipeUserInfo[] listPrimaryUsersByEmailHelper(Start start, Co
String email)
throws StorageQueryException, SQLException {
List<String> userIds = new ArrayList<>();
List<String> emailPasswordUserId = EmailPasswordQueries.getPrimaryUserIdsUsingEmail(start, con, appIdentifier,
email);
if (emailPasswordUserId != null) {
userIds.addAll(emailPasswordUserId);
}
userIds.addAll(EmailPasswordQueries.getPrimaryUserIdsUsingEmail(start, con, appIdentifier,
email));

String passwordlessUserId = PasswordlessQueries.getPrimaryUserIdUsingEmail(start, con, appIdentifier,
email);
if (passwordlessUserId != null) {
userIds.add(passwordlessUserId);
}
userIds.addAll(PasswordlessQueries.getPrimaryUserIdsUsingEmail(start, con, appIdentifier,
email));

userIds.addAll(ThirdPartyQueries.getPrimaryUserIdUsingEmail(start, con, appIdentifier, email));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ public static List<String> lockPhoneAndTenant_Transaction(Start start, Connectio
}

public static String getPrimaryUserIdUsingEmail(Start start, Connection con, TenantIdentifier tenantIdentifier,
String email)
String email)
throws StorageQueryException, SQLException {
String QUERY = "SELECT DISTINCT all_users.primary_or_recipe_user_id AS user_id "
+ "FROM " + getConfig(start).getPasswordlessUserToTenantTable() + " AS pless" +
Expand All @@ -819,8 +819,8 @@ public static String getPrimaryUserIdUsingEmail(Start start, Connection con, Ten
});
}

public static String getPrimaryUserIdUsingEmail(Start start, Connection con, AppIdentifier appIdentifier,
String email)
public static List<String> getPrimaryUserIdsUsingEmail(Start start, Connection con, AppIdentifier appIdentifier,
String email)
throws StorageQueryException, SQLException {
String QUERY = "SELECT DISTINCT all_users.primary_or_recipe_user_id AS user_id "
+ "FROM " + getConfig(start).getPasswordlessUsersTable() + " AS pless" +
Expand All @@ -832,10 +832,11 @@ public static String getPrimaryUserIdUsingEmail(Start start, Connection con, App
pst.setString(1, appIdentifier.getAppId());
pst.setString(2, email);
}, result -> {
if (result.next()) {
return result.getString("user_id");
List<String> userIds = new ArrayList<>();
while (result.next()) {
userIds.add(result.getString("user_id"));
}
return null;
return userIds;
});
}

Expand Down Expand Up @@ -886,27 +887,6 @@ public static boolean addUserIdToTenant_Transaction(Start start, Connection sqlC
UserInfoPartial userInfo = PasswordlessQueries.getUserById(start, sqlCon,
tenantIdentifier.toAppIdentifier(), userId);

if (userInfo.email != null) {
AuthRecipeUserInfo[] primaryUsers = GeneralQueries.listPrimaryUsersByEmail_Transaction(start, sqlCon,
tenantIdentifier.toAppIdentifier(), userInfo.email);
for (AuthRecipeUserInfo primaryUser : primaryUsers) {
if (primaryUser.tenantIds.contains(tenantIdentifier.getTenantId()) && !primaryUser.getSupertokensUserId().equals(userInfo.id)) {
throw new DuplicateEmailException();
}
}
}

if (userInfo.phoneNumber != null) {
AuthRecipeUserInfo[] primaryUsers = GeneralQueries.listPrimaryUsersByPhoneNumber_Transaction(start, sqlCon,
tenantIdentifier.toAppIdentifier(), userInfo.phoneNumber);

for (AuthRecipeUserInfo primaryUser : primaryUsers) {
if (primaryUser.tenantIds.contains(tenantIdentifier.getTenantId()) && !primaryUser.getSupertokensUserId().equals(userInfo.id)) {
throw new DuplicatePhoneNumberException();
}
}
}

{ // all_auth_recipe_users
String QUERY = "INSERT INTO " + getConfig(start).getUsersTable()
+ "(app_id, tenant_id, user_id, primary_or_recipe_user_id, recipe_id, time_joined, primary_or_recipe_user_time_joined)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,23 +400,6 @@ public static boolean addUserIdToTenant_Transaction(Start start, Connection sqlC
UserInfoPartial userInfo = ThirdPartyQueries.getUserInfoUsingUserId(start, sqlCon,
tenantIdentifier.toAppIdentifier(), userId);

AuthRecipeUserInfo[] primaryUsers = GeneralQueries.listPrimaryUsersByEmail_Transaction(start, sqlCon,
tenantIdentifier.toAppIdentifier(), userInfo.email);

for (AuthRecipeUserInfo primaryUser : primaryUsers) {
if (primaryUser.tenantIds.contains(tenantIdentifier.getTenantId()) && !primaryUser.getSupertokensUserId().equals(userInfo.id)) {
throw new DuplicateEmailException();
}
}

primaryUsers = GeneralQueries.listPrimaryUsersByThirdPartyInfo_transaction(start, sqlCon, tenantIdentifier.toAppIdentifier(), userInfo.thirdParty.id, userInfo.thirdParty.userId);

for (AuthRecipeUserInfo primaryUser : primaryUsers) {
if (primaryUser.tenantIds.contains(tenantIdentifier.getTenantId()) && !primaryUser.getSupertokensUserId().equals(userInfo.id)) {
throw new DuplicateEmailException();
}
}

{ // all_auth_recipe_users
String QUERY = "INSERT INTO " + getConfig(start).getUsersTable()
+ "(app_id, tenant_id, user_id, primary_or_recipe_user_id, recipe_id, time_joined, primary_or_recipe_user_time_joined)"
Expand Down

0 comments on commit 3a754bb

Please sign in to comment.