Search in sources :

Example 1 with EmailAlreadyUsedException

use of de.tum.in.www1.artemis.web.rest.errors.EmailAlreadyUsedException in project ArTEMiS by ls1intum.

the class AccountResource method registerAccount.

/**
 * POST  /register : register the user.
 *
 * @param managedUserVM the managed user View Model
 * @throws InvalidPasswordException 400 (Bad Request) if the password is incorrect
 * @throws EmailAlreadyUsedException 400 (Bad Request) if the email is already used
 * @throws LoginAlreadyUsedException 400 (Bad Request) if the login is already used
 */
@PostMapping("/register")
@Timed
@ResponseStatus(HttpStatus.CREATED)
public void registerAccount(@Valid @RequestBody ManagedUserVM managedUserVM) {
    if (!checkPasswordLength(managedUserVM.getPassword())) {
        throw new InvalidPasswordException();
    }
    userRepository.findOneByLogin(managedUserVM.getLogin().toLowerCase()).ifPresent(u -> {
        throw new LoginAlreadyUsedException();
    });
    userRepository.findOneByEmailIgnoreCase(managedUserVM.getEmail()).ifPresent(u -> {
        throw new EmailAlreadyUsedException();
    });
    User user = userService.registerUser(managedUserVM);
    mailService.sendActivationEmail(user);
}
Also used : User(de.tum.in.www1.artemis.domain.User) Timed(com.codahale.metrics.annotation.Timed)

Example 2 with EmailAlreadyUsedException

use of de.tum.in.www1.artemis.web.rest.errors.EmailAlreadyUsedException in project ArTEMiS by ls1intum.

the class UserResource method createUser.

/**
 * POST  /users  : Creates a new user.
 * <p>
 * Creates a new user if the login and email are not already used, and sends an
 * mail with an activation link.
 * The user needs to be activated on creation.
 *
 * @param managedUserVM the user to create
 * @return the ResponseEntity with status 201 (Created) and with body the new user, or with status 400 (Bad Request) if the login or email is already in use
 * @throws URISyntaxException if the Location URI syntax is incorrect
 * @throws BadRequestAlertException 400 (Bad Request) if the login or email is already in use
 */
@PostMapping("/users")
@Timed
@Secured(AuthoritiesConstants.ADMIN)
public ResponseEntity<User> createUser(@Valid @RequestBody ManagedUserVM managedUserVM) throws URISyntaxException {
    log.debug("REST request to save User : {}", managedUserVM);
    if (managedUserVM.getId() != null) {
        throw new BadRequestAlertException("A new user cannot already have an ID", "userManagement", "idexists");
    // Lowercase the user login before comparing with database
    } else if (userRepository.findOneByLogin(managedUserVM.getLogin().toLowerCase()).isPresent()) {
        throw new LoginAlreadyUsedException();
    } else if (userRepository.findOneByEmailIgnoreCase(managedUserVM.getEmail()).isPresent()) {
        throw new EmailAlreadyUsedException();
    } else {
        User newUser = userService.createUser(managedUserVM);
        mailService.sendCreationEmail(newUser);
        return ResponseEntity.created(new URI("/api/users/" + newUser.getLogin())).headers(HeaderUtil.createAlert("userManagement.created", newUser.getLogin())).body(newUser);
    }
}
Also used : BadRequestAlertException(de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException) User(de.tum.in.www1.artemis.domain.User) LoginAlreadyUsedException(de.tum.in.www1.artemis.web.rest.errors.LoginAlreadyUsedException) URI(java.net.URI) EmailAlreadyUsedException(de.tum.in.www1.artemis.web.rest.errors.EmailAlreadyUsedException) Secured(org.springframework.security.access.annotation.Secured) Timed(com.codahale.metrics.annotation.Timed)

Aggregations

Timed (com.codahale.metrics.annotation.Timed)2 User (de.tum.in.www1.artemis.domain.User)2 BadRequestAlertException (de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException)1 EmailAlreadyUsedException (de.tum.in.www1.artemis.web.rest.errors.EmailAlreadyUsedException)1 LoginAlreadyUsedException (de.tum.in.www1.artemis.web.rest.errors.LoginAlreadyUsedException)1 URI (java.net.URI)1 Secured (org.springframework.security.access.annotation.Secured)1