in services/src/main/java/org/apache/custos/service/management/UserManagementService.java [949:1076]
public OperationStatus linkUserProfile(LinkUserProfileRequest request) {
try {
LOGGER.debug("Request received to linkUserProfile at " + request.getTenantId());
GetUserManagementSATokenRequest userManagementSATokenRequest = GetUserManagementSATokenRequest.newBuilder()
.setClientId(request.getIamClientId())
.setClientSecret(request.getIamClientSecret())
.setTenantId(request.getTenantId())
.build();
AuthToken token = identityService.getUserManagementServiceAccountAccessToken(userManagementSATokenRequest);
if (token != null && StringUtils.isNotBlank(token.getAccessToken())) {
UserSearchMetadata metadata = UserSearchMetadata.newBuilder().setUsername(request.getCurrentUsername()).build();
UserSearchRequest searchRequest = UserSearchRequest.newBuilder()
.setClientId(request.getIamClientId())
.setTenantId(request.getTenantId())
.setAccessToken(token.getAccessToken())
.setUser(metadata)
.build();
UserRepresentation userTobeLinked = iamAdminService.getUser(searchRequest);
if (userTobeLinked != null && StringUtils.isNotBlank(userTobeLinked.getUsername())) {
UserSearchMetadata exMetadata = UserSearchMetadata.newBuilder().setUsername(request.getPreviousUsername()).build();
UserSearchRequest exSearchRequest = UserSearchRequest.newBuilder()
.setClientId(request.getIamClientId())
.setTenantId(request.getTenantId())
.setAccessToken(token.getAccessToken())
.setUser(exMetadata)
.build();
UserRepresentation exRep = iamAdminService.getUser(exSearchRequest);
if (exRep != null && StringUtils.isNotBlank(exRep.getUsername())) {
boolean profileUpdate = false;
List<UserAttribute> userAttributeList = new ArrayList<>();
for (String attribute : request.getLinkingAttributesList()) {
if ("name".equals(attribute)) {
profileUpdate = true;
userTobeLinked = userTobeLinked.toBuilder()
.setFirstName(exRep.getFirstName())
.setLastName(exRep.getLastName())
.build();
} else if (("email").equals(attribute)) {
profileUpdate = true;
userTobeLinked = userTobeLinked.toBuilder().setEmail(exRep.getEmail()).build();
} else {
List<UserAttribute> userAttributes = exRep.getAttributesList().stream().
filter(atr -> atr.getKey().equals(attribute)).toList();
if (!userAttributes.isEmpty()) {
UserAttribute userAttribute = userAttributes.get(0);
userAttributeList.add(userAttribute);
}
}
}
if (profileUpdate) {
UpdateUserProfileRequest updateUserProfileRequest = UpdateUserProfileRequest
.newBuilder()
.setUser(userTobeLinked)
.setAccessToken(token.getAccessToken())
.setTenantId(request.getTenantId())
.build();
iamAdminService.updateUserProfile(updateUserProfileRequest);
}
if (!userAttributeList.isEmpty()) {
AddUserAttributesRequest addUserAttributesRequest = AddUserAttributesRequest
.newBuilder()
.addUsers(request.getCurrentUsername())
.addAllAttributes(userAttributeList)
.setTenantId(request.getTenantId())
.setAccessToken(token.getAccessToken())
.setClientId(request.getIamClientId())
.setPerformedBy(request.getPerformedBy())
.build();
iamAdminService.addUserAttributes(addUserAttributesRequest);
}
UserRepresentation updatedUser = iamAdminService.getUser(searchRequest);
if (updatedUser != null) {
UserProfile profile = this.convertToProfile(updatedUser);
org.apache.custos.core.user.profile.api.UserProfileRequest req = org.apache.custos.core.user.profile.api.UserProfileRequest
.newBuilder()
.setTenantId(request.getTenantId())
.setProfile(profile)
.build();
UserProfile existingProfile = userProfileService.getUserProfile(req);
if (existingProfile == null || StringUtils.isBlank(existingProfile.getUsername())) {
userProfileService.createUserProfile(req);
} else {
userProfileService.updateUserProfile(req);
}
}
CheckingResponse response = CheckingResponse.newBuilder().setIsExist(true).build();
return OperationStatus.newBuilder().setStatus(response.getIsExist()).build();
} else {
String msg = "Cannot found existing user ";
LOGGER.error(msg);
throw new EntityNotFoundException(msg);
}
}
LOGGER.error("Cannot find an existing user to be linked. User name: " + request.getCurrentUsername());
throw new EntityNotFoundException("Cannot find an existing user to be linked. User name: " + request.getCurrentUsername());
}
LOGGER.error("Cannot find the access token for User search request. User name: " + request.getCurrentUsername());
throw new RuntimeException("Cannot find the access token for User search request. User name: " + request.getCurrentUsername());
} catch (Exception ex) {
String msg = "Error occurred while linking user profile in tenant " + ex.getMessage();
LOGGER.error(msg, ex);
if (ex.getMessage().contains("UNAUTHENTICATED")) {
throw new AuthenticationException(msg, ex);
} else {
throw new InternalServerException(msg, ex);
}
}
}