RED-6362 - Cannot add KMS signature
- rework after review
This commit is contained in:
parent
6f39a6582f
commit
47df255e12
@ -31,6 +31,7 @@ import org.jboss.resteasy.client.jaxrs.internal.ResteasyClientBuilderImpl;
|
|||||||
import org.keycloak.OAuth2Constants;
|
import org.keycloak.OAuth2Constants;
|
||||||
import org.keycloak.admin.client.KeycloakBuilder;
|
import org.keycloak.admin.client.KeycloakBuilder;
|
||||||
import org.keycloak.admin.client.resource.UserResource;
|
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.CredentialRepresentation;
|
||||||
import org.keycloak.representations.idm.RoleRepresentation;
|
import org.keycloak.representations.idm.RoleRepresentation;
|
||||||
import org.keycloak.representations.idm.UserRepresentation;
|
import org.keycloak.representations.idm.UserRepresentation;
|
||||||
@ -93,7 +94,7 @@ public class UserService {
|
|||||||
public User createUser(CreateUserRequest user) {
|
public User createUser(CreateUserRequest user) {
|
||||||
|
|
||||||
String username = StringUtils.isEmpty(user.getUsername()) ? user.getEmail() : user.getUsername();
|
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");
|
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");
|
throw new BadRequestException("Email address format is not valid");
|
||||||
}
|
}
|
||||||
// also search by email in case the username was provided at creation
|
// 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");
|
throw new ConflictException("User with this email already exists");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,7 +115,7 @@ public class UserService {
|
|||||||
userRepresentation.setFirstName(user.getFirstName());
|
userRepresentation.setFirstName(user.getFirstName());
|
||||||
userRepresentation.setLastName(user.getLastName());
|
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().getFamily() != Response.Status.Family.SUCCESSFUL) {
|
||||||
if (response.getStatusInfo().getStatusCode() == 409) {
|
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) {
|
private User getUserByUsername(String username) {
|
||||||
|
|
||||||
var userList = realmService.realm(TenantContext.getTenantId()).users().search(username);
|
var userList = this.getTenantUsersResource().search(username);
|
||||||
if (userList.isEmpty()) {
|
if (userList.isEmpty()) {
|
||||||
throw new NotFoundException("User with this username already exists");
|
throw new NotFoundException("User with this username already exists");
|
||||||
}
|
}
|
||||||
@ -166,7 +171,7 @@ public class UserService {
|
|||||||
private void sendResetPasswordEmail(String userId) {
|
private void sendResetPasswordEmail(String userId) {
|
||||||
|
|
||||||
try {
|
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) {
|
} catch (Exception e) {
|
||||||
throw new BadRequestException("Failed to send email", e);
|
throw new BadRequestException("Failed to send email", e);
|
||||||
}
|
}
|
||||||
@ -257,7 +262,7 @@ public class UserService {
|
|||||||
throw new BadRequestException("No id provided.");
|
throw new BadRequestException("No id provided.");
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return realmService.realm(TenantContext.getTenantId()).users().get(userId);
|
return this.getTenantUsersResource().get(userId);
|
||||||
} catch (NotFoundException e) {
|
} catch (NotFoundException e) {
|
||||||
throw new NotFoundException("User with id: " + userId + " does not exist", e);
|
throw new NotFoundException("User with id: " + userId + " does not exist", e);
|
||||||
}
|
}
|
||||||
@ -291,7 +296,7 @@ public class UserService {
|
|||||||
|
|
||||||
private Set<String> getRoles(String id) {
|
private Set<String> getRoles(String id) {
|
||||||
|
|
||||||
List<RoleRepresentation> realmMappings = realmService.realm(TenantContext.getTenantId()).users().get(id).roles().getAll().getRealmMappings();
|
List<RoleRepresentation> realmMappings = this.getTenantUsersResource().get(id).roles().getAll().getRealmMappings();
|
||||||
if (realmMappings == null) {
|
if (realmMappings == null) {
|
||||||
log.warn("User with id=" + id + " contains null role mappings.");
|
log.warn("User with id=" + id + " contains null role mappings.");
|
||||||
return new TreeSet<>();
|
return new TreeSet<>();
|
||||||
@ -357,7 +362,7 @@ public class UserService {
|
|||||||
@CacheEvict(value = USERS_CACHE, allEntries = true, beforeInvocation = true)
|
@CacheEvict(value = USERS_CACHE, allEntries = true, beforeInvocation = true)
|
||||||
public void updateMyProfile(UpdateMyProfileRequest updateProfileRequest) {
|
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();
|
var userRepresentation = user.toRepresentation();
|
||||||
|
|
||||||
if (userRepresentation.getFederatedIdentities() != null && !userRepresentation.getFederatedIdentities().isEmpty() && !updateProfileRequest.getEmail()
|
if (userRepresentation.getFederatedIdentities() != null && !userRepresentation.getFederatedIdentities().isEmpty() && !updateProfileRequest.getEmail()
|
||||||
@ -372,10 +377,7 @@ public class UserService {
|
|||||||
userRepresentation.setFirstName(updateProfileRequest.getFirstName());
|
userRepresentation.setFirstName(updateProfileRequest.getFirstName());
|
||||||
userRepresentation.setLastName(updateProfileRequest.getLastName());
|
userRepresentation.setLastName(updateProfileRequest.getLastName());
|
||||||
userRepresentation.setEmail(updateProfileRequest.getEmail());
|
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
|
this.setUsername(userRepresentation, updateProfileRequest.getEmail());
|
||||||
if (userRepresentation.getUsername().equals(userRepresentation.getEmail())) {
|
|
||||||
userRepresentation.setUsername(updateProfileRequest.getEmail());
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
user.update(userRepresentation);
|
user.update(userRepresentation);
|
||||||
@ -395,6 +397,12 @@ public class UserService {
|
|||||||
.build());
|
.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) {
|
private void validatePassword(String username, String password) {
|
||||||
|
|
||||||
@ -492,7 +500,7 @@ public class UserService {
|
|||||||
@CacheEvict(value = USERS_CACHE, allEntries = true, beforeInvocation = true)
|
@CacheEvict(value = USERS_CACHE, allEntries = true, beforeInvocation = true)
|
||||||
public void updateProfile(String userId, UpdateProfileRequest updateProfileRequest) {
|
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();
|
var userRepresentation = user.toRepresentation();
|
||||||
|
|
||||||
if (userRepresentation.getFederatedIdentities() != null && !userRepresentation.getFederatedIdentities().isEmpty() && !updateProfileRequest.getEmail()
|
if (userRepresentation.getFederatedIdentities() != null && !userRepresentation.getFederatedIdentities().isEmpty() && !updateProfileRequest.getEmail()
|
||||||
@ -507,9 +515,7 @@ public class UserService {
|
|||||||
userRepresentation.setFirstName(updateProfileRequest.getFirstName());
|
userRepresentation.setFirstName(updateProfileRequest.getFirstName());
|
||||||
userRepresentation.setLastName(updateProfileRequest.getLastName());
|
userRepresentation.setLastName(updateProfileRequest.getLastName());
|
||||||
userRepresentation.setEmail(updateProfileRequest.getEmail());
|
userRepresentation.setEmail(updateProfileRequest.getEmail());
|
||||||
if (userRepresentation.getUsername().equals(userRepresentation.getEmail())) {
|
this.setUsername(userRepresentation, updateProfileRequest.getEmail());
|
||||||
userRepresentation.setUsername(updateProfileRequest.getEmail());
|
|
||||||
}
|
|
||||||
|
|
||||||
user.update(userRepresentation);
|
user.update(userRepresentation);
|
||||||
|
|
||||||
@ -527,7 +533,7 @@ public class UserService {
|
|||||||
|
|
||||||
public User activateProfile(String userId, boolean isActive) {
|
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();
|
var userRepresentation = user.toRepresentation();
|
||||||
|
|
||||||
userRepresentation.setEnabled(isActive);
|
userRepresentation.setEnabled(isActive);
|
||||||
@ -546,7 +552,7 @@ public class UserService {
|
|||||||
.details(Map.of("Profile activated", isActive))
|
.details(Map.of("Profile activated", isActive))
|
||||||
.build());
|
.build());
|
||||||
|
|
||||||
return convert(realmService.realm(TenantContext.getTenantId()).users().get(userId).toRepresentation());
|
return convert(this.getTenantUsersResource().get(userId).toRepresentation());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user