Search in sources :

Example 1 with EmailExistsException

use of uk.ac.bris.cs.rfideasalreadytaken.lumberjack.exceptions.EmailExistsException in project lumberjack by fn-ctional.

the class AuthenticationController method registerUserAccount.

@PostMapping("/registration")
public ModelAndView registerUserAccount(@ModelAttribute("user") @Valid AdminUserDTO accountDTO, BindingResult result, WebRequest request, Errors errors, Model model) {
    if (result.hasErrors()) {
        return new ModelAndView("registration", "user", accountDTO);
    }
    try {
        AdminUser registered = createUserAccount(accountDTO, result);
        String appUrl = request.getContextPath();
        eventPublisher.publishEvent(new OnRegistrationCompleteEvent(registered, request.getLocale(), appUrl));
        model.addAttribute("messageType", "Registration");
        model.addAttribute("messageString", "Check your emails for a verification link!");
    } catch (EmailNotPermittedException e) {
        model.addAttribute("messageType", "Failed Registration");
        model.addAttribute("messageString", "You are not permitted to create an account.");
    } catch (EmailExistsException e) {
        model.addAttribute("messageType", "Failed Registration");
        model.addAttribute("messageString", "A user with this email address has already registered.");
    } catch (Exception e) {
        e.printStackTrace();
        model.addAttribute("messageType", "Registration Error");
        model.addAttribute("messageString", "Your registration failed!");
    }
    return new ModelAndView("message", "user", accountDTO);
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) EmailNotPermittedException(uk.ac.bris.cs.rfideasalreadytaken.lumberjack.exceptions.EmailNotPermittedException) AdminUser(uk.ac.bris.cs.rfideasalreadytaken.lumberjack.authentication.data.AdminUser) EmailExistsException(uk.ac.bris.cs.rfideasalreadytaken.lumberjack.exceptions.EmailExistsException) EmailNotPermittedException(uk.ac.bris.cs.rfideasalreadytaken.lumberjack.exceptions.EmailNotPermittedException) MailAuthenticationException(org.springframework.mail.MailAuthenticationException) SQLException(java.sql.SQLException) EmailExistsException(uk.ac.bris.cs.rfideasalreadytaken.lumberjack.exceptions.EmailExistsException)

Example 2 with EmailExistsException

use of uk.ac.bris.cs.rfideasalreadytaken.lumberjack.exceptions.EmailExistsException in project lumberjack by fn-ctional.

the class UserService method registerNewUserAccount.

@Transactional
@Override
public AdminUser registerNewUserAccount(AdminUserDTO accountDTO) throws EmailNotPermittedException, EmailExistsException, SQLException {
    if (authenticationBackend.userExists(accountDTO.getEmail())) {
        throw new EmailExistsException("There is already an account with that email address: " + accountDTO.getEmail());
    }
    if (!authenticationBackend.emailPermitted(accountDTO.getEmail())) {
        throw new EmailNotPermittedException("The following email is not on the list of permitted emails: " + accountDTO.getEmail());
    }
    AdminUser user = new AdminUser();
    user.setName(accountDTO.getName());
    user.setPassword(passwordEncoder.encode(accountDTO.getPassword()));
    user.setEmail(accountDTO.getEmail());
    user.setEnabled(false);
    authenticationBackend.addAdminUser(user);
    return user;
}
Also used : EmailNotPermittedException(uk.ac.bris.cs.rfideasalreadytaken.lumberjack.exceptions.EmailNotPermittedException) AdminUser(uk.ac.bris.cs.rfideasalreadytaken.lumberjack.authentication.data.AdminUser) EmailExistsException(uk.ac.bris.cs.rfideasalreadytaken.lumberjack.exceptions.EmailExistsException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

AdminUser (uk.ac.bris.cs.rfideasalreadytaken.lumberjack.authentication.data.AdminUser)2 EmailExistsException (uk.ac.bris.cs.rfideasalreadytaken.lumberjack.exceptions.EmailExistsException)2 EmailNotPermittedException (uk.ac.bris.cs.rfideasalreadytaken.lumberjack.exceptions.EmailNotPermittedException)2 SQLException (java.sql.SQLException)1 MailAuthenticationException (org.springframework.mail.MailAuthenticationException)1 Transactional (org.springframework.transaction.annotation.Transactional)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1