use of io.gravitee.management.service.exceptions.UsernameAlreadyExistsException in project gravitee-management-rest-api by gravitee-io.
the class UserServiceImpl method create.
/**
* Allows to complete the creation of a user which is pre-created.
* @param registerUserEntity a valid token and a password
* @return the user
*/
@Override
public UserEntity create(final RegisterUserEntity registerUserEntity) {
checkUserRegistrationEnabled();
try {
final String jwtSecret = environment.getProperty("jwt.secret");
if (jwtSecret == null || jwtSecret.isEmpty()) {
throw new IllegalStateException("JWT secret is mandatory");
}
final Map<String, Object> claims = new JWTVerifier(jwtSecret).verify(registerUserEntity.getToken());
final NewUserEntity newUserEntity = new NewUserEntity();
newUserEntity.setUsername(claims.get(Claims.SUBJECT).toString());
newUserEntity.setEmail(claims.get(Claims.EMAIL).toString());
newUserEntity.setFirstname(claims.get(Claims.FIRSTNAME).toString());
newUserEntity.setLastname(claims.get(Claims.LASTNAME).toString());
newUserEntity.setPassword(registerUserEntity.getPassword());
LOGGER.debug("Create an internal user {}", newUserEntity);
Optional<User> checkUser = userRepository.findByUsername(newUserEntity.getUsername());
if (checkUser.isPresent() && StringUtils.isNotBlank(checkUser.get().getPassword())) {
throw new UsernameAlreadyExistsException(newUserEntity.getUsername());
}
User user = convert(newUserEntity);
user.setId(UUID.toString(UUID.random()));
// Encrypt password if internal user
if (user.getPassword() != null) {
user.setPassword(passwordEncoder.encode(user.getPassword()));
}
// Set date fields
user.setUpdatedAt(new Date());
user = userRepository.update(user);
auditService.createPortalAuditLog(Collections.singletonMap(USER, user.getUsername()), User.AuditEvent.USER_CREATED, user.getUpdatedAt(), null, user);
return convert(user, true);
} catch (Exception ex) {
LOGGER.error("An error occurs while trying to create an internal user with the token {}", registerUserEntity.getToken(), ex);
throw new TechnicalManagementException(ex.getMessage(), ex);
}
}
use of io.gravitee.management.service.exceptions.UsernameAlreadyExistsException in project gravitee-management-rest-api by gravitee-io.
the class UserServiceImpl method create.
/**
* Allows to pre-create a user.
* @param newExternalUserEntity
* @return
*/
@Override
public UserEntity create(NewExternalUserEntity newExternalUserEntity, boolean addDefaultRole) {
try {
LOGGER.debug("Create an external user {}", newExternalUserEntity);
Optional<User> checkUser = userRepository.findById(newExternalUserEntity.getUsername());
if (checkUser.isPresent()) {
throw new UsernameAlreadyExistsException(newExternalUserEntity.getUsername());
}
User user = convert(newExternalUserEntity);
user.setId(UUID.toString(UUID.random()));
// Set date fields
user.setCreatedAt(new Date());
user.setUpdatedAt(user.getCreatedAt());
User createdUser = userRepository.create(user);
auditService.createPortalAuditLog(Collections.singletonMap(USER, user.getUsername()), User.AuditEvent.USER_CREATED, user.getCreatedAt(), null, user);
if (addDefaultRole) {
addDefaultMembership(createdUser);
}
return convert(createdUser, true);
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to create an external user {}", newExternalUserEntity, ex);
throw new TechnicalManagementException("An error occurs while trying to create an external user" + newExternalUserEntity, ex);
}
}
Aggregations