Search in sources :

Example 1 with UsernameAlreadyExistsException

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);
    }
}
Also used : UsernameAlreadyExistsException(io.gravitee.management.service.exceptions.UsernameAlreadyExistsException) User(io.gravitee.repository.management.model.User) JWTVerifier(com.auth0.jwt.JWTVerifier) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) UserNotFoundException(io.gravitee.management.service.exceptions.UserNotFoundException) UsernameAlreadyExistsException(io.gravitee.management.service.exceptions.UsernameAlreadyExistsException) DefaultRoleNotFoundException(io.gravitee.management.service.exceptions.DefaultRoleNotFoundException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException) IOException(java.io.IOException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 2 with UsernameAlreadyExistsException

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);
    }
}
Also used : UsernameAlreadyExistsException(io.gravitee.management.service.exceptions.UsernameAlreadyExistsException) User(io.gravitee.repository.management.model.User) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Aggregations

TechnicalManagementException (io.gravitee.management.service.exceptions.TechnicalManagementException)2 UsernameAlreadyExistsException (io.gravitee.management.service.exceptions.UsernameAlreadyExistsException)2 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)2 User (io.gravitee.repository.management.model.User)2 JWTVerifier (com.auth0.jwt.JWTVerifier)1 DefaultRoleNotFoundException (io.gravitee.management.service.exceptions.DefaultRoleNotFoundException)1 UserNotFoundException (io.gravitee.management.service.exceptions.UserNotFoundException)1 IOException (java.io.IOException)1