Skip to content
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

Add Email field to User Collection #1025

Merged
merged 14 commits into from
Jul 9, 2024
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 @@ -98,7 +98,7 @@ public Mono<Organization> createDefault(User user, boolean isSuperAdmin) {

private Mono<Boolean> joinOrganizationInEnterpriseMode(String userId) {
return getOrganizationInEnterpriseMode()
.flatMap(organization -> orgMemberService.addMember(organization.getGid(), userId, MemberRole.MEMBER))
.flatMap(organization -> orgMemberService.addMember(organization.getId(), userId, MemberRole.MEMBER))
.defaultIfEmpty(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class AuthUser {

private String uid;
private String username;
private String email;
private String avatar;
private Map<String, Object> rawUserInfo;
private Map<String, Object> extra;
Expand All @@ -41,6 +42,7 @@ public Connection toAuthConnection() {
.authId(getAuthContext().getAuthConfig().getId())
.source(getSource())
.name(getUsername())
.email(getEmail())
.rawId(getUid())
.avatar(getAvatar())
.orgIds(StringUtils.isBlank(getOrgId()) ? Set.of() : Set.of(getOrgId()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class Connection implements Serializable {

private final String name;

private final String email;

private final String avatar;

private Set<String> orgIds;
Expand All @@ -59,12 +61,13 @@ public class Connection implements Serializable {
private Set<String> tokens;

@JsonCreator
private Connection(String authId, String source, String rawId, String name, String avatar, Set<String> orgIds, @Nullable
private Connection(String authId, String source, String rawId, String name, String email, String avatar, Set<String> orgIds, @Nullable
ConnectionAuthToken authConnectionAuthToken, Map<String, Object> rawUserInfo, Set<String> tokens) {
this.authId = authId;
this.source = source;
this.rawId = rawId;
this.name = name;
this.email = email;
this.avatar = avatar;
this.orgIds = CollectionUtils.isEmpty(orgIds) ? new HashSet<>() : orgIds;
this.authConnectionAuthToken = authConnectionAuthToken;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class User extends HasIdAndAuditing implements BeforeMongodbWrite, AfterM

private String name;

private String email;

private String uiLanguage;

private String avatar;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ public interface UserRepository extends ReactiveMongoRepository<User, String> {
Flux<User> findByConnections_SourceAndConnections_RawIdIn(String source, Collection<String> rawIds);

Mono<User> findByName(String rawUuid);

//email1 and email2 should be equal
Mono<User> findByEmailOrConnections_Email(String email1, String email2);
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ public Mono<User> findByName(String rawUuid) {
return repository.findByName(rawUuid);
}

public Mono<User> findByEmailDeep(String email) {
return repository.findByEmailOrConnections_Email(email, email);
}

@Override
public Mono<Boolean> saveProfilePhoto(Part filePart, User user) {
String prevAvatar = ObjectUtils.defaultIfNull(user.getAvatar(), "");
Expand Down Expand Up @@ -151,13 +155,14 @@ public Mono<User> findByAuthUserSourceAndRawId(AuthUser authUser) {

@Override
public Mono<User> findByAuthUserRawId(AuthUser authUser) {
return findByName(authUser.getUsername());
return findByEmailDeep(authUser.getEmail());
}

@Override
public Mono<User> createNewUserByAuthUser(AuthUser authUser) {
User.UserBuilder userBuilder = User.builder()
.name(authUser.getUsername())
.email(authUser.getEmail())
.state(UserState.ACTIVATED)
.isEnabled(true)
.tpAvatarLink(authUser.getAvatar());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public Mono<AuthUser> auth(AuthRequestContext authRequestContext) {
return Mono.empty();
});
})
.thenReturn(AuthUser.builder().uid(context.getLoginId()).username(context.getLoginId()).build());
.thenReturn(AuthUser.builder().uid(context.getLoginId()).username(context.getLoginId()).email(context.getLoginId()).build());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public static AuthUser mapToAuthUser(Map<String, Object> map, HashMap<String, St
return AuthUser.builder()
.uid(uid)
.username(username)
.email(email)
.avatar(avatar)
.rawUserInfo(map)
.build();
Expand All @@ -111,6 +112,7 @@ public static AuthUser mergeAuthUser(AuthUser low, AuthUser high) {
return AuthUser.builder()
.uid(high.getUid() != null ? high.getUid() : low.getUid())
.username(high.getUsername() != null ? high.getUsername() : low.getUsername())
.email(high.getEmail() != null ? high.getEmail() : low.getEmail())
.avatar(high.getAvatar() != null ? high.getAvatar() : low.getAvatar())
.rawUserInfo(high.getRawUserInfo() != null ? high.getRawUserInfo() : low.getRawUserInfo())
.authToken(high.getAuthToken() != null ? high.getAuthToken() : low.getAuthToken())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,30 @@ public void addGidToDBObjects(MongockTemplate mongoTemplate) {
}
}

@ChangeSet(order = "023", id = "add-email", author = "")
public void addEmailField(MongockTemplate mongoTemplate) {
// Create a query to match all documents
Query query = new Query();

// Use a DocumentCallbackHandler to iterate through each document
mongoTemplate.executeQuery(query, "user", new DocumentCallbackHandler() {
@Override
public void processDocument(Document document) {
// Generate a random UUID and ensure it is unique within the collection
String username = document.getString("name");
// Create an update object to add the 'gid' field
Update update = new Update();
update.set("email", username);

// Create a query to match the current document by its _id
Query idQuery = new Query(Criteria.where("_id").is(document.getObjectId("_id")));

// Update the document with the new 'gid' field
mongoTemplate.updateFirst(idQuery, update, "user");
}
});
}

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