-
Notifications
You must be signed in to change notification settings - Fork 4.6k
chore: Add admin email to config DB #36596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a658ac5
chore: Add admin email to config DB for quick reference
abhvsn b11479f
chore: Add in memory caching for analytics event
abhvsn 935adfa
Merge branch 'release' of github.com:appsmithorg/appsmith into chore/…
abhvsn 8f091de
chore: Refactor
abhvsn c823212
Merge branch 'release' of github.com:appsmithorg/appsmith into chore/…
abhvsn 38a1b07
chore: Fix issues when the instance admin is not set
abhvsn 2d5ecba
chore: Update migrations
abhvsn 5233067
chore: Address the review comments
abhvsn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
app/server/appsmith-server/src/main/java/com/appsmith/server/dtos/InstanceAdminMetaDTO.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package com.appsmith.server.dtos; | ||
|
|
||
| import com.appsmith.server.helpers.HashUtils; | ||
| import lombok.Data; | ||
| import net.minidev.json.JSONObject; | ||
|
|
||
| @Data | ||
| public class InstanceAdminMetaDTO { | ||
| String email; | ||
| String emailDomainHash; | ||
|
|
||
| public static JSONObject toJsonObject(String email) { | ||
| email = email == null ? "" : email; | ||
| JSONObject jsonObject = new JSONObject(); | ||
| jsonObject.put("email", email); | ||
| jsonObject.put("emailDomainHash", HashUtils.getEmailDomainHash(email)); | ||
| return jsonObject; | ||
| } | ||
|
|
||
| public static InstanceAdminMetaDTO fromJsonObject(JSONObject jsonObject) { | ||
| if (jsonObject == null) { | ||
| return new InstanceAdminMetaDTO(); | ||
| } | ||
| InstanceAdminMetaDTO instanceAdminMetaDTO = new InstanceAdminMetaDTO(); | ||
| instanceAdminMetaDTO.setEmail((String) jsonObject.get("email")); | ||
| instanceAdminMetaDTO.setEmailDomainHash((String) jsonObject.get("emailDomainHash")); | ||
| return instanceAdminMetaDTO; | ||
| } | ||
|
abhvsn marked this conversation as resolved.
|
||
| } | ||
19 changes: 19 additions & 0 deletions
19
app/server/appsmith-server/src/main/java/com/appsmith/server/helpers/HashUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package com.appsmith.server.helpers; | ||
|
|
||
| import org.apache.commons.codec.digest.DigestUtils; | ||
| import org.apache.commons.lang3.StringUtils; | ||
|
|
||
| public class HashUtils { | ||
|
|
||
| public static String hash(String value) { | ||
| return StringUtils.isEmpty(value) ? "" : DigestUtils.sha256Hex(value); | ||
| } | ||
|
|
||
| public static String getEmailDomainHash(String email) { | ||
| if (email == null) { | ||
| return ""; | ||
| } | ||
|
|
||
| return hash(email.contains("@") ? email.split("@", 2)[1] : ""); | ||
| } | ||
|
abhvsn marked this conversation as resolved.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moving these methods from AnalyticsService to helper class to be reusable. |
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...in/java/com/appsmith/server/migrations/db/ce/Migration064AddInstanceAdminDetailsToDB.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package com.appsmith.server.migrations.db.ce; | ||
|
|
||
| import com.appsmith.server.configurations.CommonConfig; | ||
| import com.appsmith.server.constants.FieldName; | ||
| import com.appsmith.server.domains.Config; | ||
| import com.appsmith.server.domains.PermissionGroup; | ||
| import com.appsmith.server.domains.User; | ||
| import com.appsmith.server.dtos.InstanceAdminMetaDTO; | ||
| import com.appsmith.server.helpers.CollectionUtils; | ||
| import io.mongock.api.annotations.ChangeUnit; | ||
| import io.mongock.api.annotations.Execution; | ||
| import io.mongock.api.annotations.RollbackExecution; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.data.mongodb.core.MongoTemplate; | ||
| import org.springframework.data.mongodb.core.query.Query; | ||
| import org.springframework.util.StringUtils; | ||
| import reactor.core.publisher.Flux; | ||
|
|
||
| import static com.appsmith.server.constants.ce.FieldNameCE.INSTANCE_ADMIN_CONFIG; | ||
| import static com.appsmith.server.helpers.ce.bridge.BridgeQuery.where; | ||
| import static com.appsmith.server.repositories.ce.BaseAppsmithRepositoryCEImpl.notDeleted; | ||
|
|
||
| @RequiredArgsConstructor | ||
| @Slf4j | ||
| @ChangeUnit(order = "064", id = "add_instance_admin_details_to_config_collection") | ||
| public class Migration064AddInstanceAdminDetailsToDB { | ||
|
|
||
| private final MongoTemplate mongoTemplate; | ||
| private final CommonConfig commonConfig; | ||
|
|
||
| @RollbackExecution | ||
| public void rollbackExecution() {} | ||
|
|
||
| @Execution | ||
| public void executeMigration() { | ||
| // Add instance admin details to the DB | ||
| // This migration is idempotent and can be run multiple times without any side effects | ||
| log.info("Adding instance admin details to the DB"); | ||
| // Check if instance admin details are already present in the DB to make the migration idempotent | ||
| if (verifyIfInstanceAdminDetailsArePresent(mongoTemplate)) { | ||
| return; | ||
| } | ||
|
|
||
| // Add instance admin details to the DB | ||
| Query instanceAdminRoleQuery = new Query() | ||
| .addCriteria(where(FieldName.NAME).is(FieldName.INSTANCE_ADMIN_ROLE)) | ||
| .addCriteria(notDeleted()); | ||
| PermissionGroup instanceAdminPG = mongoTemplate.findOne(instanceAdminRoleQuery, PermissionGroup.class); | ||
| if (instanceAdminPG == null) { | ||
| log.error("Instance admin permission group not found in the DB. Skipping migration 63"); | ||
|
abhvsn marked this conversation as resolved.
Outdated
|
||
| return; | ||
| } | ||
| String adminEmail = null; | ||
| if (commonConfig.isCloudHosting()) { | ||
| // As a fallback, use the default admin email for cloud hosting | ||
| adminEmail = "admin@appsmith.com"; | ||
|
abhvsn marked this conversation as resolved.
Outdated
|
||
| } else if (!CollectionUtils.isNullOrEmpty(instanceAdminPG.getAssignedToUserIds())) { | ||
| adminEmail = Flux.fromIterable(instanceAdminPG.getAssignedToUserIds()) | ||
| .map(userId -> mongoTemplate.findOne( | ||
| new Query() | ||
| .addCriteria(where(FieldName.ID).is(userId)) | ||
| .addCriteria(notDeleted()), | ||
| User.class)) | ||
| .map(User::getEmail) | ||
| .filter(email -> email != null && email.contains("@")) | ||
| .blockFirst(); | ||
|
abhvsn marked this conversation as resolved.
|
||
| } | ||
|
|
||
| if (!StringUtils.hasLength(adminEmail)) { | ||
| adminEmail = commonConfig.getAdminEmails().stream() | ||
| .filter(email -> email != null && email.contains("@")) | ||
| .findFirst() | ||
| .orElse(null); | ||
| } | ||
| Config config = new Config(); | ||
| config.setName(INSTANCE_ADMIN_CONFIG); | ||
| if (StringUtils.hasLength(adminEmail)) { | ||
| config.setConfig(InstanceAdminMetaDTO.toJsonObject(adminEmail)); | ||
| mongoTemplate.save(config); | ||
| } | ||
| } | ||
|
|
||
| public static boolean verifyIfInstanceAdminDetailsArePresent(MongoTemplate mongoTemplate) { | ||
| Query instanceAdminConfigQuery = new Query() | ||
| .addCriteria(where(FieldName.NAME).is(FieldName.INSTANCE_ADMIN_CONFIG)) | ||
| .addCriteria(notDeleted()); | ||
| Config instanceAdminConfig = mongoTemplate.findOne(instanceAdminConfigQuery, Config.class); | ||
| if (instanceAdminConfig != null) { | ||
| InstanceAdminMetaDTO instanceAdminMetaDTO = | ||
| InstanceAdminMetaDTO.fromJsonObject(instanceAdminConfig.getConfig()); | ||
| if (StringUtils.hasLength(instanceAdminMetaDTO.getEmail())) { | ||
| log.info("Instance admin details already present in the DB. Skipping migration 64"); | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
abhvsn marked this conversation as resolved.
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.