-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add admin email to config DB for quick reference
- Loading branch information
Showing
10 changed files
with
202 additions
and
45 deletions.
There are no files selected for viewing
This file contains 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 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 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; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
app/server/appsmith-server/src/main/java/com/appsmith/server/helpers/HashUtils.java
This file contains 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] : ""); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
...in/java/com/appsmith/server/migrations/db/ce/Migration063AddInstanceAdminDetailsToDB.java
This file contains 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,70 @@ | ||
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 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 = "063", id = "add_instance_admin_details_to_config_collection") | ||
public class Migration063AddInstanceAdminDetailsToDB { | ||
|
||
MongoTemplate mongoTemplate; | ||
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"); | ||
// Add instance admin details to the DB | ||
Query query = new Query() | ||
.addCriteria(where(FieldName.NAME).is(FieldName.INSTANCE_ADMIN_ROLE)) | ||
.addCriteria(notDeleted()); | ||
PermissionGroup instanceAdminPG = mongoTemplate.findOne(query, PermissionGroup.class); | ||
if (instanceAdminPG == null) { | ||
log.error("Instance admin permission group not found in the DB. Skipping migration 63"); | ||
return; | ||
} | ||
String adminEmail; | ||
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(); | ||
} else { | ||
adminEmail = commonConfig.getAdminEmails().stream() | ||
.filter(email -> email != null && email.contains("@")) | ||
.findFirst() | ||
.orElse(null); | ||
} | ||
Config config = new Config(); | ||
config.setName(INSTANCE_ADMIN_CONFIG); | ||
config.setConfig(InstanceAdminMetaDTO.toJsonObject(adminEmail)); | ||
mongoTemplate.save(config); | ||
} | ||
} |
This file contains 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 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 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.