Skip to content

Commit

Permalink
fix: recipe user association
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Sep 11, 2023
1 parent 6986b33 commit e90c778
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
30 changes: 15 additions & 15 deletions src/main/java/io/supertokens/multitenancy/Multitenancy.java
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,10 @@ public static boolean addUserIdToTenant(Main main, TenantIdentifierWithStorage t
}

for (String email : emails) {
AuthRecipeUserInfo[] users = storage.listPrimaryUsersByEmail_Transaction(tenantIdentifierWithStorage.toAppIdentifier(), con, email);
for (AuthRecipeUserInfo user : users) {
if (user.tenantIds.contains(tenantId) && !user.getSupertokensUserId().equals(userId)) {
for (LoginMethod lm1 : user.loginMethods) {
AuthRecipeUserInfo[] usersWithSameEmail = storage.listPrimaryUsersByEmail_Transaction(tenantIdentifierWithStorage.toAppIdentifier(), con, email);
for (AuthRecipeUserInfo userWithSameEmail : usersWithSameEmail) {
if (userWithSameEmail.isPrimaryUser && userWithSameEmail.tenantIds.contains(tenantId) && !userWithSameEmail.getSupertokensUserId().equals(userId)) {
for (LoginMethod lm1 : userWithSameEmail.loginMethods) {
if (lm1.tenantIds.contains(tenantId)) {
for (LoginMethod lm2 : userToAssociate.loginMethods) {
if (lm1.recipeId.equals(lm2.recipeId) && email.equals(lm1.email) && lm1.email.equals(lm2.email)) {
Expand All @@ -436,16 +436,16 @@ public static boolean addUserIdToTenant(Main main, TenantIdentifierWithStorage t
}
}
}
throw new StorageTransactionLogicException(new AnotherPrimaryUserWithEmailAlreadyExistsException(user.getSupertokensUserId()));
throw new StorageTransactionLogicException(new AnotherPrimaryUserWithEmailAlreadyExistsException(userWithSameEmail.getSupertokensUserId()));
}
}
}

for (String phoneNumber : phoneNumbers) {
AuthRecipeUserInfo[] users = storage.listPrimaryUsersByPhoneNumber_Transaction(tenantIdentifierWithStorage.toAppIdentifier(), con, phoneNumber);
for (AuthRecipeUserInfo user : users) {
if (user.tenantIds.contains(tenantId) && !user.getSupertokensUserId().equals(userId)) {
for (LoginMethod lm1 : user.loginMethods) {
AuthRecipeUserInfo[] usersWithSamePhoneNumber = storage.listPrimaryUsersByPhoneNumber_Transaction(tenantIdentifierWithStorage.toAppIdentifier(), con, phoneNumber);
for (AuthRecipeUserInfo userWithSamePhoneNumber : usersWithSamePhoneNumber) {
if (userWithSamePhoneNumber.tenantIds.contains(tenantId) && !userWithSamePhoneNumber.getSupertokensUserId().equals(userId)) {
for (LoginMethod lm1 : userWithSamePhoneNumber.loginMethods) {
if (lm1.tenantIds.contains(tenantId)) {
for (LoginMethod lm2 : userToAssociate.loginMethods) {
if (lm1.recipeId.equals(lm2.recipeId) && phoneNumber.equals(lm1.phoneNumber) && lm1.phoneNumber.equals(lm2.phoneNumber)) {
Expand All @@ -454,16 +454,16 @@ public static boolean addUserIdToTenant(Main main, TenantIdentifierWithStorage t
}
}
}
throw new StorageTransactionLogicException(new AnotherPrimaryUserWithPhoneNumberAlreadyExistsException(user.getSupertokensUserId()));
throw new StorageTransactionLogicException(new AnotherPrimaryUserWithPhoneNumberAlreadyExistsException(userWithSamePhoneNumber.getSupertokensUserId()));
}
}
}

for (LoginMethod.ThirdParty tp : thirdParties) {
AuthRecipeUserInfo[] users = storage.listPrimaryUsersByThirdPartyInfo_Transaction(tenantIdentifierWithStorage.toAppIdentifier(), con, tp.id, tp.userId);
for (AuthRecipeUserInfo user : users) {
if (user.tenantIds.contains(tenantId) && !user.getSupertokensUserId().equals(userId)) {
for (LoginMethod lm1 : user.loginMethods) {
AuthRecipeUserInfo[] usersWithSameThirdPartyInfo = storage.listPrimaryUsersByThirdPartyInfo_Transaction(tenantIdentifierWithStorage.toAppIdentifier(), con, tp.id, tp.userId);
for (AuthRecipeUserInfo userWithSameThirdPartyInfo : usersWithSameThirdPartyInfo) {
if (userWithSameThirdPartyInfo.tenantIds.contains(tenantId) && !userWithSameThirdPartyInfo.getSupertokensUserId().equals(userId)) {
for (LoginMethod lm1 : userWithSameThirdPartyInfo.loginMethods) {
if (lm1.tenantIds.contains(tenantId)) {
for (LoginMethod lm2 : userToAssociate.loginMethods) {
if (lm1.recipeId.equals(lm2.recipeId) && tp.equals(lm1.thirdParty) && lm1.thirdParty.equals(lm2.thirdParty)) {
Expand All @@ -473,7 +473,7 @@ public static boolean addUserIdToTenant(Main main, TenantIdentifierWithStorage t
}
}

throw new StorageTransactionLogicException(new AnotherPrimaryUserWithThirdPartyInfoAlreadyExistsException(user.getSupertokensUserId()));
throw new StorageTransactionLogicException(new AnotherPrimaryUserWithThirdPartyInfoAlreadyExistsException(userWithSameThirdPartyInfo.getSupertokensUserId()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,19 @@ public void testVariousCases() throws Exception {
t4 = new TenantIdentifier(null, "a1", "t3");

TestCase[] testCases = new TestCase[]{
new TestCase(new TestCaseStep[]{
new CreateEmailPasswordUser(t1, "[email protected]"),
new CreatePlessUserWithEmail(t2, "[email protected]"),
new MakePrimaryUser(t1, 0),
new AssociateUserToTenant(t2, 0),
}),
new TestCase(new TestCaseStep[]{
new CreateEmailPasswordUser(t1, "[email protected]"),
new CreatePlessUserWithEmail(t2, "[email protected]"),
new AssociateUserToTenant(t2, 0),
new MakePrimaryUser(t1, 0),
}),

new TestCase(new TestCaseStep[]{
new CreateEmailPasswordUser(t1, "[email protected]"),
new CreateEmailPasswordUser(t2, "[email protected]"),
Expand Down

0 comments on commit e90c778

Please sign in to comment.