diff --git a/persistence-service-v1/persistence-service-processor-v1/src/main/java/com/iqser/red/service/persistence/management/v1/processor/service/UserService.java b/persistence-service-v1/persistence-service-processor-v1/src/main/java/com/iqser/red/service/persistence/management/v1/processor/service/UserService.java index ad484456b..2891cabf0 100644 --- a/persistence-service-v1/persistence-service-processor-v1/src/main/java/com/iqser/red/service/persistence/management/v1/processor/service/UserService.java +++ b/persistence-service-v1/persistence-service-processor-v1/src/main/java/com/iqser/red/service/persistence/management/v1/processor/service/UserService.java @@ -31,6 +31,7 @@ import org.jboss.resteasy.client.jaxrs.internal.ResteasyClientBuilderImpl; import org.keycloak.OAuth2Constants; import org.keycloak.admin.client.KeycloakBuilder; import org.keycloak.admin.client.resource.UserResource; +import org.keycloak.admin.client.resource.UsersResource; import org.keycloak.representations.idm.CredentialRepresentation; import org.keycloak.representations.idm.RoleRepresentation; import org.keycloak.representations.idm.UserRepresentation; @@ -93,7 +94,7 @@ public class UserService { public User createUser(CreateUserRequest user) { String username = StringUtils.isEmpty(user.getUsername()) ? user.getEmail() : user.getUsername(); - if (!realmService.realm(TenantContext.getTenantId()).users().search(username).isEmpty()) { + if (!this.getTenantUsersResource().search(username).isEmpty()) { throw new ConflictException("User with this username already exists"); } @@ -101,7 +102,7 @@ public class UserService { 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()) { + if (!StringUtils.isEmpty(user.getUsername()) && !this.getTenantUsersResource().searchByEmail(user.getEmail(), true).isEmpty()) { throw new ConflictException("User with this email already exists"); } @@ -114,7 +115,7 @@ public class UserService { userRepresentation.setFirstName(user.getFirstName()); userRepresentation.setLastName(user.getLastName()); - try (var response = realmService.realm(TenantContext.getTenantId()).users().create(userRepresentation)) { + try (var response = this.getTenantUsersResource().create(userRepresentation)) { if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { if (response.getStatusInfo().getStatusCode() == 409) { @@ -152,9 +153,13 @@ public class UserService { } + private UsersResource getTenantUsersResource() { + return realmService.realm(TenantContext.getTenantId()).users(); + } + private User getUserByUsername(String username) { - var userList = realmService.realm(TenantContext.getTenantId()).users().search(username); + var userList = this.getTenantUsersResource().search(username); if (userList.isEmpty()) { throw new NotFoundException("User with this username already exists"); } @@ -166,7 +171,7 @@ public class UserService { private void sendResetPasswordEmail(String userId) { try { - realmService.realm(TenantContext.getTenantId()).users().get(userId).executeActionsEmail(Collections.singletonList("UPDATE_PASSWORD"), 86400); + this.getTenantUsersResource().get(userId).executeActionsEmail(Collections.singletonList("UPDATE_PASSWORD"), 86400); } catch (Exception e) { throw new BadRequestException("Failed to send email", e); } @@ -257,7 +262,7 @@ public class UserService { throw new BadRequestException("No id provided."); } try { - return realmService.realm(TenantContext.getTenantId()).users().get(userId); + return this.getTenantUsersResource().get(userId); } catch (NotFoundException e) { throw new NotFoundException("User with id: " + userId + " does not exist", e); } @@ -291,7 +296,7 @@ public class UserService { private Set getRoles(String id) { - List realmMappings = realmService.realm(TenantContext.getTenantId()).users().get(id).roles().getAll().getRealmMappings(); + List realmMappings = this.getTenantUsersResource().get(id).roles().getAll().getRealmMappings(); if (realmMappings == null) { log.warn("User with id=" + id + " contains null role mappings."); return new TreeSet<>(); @@ -357,7 +362,7 @@ public class UserService { @CacheEvict(value = USERS_CACHE, allEntries = true, beforeInvocation = true) public void updateMyProfile(UpdateMyProfileRequest updateProfileRequest) { - var user = realmService.realm(TenantContext.getTenantId()).users().get(KeycloakSecurity.getUserId()); + var user = this.getUserResource(KeycloakSecurity.getUserId()); var userRepresentation = user.toRepresentation(); if (userRepresentation.getFederatedIdentities() != null && !userRepresentation.getFederatedIdentities().isEmpty() && !updateProfileRequest.getEmail() @@ -372,10 +377,7 @@ public class UserService { userRepresentation.setFirstName(updateProfileRequest.getFirstName()); userRepresentation.setLastName(updateProfileRequest.getLastName()); userRepresentation.setEmail(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()); - } + this.setUsername(userRepresentation, updateProfileRequest.getEmail()); try { user.update(userRepresentation); @@ -395,6 +397,12 @@ public class UserService { .build()); } + private void setUsername(UserRepresentation userRepresentation, String emailToSet) { + // 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(emailToSet); + } + } private void validatePassword(String username, String password) { @@ -492,7 +500,7 @@ public class UserService { @CacheEvict(value = USERS_CACHE, allEntries = true, beforeInvocation = true) public void updateProfile(String userId, UpdateProfileRequest updateProfileRequest) { - var user = realmService.realm(TenantContext.getTenantId()).users().get(userId); + var user = this.getUserResource(userId); var userRepresentation = user.toRepresentation(); if (userRepresentation.getFederatedIdentities() != null && !userRepresentation.getFederatedIdentities().isEmpty() && !updateProfileRequest.getEmail() @@ -507,9 +515,7 @@ 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()); - } + this.setUsername(userRepresentation, updateProfileRequest.getEmail()); user.update(userRepresentation); @@ -527,7 +533,7 @@ public class UserService { public User activateProfile(String userId, boolean isActive) { - var user = realmService.realm(TenantContext.getTenantId()).users().get(userId); + var user = this.getUserResource(userId); var userRepresentation = user.toRepresentation(); userRepresentation.setEnabled(isActive); @@ -546,7 +552,7 @@ public class UserService { .details(Map.of("Profile activated", isActive)) .build()); - return convert(realmService.realm(TenantContext.getTenantId()).users().get(userId).toRepresentation()); + return convert(this.getTenantUsersResource().get(userId).toRepresentation()); }