in services/src/main/java/org/apache/custos/service/management/UserManagementService.java [730:822]
public UserProfile updateUserProfile(UserProfileRequest request) {
try {
LOGGER.debug("Request received to updateUserProfile for " + request.getUserProfile().getUsername() + " in " + request.getTenantId());
UserSearchMetadata metadata = UserSearchMetadata.newBuilder().setUsername(request.getUserProfile().getUsername()).build();
UserSearchRequest info = UserSearchRequest
.newBuilder()
.setAccessToken(request.getAccessToken())
.setTenantId(request.getTenantId())
.setUser(metadata)
.build();
CheckingResponse response = iamAdminService.isUserExist(info);
if (!response.getIsExist()) {
String msg = "User not found with username " + request.getUserProfile().getUsername();
LOGGER.error(msg);
throw new InternalServerException(msg);
}
UserRepresentation userRepresentation = iamAdminService.getUser(info);
userRepresentation = userRepresentation
.toBuilder()
.setFirstName(request.getUserProfile().getFirstName())
.setLastName(request.getUserProfile().getLastName())
.build();
UpdateUserProfileRequest updateUserProfileRequest = UpdateUserProfileRequest
.newBuilder()
.setUser(userRepresentation)
.setAccessToken(request.getAccessToken())
.setTenantId(request.getTenantId())
.build();
OperationStatus operationStatus = iamAdminService.updateUserProfile(updateUserProfileRequest);
if (operationStatus != null && operationStatus.getStatus()) {
try {
org.apache.custos.core.user.profile.api.UserProfileRequest userProfileRequest = org.apache.custos.core.user.profile.api.UserProfileRequest.
newBuilder()
.setProfile(request.getUserProfile())
.setTenantId(request.getTenantId())
.build();
UserProfile profile = userProfileService.getUserProfile(userProfileRequest);
if (profile != null && StringUtils.isNotBlank(profile.getUsername())) {
profile = profile.toBuilder()
.setFirstName(request.getUserProfile().getFirstName())
.setLastName(request.getUserProfile().getLastName())
.build();
userProfileRequest = userProfileRequest.toBuilder().setProfile(profile).build();
userProfileService.updateUserProfile(userProfileRequest);
return userProfileService.getFullUserProfile(userProfileRequest);
} else {
UserProfile userProfile = UserProfile.newBuilder()
.setFirstName(request.getUserProfile().getFirstName())
.setLastName(request.getUserProfile().getLastName())
.build();
userProfileRequest = userProfileRequest.toBuilder().setProfile(userProfile).build();
userProfileService.createUserProfile(userProfileRequest);
return userProfileService.getFullUserProfile(userProfileRequest);
}
} catch (Exception ex) {
String msg = "Error occurred while saving user profile in local DB, rolling back IAM service" + ex.getMessage();
LOGGER.error(msg);
UpdateUserProfileRequest rollingRequest = UpdateUserProfileRequest.newBuilder()
.setUser(userRepresentation)
.setAccessToken(request.getAccessToken())
.setTenantId(request.getTenantId())
.build();
iamAdminService.updateUserProfile(rollingRequest);
throw new RuntimeException(msg, ex);
}
} else {
String msg = "Cannot update user profile in keycloak for user " + request.getUserProfile().getUsername();
LOGGER.error(msg);
throw new InternalServerException(msg);
}
} catch (Exception ex) {
String msg = "Error occurred while updating user profile " + ex.getMessage();
LOGGER.error(msg);
if (ex.getMessage().contains("UNAUTHENTICATED")) {
throw new AuthenticationException(msg, ex);
} else {
throw new InternalServerException(msg, ex);
}
}
}