RED-6548 - Username not saved / Email saved as username
- extend createUserRequest with username - update the username with the one provided - update also the username in case of updating profile with a new email address
This commit is contained in:
parent
1fb1066436
commit
6f39a6582f
@ -92,18 +92,23 @@ public class UserService {
|
||||
@CacheEvict(value = USERS_CACHE, allEntries = true, beforeInvocation = true)
|
||||
public User createUser(CreateUserRequest user) {
|
||||
|
||||
if (!realmService.realm(TenantContext.getTenantId()).users().search(user.getEmail()).isEmpty()) {
|
||||
String username = StringUtils.isEmpty(user.getUsername()) ? user.getEmail() : user.getUsername();
|
||||
if (!realmService.realm(TenantContext.getTenantId()).users().search(username).isEmpty()) {
|
||||
throw new ConflictException("User with this username already exists");
|
||||
}
|
||||
|
||||
if (!EmailValidator.getInstance().isValid(user.getEmail())) {
|
||||
throw new BadRequestException("Email address format is not valid");
|
||||
}
|
||||
// also search by email in case the username was provided at creation
|
||||
if (!StringUtils.isEmpty(user.getUsername()) && !realmService.realm(TenantContext.getTenantId()).users().searchByEmail(user.getEmail(), true).isEmpty()) {
|
||||
throw new ConflictException("User with this email already exists");
|
||||
}
|
||||
|
||||
validateRoles(user.getRoles());
|
||||
|
||||
UserRepresentation userRepresentation = new UserRepresentation();
|
||||
userRepresentation.setUsername(user.getEmail());
|
||||
userRepresentation.setUsername(username);
|
||||
userRepresentation.setEmail(user.getEmail());
|
||||
userRepresentation.setEnabled(true);
|
||||
userRepresentation.setFirstName(user.getFirstName());
|
||||
@ -121,7 +126,7 @@ public class UserService {
|
||||
throw new BadRequestException("Cannot create user ... ");
|
||||
}
|
||||
|
||||
var createdUser = getUserByUsername(user.getEmail());
|
||||
var createdUser = getUserByUsername(username);
|
||||
|
||||
try {
|
||||
sendResetPasswordEmail(createdUser.getUserId());
|
||||
@ -142,7 +147,7 @@ public class UserService {
|
||||
|
||||
customPermissionService.syncAllCustomPermissions();
|
||||
|
||||
return getUserByUsername(user.getEmail());
|
||||
return getUserByUsername(username);
|
||||
}
|
||||
}
|
||||
|
||||
@ -367,7 +372,10 @@ public class UserService {
|
||||
userRepresentation.setFirstName(updateProfileRequest.getFirstName());
|
||||
userRepresentation.setLastName(updateProfileRequest.getLastName());
|
||||
userRepresentation.setEmail(updateProfileRequest.getEmail());
|
||||
userRepresentation.setUsername(updateProfileRequest.getEmail());
|
||||
// update the username only if none was provided at creation and in this case the email and username are the same
|
||||
if (userRepresentation.getUsername().equals(userRepresentation.getEmail())) {
|
||||
userRepresentation.setUsername(updateProfileRequest.getEmail());
|
||||
}
|
||||
|
||||
try {
|
||||
user.update(userRepresentation);
|
||||
@ -499,6 +507,9 @@ public class UserService {
|
||||
userRepresentation.setFirstName(updateProfileRequest.getFirstName());
|
||||
userRepresentation.setLastName(updateProfileRequest.getLastName());
|
||||
userRepresentation.setEmail(updateProfileRequest.getEmail());
|
||||
if (userRepresentation.getUsername().equals(userRepresentation.getEmail())) {
|
||||
userRepresentation.setUsername(updateProfileRequest.getEmail());
|
||||
}
|
||||
|
||||
user.update(userRepresentation);
|
||||
|
||||
|
||||
@ -12,6 +12,9 @@ public class CreateUserRequest {
|
||||
@Schema(description = "Email of user.")
|
||||
private String email;
|
||||
|
||||
@Schema(description = "Username of user.")
|
||||
private String username;
|
||||
|
||||
@Schema(description = "First name of user.")
|
||||
private String firstName;
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user