use of io.gravitee.rest.api.model.permissions.RolePermissionAction.UPDATE in project gravitee-management-rest-api by gravitee-io.
the class UserServiceImpl method update.
@Override
public UserEntity update(String id, UpdateUserEntity updateUserEntity, String newsletterEmail) {
try {
LOGGER.debug("Updating {}", updateUserEntity);
Optional<User> checkUser = userRepository.findById(id);
if (!checkUser.isPresent()) {
throw new UserNotFoundException(id);
}
User user = checkUser.get();
User previousUser = new User(user);
// Set date fields
user.setUpdatedAt(new Date());
// Set variant fields
if (updateUserEntity.getPicture() != null) {
user.setPicture(updateUserEntity.getPicture());
}
if (updateUserEntity.getFirstname() != null) {
user.setFirstname(updateUserEntity.getFirstname());
}
if (updateUserEntity.getLastname() != null) {
user.setLastname(updateUserEntity.getLastname());
}
if (updateUserEntity.getEmail() != null && !updateUserEntity.getEmail().equals(user.getEmail())) {
if (isInternalUser(user)) {
// sourceId can be updated only for user registered into the Gravitee Repository
// in that case, check if the email is available before update sourceId
final Optional<User> optionalUser = userRepository.findBySource(user.getSource(), updateUserEntity.getEmail(), user.getOrganizationId());
if (optionalUser.isPresent()) {
throw new UserAlreadyExistsException(user.getSource(), updateUserEntity.getEmail(), user.getOrganizationId());
}
user.setSourceId(updateUserEntity.getEmail());
}
user.setEmail(updateUserEntity.getEmail());
}
if (updateUserEntity.getStatus() != null) {
user.setStatus(UserStatus.valueOf(updateUserEntity.getStatus()));
}
if (updateUserEntity.isNewsletter() != null) {
user.setNewsletterSubscribed(updateUserEntity.isNewsletter());
if (updateUserEntity.isNewsletter() && newsletterEmail != null) {
newsletterService.subscribe(newsletterEmail);
}
}
User updatedUser = userRepository.update(user);
auditService.createOrganizationAuditLog(Collections.singletonMap(USER, user.getId()), User.AuditEvent.USER_UPDATED, user.getUpdatedAt(), previousUser, user);
List<UserMetadataEntity> updatedMetadata = new ArrayList<>();
if (updateUserEntity.getCustomFields() != null && !updateUserEntity.getCustomFields().isEmpty()) {
List<UserMetadataEntity> metadata = userMetadataService.findAllByUserId(user.getId());
for (Map.Entry<String, Object> entry : updateUserEntity.getCustomFields().entrySet()) {
Optional<UserMetadataEntity> existingMeta = metadata.stream().filter(meta -> meta.getKey().equals(entry.getKey())).findFirst();
if (existingMeta.isPresent()) {
UserMetadataEntity meta = existingMeta.get();
UpdateUserMetadataEntity metadataEntity = new UpdateUserMetadataEntity();
metadataEntity.setName(meta.getName());
metadataEntity.setKey(meta.getKey());
metadataEntity.setValue(String.valueOf(entry.getValue()));
metadataEntity.setUserId(meta.getUserId());
metadataEntity.setFormat(meta.getFormat());
updatedMetadata.add(userMetadataService.update(metadataEntity));
} else {
// some additional fields may have been added after the user registration
NewUserMetadataEntity metadataEntity = new NewUserMetadataEntity();
metadataEntity.setName(entry.getKey());
metadataEntity.setValue(String.valueOf(entry.getValue()));
metadataEntity.setUserId(user.getId());
metadataEntity.setFormat(MetadataFormat.STRING);
updatedMetadata.add(userMetadataService.create(metadataEntity));
}
}
}
return convert(updatedUser, true, updatedMetadata);
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to update {}", updateUserEntity, ex);
throw new TechnicalManagementException("An error occurs while trying update " + updateUserEntity, ex);
}
}
Aggregations