Skip to content

Commit

Permalink
fix: pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Sep 6, 2023
1 parent d9410ec commit 5134184
Show file tree
Hide file tree
Showing 6 changed files with 515 additions and 251 deletions.
44 changes: 40 additions & 4 deletions src/main/java/io/supertokens/multitenancy/Multitenancy.java
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,9 @@ public static boolean addUserIdToTenant(Main main, TenantIdentifierWithStorage t
String userId)
throws TenantOrAppNotFoundException, UnknownUserIdException, StorageQueryException,
FeatureNotEnabledException, DuplicateEmailException, DuplicatePhoneNumberException,
DuplicateThirdPartyUserException {
DuplicateThirdPartyUserException, AnotherPrimaryUserWithPhoneNumberAlreadyExistsException,
AnotherPrimaryUserWithEmailAlreadyExistsException,
AnotherPrimaryUserWithThirdPartyInfoAlreadyExistsException {
if (Arrays.stream(FeatureFlag.getInstance(main, new AppIdentifier(null, null)).getEnabledFeatures())
.noneMatch(ee_features -> ee_features == EE_FEATURES.MULTI_TENANCY)) {
throw new FeatureNotEnabledException(EE_FEATURES.MULTI_TENANCY);
Expand Down Expand Up @@ -418,7 +420,16 @@ public static boolean addUserIdToTenant(Main main, TenantIdentifierWithStorage t
AuthRecipeUserInfo[] users = storage.listPrimaryUsersByEmail_Transaction(tenantIdentifierWithStorage.toAppIdentifier(), con, email);
for (AuthRecipeUserInfo user : users) {
if (user.tenantIds.contains(tenantId) && !user.getSupertokensUserId().equals(userId)) {
throw new StorageTransactionLogicException(new DuplicateEmailException());
for (LoginMethod lm1 : user.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)) {
throw new StorageTransactionLogicException(new DuplicateEmailException());
}
}
}
}
throw new StorageTransactionLogicException(new AnotherPrimaryUserWithEmailAlreadyExistsException(user.getSupertokensUserId()));
}
}
}
Expand All @@ -427,7 +438,16 @@ public static boolean addUserIdToTenant(Main main, TenantIdentifierWithStorage t
AuthRecipeUserInfo[] users = storage.listPrimaryUsersByPhoneNumber_Transaction(tenantIdentifierWithStorage.toAppIdentifier(), con, phoneNumber);
for (AuthRecipeUserInfo user : users) {
if (user.tenantIds.contains(tenantId) && !user.getSupertokensUserId().equals(userId)) {
throw new StorageTransactionLogicException(new DuplicatePhoneNumberException());
for (LoginMethod lm1 : user.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)) {
throw new StorageTransactionLogicException(new DuplicatePhoneNumberException());
}
}
}
}
throw new StorageTransactionLogicException(new AnotherPrimaryUserWithPhoneNumberAlreadyExistsException(user.getSupertokensUserId()));
}
}
}
Expand All @@ -436,7 +456,17 @@ public static boolean addUserIdToTenant(Main main, TenantIdentifierWithStorage t
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)) {
throw new StorageTransactionLogicException(new DuplicateThirdPartyUserException());
for (LoginMethod lm1 : user.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)) {
throw new StorageTransactionLogicException(new DuplicateThirdPartyUserException());
}
}
}
}

throw new StorageTransactionLogicException(new AnotherPrimaryUserWithThirdPartyInfoAlreadyExistsException(user.getSupertokensUserId()));
}
}
}
Expand Down Expand Up @@ -465,6 +495,12 @@ public static boolean addUserIdToTenant(Main main, TenantIdentifierWithStorage t
throw (TenantOrAppNotFoundException) e.actualException;
} else if (e.actualException instanceof UnknownUserIdException) {
throw (UnknownUserIdException) e.actualException;
} else if (e.actualException instanceof AnotherPrimaryUserWithPhoneNumberAlreadyExistsException) {
throw (AnotherPrimaryUserWithPhoneNumberAlreadyExistsException) e.actualException;
} else if (e.actualException instanceof AnotherPrimaryUserWithEmailAlreadyExistsException) {
throw (AnotherPrimaryUserWithEmailAlreadyExistsException) e.actualException;
} else if (e.actualException instanceof AnotherPrimaryUserWithThirdPartyInfoAlreadyExistsException) {
throw (AnotherPrimaryUserWithThirdPartyInfoAlreadyExistsException) e.actualException;
}
throw new StorageQueryException(e.actualException);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2023, VRAI Labs and/or its affiliates. All rights reserved.
*
* This software is licensed under the Apache License, Version 2.0 (the
* "License") as published by the Apache Software Foundation.
*
* You may not use this file except in compliance with the License. You may
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package io.supertokens.multitenancy.exception;

public class AnotherPrimaryUserWithEmailAlreadyExistsException extends Exception {
public AnotherPrimaryUserWithEmailAlreadyExistsException(String primaryUserId) {
super("Another primary user with email already exists: " + primaryUserId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2023, VRAI Labs and/or its affiliates. All rights reserved.
*
* This software is licensed under the Apache License, Version 2.0 (the
* "License") as published by the Apache Software Foundation.
*
* You may not use this file except in compliance with the License. You may
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package io.supertokens.multitenancy.exception;

public class AnotherPrimaryUserWithPhoneNumberAlreadyExistsException extends Exception {
public AnotherPrimaryUserWithPhoneNumberAlreadyExistsException(String primaryUserId) {
super("Another primary user with phone number already exists: " + primaryUserId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2023, VRAI Labs and/or its affiliates. All rights reserved.
*
* This software is licensed under the Apache License, Version 2.0 (the
* "License") as published by the Apache Software Foundation.
*
* You may not use this file except in compliance with the License. You may
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package io.supertokens.multitenancy.exception;

public class AnotherPrimaryUserWithThirdPartyInfoAlreadyExistsException extends Exception {
public AnotherPrimaryUserWithThirdPartyInfoAlreadyExistsException(String primaryUserId) {
super("Another primary user with third party info already exists: " + primaryUserId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import io.supertokens.Main;
import io.supertokens.featureflag.exceptions.FeatureNotEnabledException;
import io.supertokens.multitenancy.Multitenancy;
import io.supertokens.multitenancy.exception.AnotherPrimaryUserWithEmailAlreadyExistsException;
import io.supertokens.multitenancy.exception.AnotherPrimaryUserWithPhoneNumberAlreadyExistsException;
import io.supertokens.multitenancy.exception.AnotherPrimaryUserWithThirdPartyInfoAlreadyExistsException;
import io.supertokens.pluginInterface.RECIPE_ID;
import io.supertokens.pluginInterface.emailpassword.exceptions.DuplicateEmailException;
import io.supertokens.pluginInterface.emailpassword.exceptions.UnknownUserIdException;
Expand Down Expand Up @@ -99,6 +102,13 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
JsonObject result = new JsonObject();
result.addProperty("status", "THIRD_PARTY_USER_ALREADY_EXISTS_ERROR");
super.sendJsonResponse(200, result, resp);

} catch (AnotherPrimaryUserWithEmailAlreadyExistsException | AnotherPrimaryUserWithPhoneNumberAlreadyExistsException |
AnotherPrimaryUserWithThirdPartyInfoAlreadyExistsException e) {
JsonObject result = new JsonObject();
result.addProperty("status", "ASSOCIATION_NOT_ALLOWED_ERROR");
result.addProperty("reason", e.getMessage());
super.sendJsonResponse(200, result, resp);
}
}
}
Loading

0 comments on commit 5134184

Please sign in to comment.