|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, VRAI Labs and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * This software is licensed under the Apache License, Version 2.0 (the |
| 5 | + * "License") as published by the Apache Software Foundation. |
| 6 | + * |
| 7 | + * You may not use this file except in compliance with the License. You may |
| 8 | + * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.supertokens.pluginInterface.bulkimport; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import com.google.gson.Gson; |
| 22 | +import com.google.gson.JsonObject; |
| 23 | + |
| 24 | +import io.supertokens.pluginInterface.bulkimport.BulkImportStorage.BULK_IMPORT_USER_STATUS; |
| 25 | + |
| 26 | +public class BulkImportUser { |
| 27 | + public String id; |
| 28 | + public String externalUserId; |
| 29 | + public JsonObject userMetadata; |
| 30 | + public List<UserRole> userRoles; |
| 31 | + public List<TotpDevice> totpDevices; |
| 32 | + public List<LoginMethod> loginMethods; |
| 33 | + |
| 34 | + // Following fields come from the DB Record. |
| 35 | + public BULK_IMPORT_USER_STATUS status; |
| 36 | + public String primaryUserId; |
| 37 | + public String errorMessage; |
| 38 | + public Long createdAt; |
| 39 | + public Long updatedAt; |
| 40 | + |
| 41 | + private static final Gson gson = new Gson(); |
| 42 | + |
| 43 | + public BulkImportUser(String id, String externalUserId, JsonObject userMetadata, List<UserRole> userRoles, |
| 44 | + List<TotpDevice> totpDevices, List<LoginMethod> loginMethods) { |
| 45 | + this.id = id; |
| 46 | + this.externalUserId = externalUserId; |
| 47 | + this.userMetadata = userMetadata; |
| 48 | + this.userRoles = userRoles; |
| 49 | + this.totpDevices = totpDevices; |
| 50 | + this.loginMethods = loginMethods; |
| 51 | + } |
| 52 | + |
| 53 | + public static BulkImportUser forTesting_fromJson(JsonObject jsonObject) { |
| 54 | + return gson.fromJson(jsonObject, BulkImportUser.class); |
| 55 | + } |
| 56 | + |
| 57 | + // The bulk_import_users table stores users to be imported via a Cron Job. |
| 58 | + // It has a `raw_data` column containing user data in JSON format. |
| 59 | + |
| 60 | + // The BulkImportUser class represents this `raw_data`, including additional fields like `status`, `createdAt`, and `updatedAt`. |
| 61 | + // First, we validate all fields of `raw_data` using the BulkImportUser class, then store this data in the bulk_import_users table. |
| 62 | + // This function retrieves the `raw_data` after removing the additional fields. |
| 63 | + public String toRawDataForDbStorage() { |
| 64 | + JsonObject jsonObject = gson.fromJson(new Gson().toJson(this), JsonObject.class); |
| 65 | + jsonObject.remove("status"); |
| 66 | + jsonObject.remove("createdAt"); |
| 67 | + jsonObject.remove("updatedAt"); |
| 68 | + return jsonObject.toString(); |
| 69 | + } |
| 70 | + |
| 71 | + // The bulk_import_users table contains a `raw_data` column with user data in JSON format, along with other columns such as `id`, `status`, `primary_user_id`, and `error_msg` etc. |
| 72 | + |
| 73 | + // When creating an instance of the BulkImportUser class, the extra fields must be passed separately as they are not part of the `raw_data`. |
| 74 | + // This function creates a BulkImportUser instance from a stored bulk_import_user entry. |
| 75 | + public static BulkImportUser fromRawDataFromDbStorage(String id, String rawData, BULK_IMPORT_USER_STATUS status, String primaryUserId, String errorMessage, long createdAt, long updatedAt) { |
| 76 | + BulkImportUser user = gson.fromJson(rawData, BulkImportUser.class); |
| 77 | + user.id = id; |
| 78 | + user.status = status; |
| 79 | + user.primaryUserId = primaryUserId; |
| 80 | + user.errorMessage = errorMessage; |
| 81 | + user.createdAt = createdAt; |
| 82 | + user.updatedAt = updatedAt; |
| 83 | + return user; |
| 84 | + } |
| 85 | + |
| 86 | + public JsonObject toJsonObject() { |
| 87 | + return gson.fromJson(gson.toJson(this), JsonObject.class); |
| 88 | + } |
| 89 | + |
| 90 | + public static class UserRole { |
| 91 | + public String role; |
| 92 | + public List<String> tenantIds; |
| 93 | + |
| 94 | + public UserRole(String role, List<String> tenantIds) { |
| 95 | + this.role = role; |
| 96 | + this.tenantIds = tenantIds; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + public static class TotpDevice { |
| 101 | + public String secretKey; |
| 102 | + public int period; |
| 103 | + public int skew; |
| 104 | + public String deviceName; |
| 105 | + |
| 106 | + public TotpDevice(String secretKey, int period, int skew, String deviceName) { |
| 107 | + this.secretKey = secretKey; |
| 108 | + this.period = period; |
| 109 | + this.skew = skew; |
| 110 | + this.deviceName = deviceName; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + public static class LoginMethod { |
| 115 | + public List<String> tenantIds; |
| 116 | + public boolean isVerified; |
| 117 | + public boolean isPrimary; |
| 118 | + public long timeJoinedInMSSinceEpoch; |
| 119 | + public String recipeId; |
| 120 | + public String email; |
| 121 | + public String passwordHash; |
| 122 | + public String hashingAlgorithm; |
| 123 | + public String plainTextPassword; |
| 124 | + public String thirdPartyId; |
| 125 | + public String thirdPartyUserId; |
| 126 | + public String phoneNumber; |
| 127 | + public String superTokensUserId; |
| 128 | + public String externalUserId; |
| 129 | + |
| 130 | + public String getSuperTokenOrExternalUserId() { |
| 131 | + return this.externalUserId != null ? this.externalUserId : this.superTokensUserId; |
| 132 | + } |
| 133 | + |
| 134 | + public LoginMethod(List<String> tenantIds, String recipeId, boolean isVerified, boolean isPrimary, |
| 135 | + long timeJoinedInMSSinceEpoch, String email, String passwordHash, String hashingAlgorithm, String plainTextPassword, |
| 136 | + String thirdPartyId, String thirdPartyUserId, String phoneNumber) { |
| 137 | + this.tenantIds = tenantIds; |
| 138 | + this.recipeId = recipeId; |
| 139 | + this.isVerified = isVerified; |
| 140 | + this.isPrimary = isPrimary; |
| 141 | + this.timeJoinedInMSSinceEpoch = timeJoinedInMSSinceEpoch; |
| 142 | + this.email = email; |
| 143 | + this.passwordHash = passwordHash; |
| 144 | + this.hashingAlgorithm = hashingAlgorithm; |
| 145 | + this.plainTextPassword = plainTextPassword; |
| 146 | + this.thirdPartyId = thirdPartyId; |
| 147 | + this.thirdPartyUserId = thirdPartyUserId; |
| 148 | + this.phoneNumber = phoneNumber; |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments