Skip to content

Only one super admin should exists #1710

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

Merged
merged 2 commits into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ public Mono<User> createNewUserByAuthUser(AuthUser authUser, boolean isSuperAdmi
newUser.setConnections(connections);
newUser.setActiveAuthId(connection.getAuthId());
newUser.setIsNewUser(true);
if(isSuperAdmin) {
return repository.findBySuperAdminIsTrue()
.flatMap(user -> update(user.getId(), newUser))
.switchIfEmpty(create(newUser));
}
return create(newUser);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,27 @@ public void renameApplicationRecordCollection(MongockTemplate mongoTemplate, Mon

}

@ChangeSet(order = "031", id = "delete-old-super-admin", author = "Thomas")
Copy link
Preview

Copilot AI May 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a unique index on the superAdmin field at the database level to enforce uniqueness and prevent race conditions from creating multiple super admin users.

Copilot uses AI. Check for mistakes.

public void deleteOldSuperAdmin(MongockTemplate mongoTemplate, MongoDatabase mongoDatabase) {
List<User> users = mongoTemplate.find(
Query.query(Criteria.where("superAdmin").is(true))
.with(Sort.by(Sort.Direction.DESC, "createdAt")),
User.class
);

// Ensure there's more than one superAdmin user
if (users.size() > 1) {
// Keep the most recent one (first in the sorted list), delete the rest
List<ObjectId> userIdsToDelete = users.subList(1, users.size())
.stream()
.map(User::getId)
.collect(Collectors.toList());

Query deleteQuery = Query.query(Criteria.where("_id").in(userIdsToDelete));
mongoTemplate.remove(deleteQuery, User.class);
}
}

private void addGidField(MongockTemplate mongoTemplate, String collectionName) {
// Create a query to match all documents
Query query = new Query();
Expand Down
Loading