From f63dd11f53202cb69fc4ad3aa5a4a9074fcc35d3 Mon Sep 17 00:00:00 2001 From: Ankit Tiwari Date: Fri, 8 Mar 2024 20:05:48 +0530 Subject: [PATCH] feat: plugin interface changes for ProcessBulkImportUsers cron --- .../bulkimport/BulkImportStorage.java | 5 ++++ .../bulkimport/BulkImportUser.java | 27 ++++++++++++++----- .../sqlStorage/BulkImportSQLStorage.java | 8 +++++- .../sqlStorage/EmailPasswordSQLStorage.java | 8 ++++++ .../EmailVerificationSQLStorage.java | 1 + .../AppIdentifierWithStorage.java | 6 ++--- .../sqlStorage/PasswordlessSQLStorage.java | 8 ++++++ .../sqlStorage/ThirdPartySQLStorage.java | 12 +++++++++ .../sqlStorage/UserIdMappingSQLStorage.java | 9 ++++++- .../sqlStorage/UserRolesSQLStorage.java | 5 ++++ 10 files changed, 77 insertions(+), 12 deletions(-) diff --git a/src/main/java/io/supertokens/pluginInterface/bulkimport/BulkImportStorage.java b/src/main/java/io/supertokens/pluginInterface/bulkimport/BulkImportStorage.java index 59b31ed9..79c5961a 100644 --- a/src/main/java/io/supertokens/pluginInterface/bulkimport/BulkImportStorage.java +++ b/src/main/java/io/supertokens/pluginInterface/bulkimport/BulkImportStorage.java @@ -47,6 +47,11 @@ List getBulkImportUsers(AppIdentifier appIdentifier, @Nonnull In */ List deleteBulkImportUsers(AppIdentifier appIdentifier, @Nonnull String[] bulkImportUserIds) throws StorageQueryException; + /** + * Returns the users from the bulk_import_users table for processing + */ + List getBulkImportUsersForProcessing(AppIdentifier appIdentifier, @Nonnull Integer limit) throws StorageQueryException; + public enum BULK_IMPORT_USER_STATUS { NEW, PROCESSING, FAILED } diff --git a/src/main/java/io/supertokens/pluginInterface/bulkimport/BulkImportUser.java b/src/main/java/io/supertokens/pluginInterface/bulkimport/BulkImportUser.java index d0d99a1b..d3f192e0 100644 --- a/src/main/java/io/supertokens/pluginInterface/bulkimport/BulkImportUser.java +++ b/src/main/java/io/supertokens/pluginInterface/bulkimport/BulkImportUser.java @@ -27,16 +27,17 @@ public class BulkImportUser { public String id; public String externalUserId; public JsonObject userMetadata; - public List userRoles; + public List userRoles; public List totpDevices; public List loginMethods; // Following fields come from the DB Record. public BULK_IMPORT_USER_STATUS status; + public String errorMessage; public Long createdAt; public Long updatedAt; - public BulkImportUser(String id, String externalUserId, JsonObject userMetadata, List userRoles, + public BulkImportUser(String id, String externalUserId, JsonObject userMetadata, List userRoles, List totpDevices, List loginMethods) { this.id = id; this.externalUserId = externalUserId; @@ -46,7 +47,7 @@ public BulkImportUser(String id, String externalUserId, JsonObject userMetadata, this.loginMethods = loginMethods; } - public static BulkImportUser fromTesting_fromJson(JsonObject jsonObject) { + public static BulkImportUser forTesting_fromJson(JsonObject jsonObject) { return new Gson().fromJson(jsonObject, BulkImportUser.class); } @@ -60,10 +61,11 @@ public String toRawDataForDbStorage() { return jsonObject.toString(); } - public static BulkImportUser fromRawDataFromDbStorage(String id, String rawData, BULK_IMPORT_USER_STATUS status, long createdAt, long updatedAt) { + public static BulkImportUser fromRawDataFromDbStorage(String id, String rawData, BULK_IMPORT_USER_STATUS status, String errorMessage, long createdAt, long updatedAt) { BulkImportUser user = new Gson().fromJson(rawData, BulkImportUser.class); user.id = id; user.status = status; + user.errorMessage = errorMessage; user.createdAt = createdAt; user.updatedAt = updatedAt; return user; @@ -73,6 +75,16 @@ public JsonObject toJsonObject() { return new Gson().fromJson(new Gson().toJson(this), JsonObject.class); } + public static class UserRole { + public String role; + public List tenantIds; + + public UserRole(String role, List tenantIds) { + this.role = role; + this.tenantIds = tenantIds; + } + } + public static class TotpDevice { public String secretKey; public int period; @@ -88,7 +100,7 @@ public TotpDevice(String secretKey, int period, int skew, String deviceName) { } public static class LoginMethod { - public String tenantId; + public List tenantIds; public boolean isVerified; public boolean isPrimary; public long timeJoinedInMSSinceEpoch; @@ -99,11 +111,12 @@ public static class LoginMethod { public String thirdPartyId; public String thirdPartyUserId; public String phoneNumber; + public String superTokensOrExternalUserId; - public LoginMethod(String tenantId, String recipeId, boolean isVerified, boolean isPrimary, + public LoginMethod(List tenantIds, String recipeId, boolean isVerified, boolean isPrimary, long timeJoinedInMSSinceEpoch, String email, String passwordHash, String hashingAlgorithm, String thirdPartyId, String thirdPartyUserId, String phoneNumber) { - this.tenantId = tenantId; + this.tenantIds = tenantIds; this.recipeId = recipeId; this.isVerified = isVerified; this.isPrimary = isPrimary; diff --git a/src/main/java/io/supertokens/pluginInterface/bulkimport/sqlStorage/BulkImportSQLStorage.java b/src/main/java/io/supertokens/pluginInterface/bulkimport/sqlStorage/BulkImportSQLStorage.java index be446472..2a8bf006 100644 --- a/src/main/java/io/supertokens/pluginInterface/bulkimport/sqlStorage/BulkImportSQLStorage.java +++ b/src/main/java/io/supertokens/pluginInterface/bulkimport/sqlStorage/BulkImportSQLStorage.java @@ -17,6 +17,7 @@ package io.supertokens.pluginInterface.bulkimport.sqlStorage; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import io.supertokens.pluginInterface.bulkimport.BulkImportStorage; import io.supertokens.pluginInterface.exceptions.StorageQueryException; @@ -30,5 +31,10 @@ public interface BulkImportSQLStorage extends BulkImportStorage, SQLStorage { * Update the status of the users in the bulk_import_users table */ void updateBulkImportUserStatus_Transaction(AppIdentifier appIdentifier, - TransactionConnection con, @Nonnull String[] bulkImportUserIds, @Nonnull BULK_IMPORT_USER_STATUS status) throws StorageQueryException; + TransactionConnection con, @Nonnull String[] bulkImportUserIds, @Nonnull BULK_IMPORT_USER_STATUS status, @Nullable String errorMessage) throws StorageQueryException; + + /** + * Delete users by id from the bulk_import_users table + */ + void deleteBulkImportUser_Transaction(AppIdentifier appIdentifier, TransactionConnection con, @Nonnull String bulkImportUserId) throws StorageQueryException; } diff --git a/src/main/java/io/supertokens/pluginInterface/emailpassword/sqlStorage/EmailPasswordSQLStorage.java b/src/main/java/io/supertokens/pluginInterface/emailpassword/sqlStorage/EmailPasswordSQLStorage.java index af47334f..57788cdf 100644 --- a/src/main/java/io/supertokens/pluginInterface/emailpassword/sqlStorage/EmailPasswordSQLStorage.java +++ b/src/main/java/io/supertokens/pluginInterface/emailpassword/sqlStorage/EmailPasswordSQLStorage.java @@ -16,11 +16,15 @@ package io.supertokens.pluginInterface.emailpassword.sqlStorage; +import io.supertokens.pluginInterface.authRecipe.AuthRecipeUserInfo; import io.supertokens.pluginInterface.emailpassword.EmailPasswordStorage; import io.supertokens.pluginInterface.emailpassword.PasswordResetTokenInfo; import io.supertokens.pluginInterface.emailpassword.exceptions.DuplicateEmailException; +import io.supertokens.pluginInterface.emailpassword.exceptions.DuplicateUserIdException; import io.supertokens.pluginInterface.exceptions.StorageQueryException; import io.supertokens.pluginInterface.multitenancy.AppIdentifier; +import io.supertokens.pluginInterface.multitenancy.TenantIdentifier; +import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException; import io.supertokens.pluginInterface.sqlStorage.SQLStorage; import io.supertokens.pluginInterface.sqlStorage.TransactionConnection; @@ -49,4 +53,8 @@ void updateUsersEmail_Transaction(AppIdentifier appIdentifier, TransactionConnec void deleteEmailPasswordUser_Transaction(TransactionConnection con, AppIdentifier appIdentifier, String userId, boolean deleteUserIdMappingToo) throws StorageQueryException; + + AuthRecipeUserInfo bulkImport_signUp_Transaction(TransactionConnection con, TenantIdentifier tenantIdentifier, String id, String email, String passwordHash, long timeJoined) + throws StorageQueryException, DuplicateUserIdException, DuplicateEmailException, + TenantOrAppNotFoundException; } diff --git a/src/main/java/io/supertokens/pluginInterface/emailverification/sqlStorage/EmailVerificationSQLStorage.java b/src/main/java/io/supertokens/pluginInterface/emailverification/sqlStorage/EmailVerificationSQLStorage.java index 877fb902..9f5dfee3 100644 --- a/src/main/java/io/supertokens/pluginInterface/emailverification/sqlStorage/EmailVerificationSQLStorage.java +++ b/src/main/java/io/supertokens/pluginInterface/emailverification/sqlStorage/EmailVerificationSQLStorage.java @@ -45,4 +45,5 @@ void updateIsEmailVerified_Transaction(AppIdentifier appIdentifier, TransactionC void deleteEmailVerificationUserInfo_Transaction(TransactionConnection con, AppIdentifier appIdentifier, String userId) throws StorageQueryException; + void bulkImport_updateIsEmailVerifiedToExternalUserId_Transaction(TransactionConnection con, AppIdentifier appIdentifier, String supertokensUserId, String externalUserId) throws StorageQueryException; } diff --git a/src/main/java/io/supertokens/pluginInterface/multitenancy/AppIdentifierWithStorage.java b/src/main/java/io/supertokens/pluginInterface/multitenancy/AppIdentifierWithStorage.java index ed5db6e3..b274e741 100644 --- a/src/main/java/io/supertokens/pluginInterface/multitenancy/AppIdentifierWithStorage.java +++ b/src/main/java/io/supertokens/pluginInterface/multitenancy/AppIdentifierWithStorage.java @@ -28,7 +28,7 @@ import io.supertokens.pluginInterface.session.SessionStorage; import io.supertokens.pluginInterface.thirdparty.sqlStorage.ThirdPartySQLStorage; import io.supertokens.pluginInterface.totp.sqlStorage.TOTPSQLStorage; -import io.supertokens.pluginInterface.useridmapping.UserIdMappingStorage; +import io.supertokens.pluginInterface.useridmapping.sqlStorage.UserIdMappingSQLStorage; import io.supertokens.pluginInterface.usermetadata.sqlStorage.UserMetadataSQLStorage; import io.supertokens.pluginInterface.userroles.sqlStorage.UserRolesSQLStorage; @@ -120,13 +120,13 @@ public UserMetadataSQLStorage getUserMetadataStorage() { return (UserMetadataSQLStorage) this.storage; } - public UserIdMappingStorage getUserIdMappingStorage() { + public UserIdMappingSQLStorage getUserIdMappingStorage() { if (this.storage.getType() != STORAGE_TYPE.SQL) { // we only support SQL for now throw new UnsupportedOperationException(""); } - return (UserIdMappingStorage) this.storage; + return (UserIdMappingSQLStorage) this.storage; } public UserRolesSQLStorage getUserRolesStorage() { diff --git a/src/main/java/io/supertokens/pluginInterface/passwordless/sqlStorage/PasswordlessSQLStorage.java b/src/main/java/io/supertokens/pluginInterface/passwordless/sqlStorage/PasswordlessSQLStorage.java index cfd6dbf7..029a1744 100644 --- a/src/main/java/io/supertokens/pluginInterface/passwordless/sqlStorage/PasswordlessSQLStorage.java +++ b/src/main/java/io/supertokens/pluginInterface/passwordless/sqlStorage/PasswordlessSQLStorage.java @@ -16,11 +16,14 @@ package io.supertokens.pluginInterface.passwordless.sqlStorage; +import io.supertokens.pluginInterface.authRecipe.AuthRecipeUserInfo; import io.supertokens.pluginInterface.emailpassword.exceptions.DuplicateEmailException; +import io.supertokens.pluginInterface.emailpassword.exceptions.DuplicateUserIdException; import io.supertokens.pluginInterface.emailpassword.exceptions.UnknownUserIdException; import io.supertokens.pluginInterface.exceptions.StorageQueryException; import io.supertokens.pluginInterface.multitenancy.AppIdentifier; import io.supertokens.pluginInterface.multitenancy.TenantIdentifier; +import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException; import io.supertokens.pluginInterface.passwordless.PasswordlessCode; import io.supertokens.pluginInterface.passwordless.PasswordlessDevice; import io.supertokens.pluginInterface.passwordless.PasswordlessStorage; @@ -85,4 +88,9 @@ void updateUserPhoneNumber_Transaction(AppIdentifier appIdentifier, TransactionC void deletePasswordlessUser_Transaction(TransactionConnection con, AppIdentifier appIdentifier, String userId, boolean deleteUserIdMappingToo) throws StorageQueryException; + + AuthRecipeUserInfo bulkImport_createUser_Transaction(TransactionConnection con, TenantIdentifier tenantIdentifier, String id, + @Nullable String email, @Nullable String phoneNumber, long timeJoined) + throws StorageQueryException, DuplicateEmailException, DuplicatePhoneNumberException, + DuplicateUserIdException, TenantOrAppNotFoundException; } diff --git a/src/main/java/io/supertokens/pluginInterface/thirdparty/sqlStorage/ThirdPartySQLStorage.java b/src/main/java/io/supertokens/pluginInterface/thirdparty/sqlStorage/ThirdPartySQLStorage.java index ea55a02f..86428a5e 100644 --- a/src/main/java/io/supertokens/pluginInterface/thirdparty/sqlStorage/ThirdPartySQLStorage.java +++ b/src/main/java/io/supertokens/pluginInterface/thirdparty/sqlStorage/ThirdPartySQLStorage.java @@ -16,11 +16,17 @@ package io.supertokens.pluginInterface.thirdparty.sqlStorage; +import io.supertokens.pluginInterface.authRecipe.AuthRecipeUserInfo; +import io.supertokens.pluginInterface.authRecipe.LoginMethod; import io.supertokens.pluginInterface.exceptions.StorageQueryException; import io.supertokens.pluginInterface.multitenancy.AppIdentifier; +import io.supertokens.pluginInterface.multitenancy.TenantIdentifier; +import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException; import io.supertokens.pluginInterface.sqlStorage.SQLStorage; import io.supertokens.pluginInterface.sqlStorage.TransactionConnection; import io.supertokens.pluginInterface.thirdparty.ThirdPartyStorage; +import io.supertokens.pluginInterface.thirdparty.exception.DuplicateThirdPartyUserException; +import io.supertokens.pluginInterface.thirdparty.exception.DuplicateUserIdException; public interface ThirdPartySQLStorage extends ThirdPartyStorage, SQLStorage { @@ -31,4 +37,10 @@ void updateUserEmail_Transaction(AppIdentifier appIdentifier, TransactionConnect void deleteThirdPartyUser_Transaction(TransactionConnection con, AppIdentifier appIdentifier, String userId, boolean deleteUserIdMappingToo) throws StorageQueryException; + + AuthRecipeUserInfo bulkImport_signUp_Transaction(TransactionConnection con, TenantIdentifier tenantIdentifier, + String id, String email, LoginMethod.ThirdParty thirdParty, + long timeJoined) + throws StorageQueryException, DuplicateUserIdException, DuplicateThirdPartyUserException, + TenantOrAppNotFoundException; } diff --git a/src/main/java/io/supertokens/pluginInterface/useridmapping/sqlStorage/UserIdMappingSQLStorage.java b/src/main/java/io/supertokens/pluginInterface/useridmapping/sqlStorage/UserIdMappingSQLStorage.java index 4d8ab903..e7d5c0d3 100644 --- a/src/main/java/io/supertokens/pluginInterface/useridmapping/sqlStorage/UserIdMappingSQLStorage.java +++ b/src/main/java/io/supertokens/pluginInterface/useridmapping/sqlStorage/UserIdMappingSQLStorage.java @@ -16,13 +16,16 @@ package io.supertokens.pluginInterface.useridmapping.sqlStorage; +import javax.annotation.Nullable; + import io.supertokens.pluginInterface.exceptions.StorageQueryException; import io.supertokens.pluginInterface.multitenancy.AppIdentifier; -import io.supertokens.pluginInterface.multitenancy.AppIdentifierWithStorage; import io.supertokens.pluginInterface.sqlStorage.SQLStorage; import io.supertokens.pluginInterface.sqlStorage.TransactionConnection; import io.supertokens.pluginInterface.useridmapping.UserIdMapping; import io.supertokens.pluginInterface.useridmapping.UserIdMappingStorage; +import io.supertokens.pluginInterface.useridmapping.exception.UnknownSuperTokensUserIdException; +import io.supertokens.pluginInterface.useridmapping.exception.UserIdMappingAlreadyExistsException; public interface UserIdMappingSQLStorage extends UserIdMappingStorage, SQLStorage { UserIdMapping getUserIdMapping_Transaction(TransactionConnection con, AppIdentifier appIdentifier, String userId, boolean isSuperTokensUserId) @@ -30,4 +33,8 @@ UserIdMapping getUserIdMapping_Transaction(TransactionConnection con, AppIdentif UserIdMapping[] getUserIdMapping_Transaction(TransactionConnection con, AppIdentifier appIdentifier, String userId) throws StorageQueryException; + + void bulkImport_createUserIdMapping_Transaction(TransactionConnection con, AppIdentifier appIdentifier, String superTokensUserId, String externalUserId, + @Nullable String externalUserIdInfo) + throws StorageQueryException, UnknownSuperTokensUserIdException, UserIdMappingAlreadyExistsException; } diff --git a/src/main/java/io/supertokens/pluginInterface/userroles/sqlStorage/UserRolesSQLStorage.java b/src/main/java/io/supertokens/pluginInterface/userroles/sqlStorage/UserRolesSQLStorage.java index c395ff31..ff88b6f8 100644 --- a/src/main/java/io/supertokens/pluginInterface/userroles/sqlStorage/UserRolesSQLStorage.java +++ b/src/main/java/io/supertokens/pluginInterface/userroles/sqlStorage/UserRolesSQLStorage.java @@ -23,6 +23,7 @@ import io.supertokens.pluginInterface.sqlStorage.SQLStorage; import io.supertokens.pluginInterface.sqlStorage.TransactionConnection; import io.supertokens.pluginInterface.userroles.UserRolesStorage; +import io.supertokens.pluginInterface.userroles.exception.DuplicateUserRoleMappingException; import io.supertokens.pluginInterface.userroles.exception.UnknownRoleException; public interface UserRolesSQLStorage extends UserRolesStorage, SQLStorage { @@ -56,4 +57,8 @@ boolean doesRoleExist_Transaction(AppIdentifier appIdentifier, TransactionConnec void deleteAllRolesForUser_Transaction(TransactionConnection con, AppIdentifier appIdentifier, String userId) throws StorageQueryException; + + void bulkImport_addRoleToUser_Transaction(TransactionConnection con, TenantIdentifier tenantIdentifier, String userId, String role) + throws StorageQueryException, UnknownRoleException, DuplicateUserRoleMappingException, + TenantOrAppNotFoundException; }