Search in sources :

Example 1 with PasswordReset

use of ca.corefacility.bioinformatics.irida.model.user.PasswordReset in project irida by phac-nml.

the class PasswordResetController method createNewPasswordReset.

/**
 * Create a new password reset for a given {@link User} and send a reset
 * email
 *
 * @param user
 *            The user to create the reset for
 */
private void createNewPasswordReset(User user) {
    PasswordReset passwordReset = new PasswordReset(user);
    passwordResetService.create(passwordReset);
    // email the user their info
    emailController.sendPasswordResetLinkEmail(user, passwordReset);
}
Also used : PasswordReset(ca.corefacility.bioinformatics.irida.model.user.PasswordReset)

Example 2 with PasswordReset

use of ca.corefacility.bioinformatics.irida.model.user.PasswordReset in project irida by phac-nml.

the class UsersController method submitCreateUser.

/**
 * Create a new user object
 *
 * @param user
 *            User to create as a motel attribute
 * @param systemRole
 *            The system role to give to the user
 * @param confirmPassword
 *            Password confirmation
 * @param requireActivation
 *            Checkbox whether the user account needs to be activated
 * @param model
 *            Model for the view
 * @param principal
 *            The user creating the object
 *
 * @return A redirect to the user details view
 */
@RequestMapping(value = "/create", method = RequestMethod.POST)
@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MANAGER')")
public String submitCreateUser(@ModelAttribute User user, @RequestParam String systemRole, @RequestParam String confirmPassword, @RequestParam(required = false) String requireActivation, Model model, Principal principal) {
    Map<String, String> errors = new HashMap<>();
    String returnView = null;
    Locale locale = LocaleContextHolder.getLocale();
    User creator = userService.getUserByUsername(principal.getName());
    // check if we need to generate a password
    boolean generateActivation = !Strings.isNullOrEmpty(requireActivation);
    if (generateActivation) {
        user.setPassword(generatePassword());
        confirmPassword = user.getPassword();
        user.setCredentialsNonExpired(false);
    }
    // check validity of password
    if (!user.getPassword().equals(confirmPassword)) {
        errors.put("password", messageSource.getMessage("user.edit.password.match", null, locale));
    }
    // Check if there are any errors for the user creation
    if (errors.isEmpty()) {
        if (isAdmin(principal)) {
            user.setSystemRole(Role.valueOf(systemRole));
        } else {
            user.setSystemRole(Role.ROLE_USER);
        }
        try {
            user = userService.create(user);
            Long userId = user.getId();
            returnView = "redirect:/users/" + userId;
            // if the password isn't set, we'll generate a password reset
            PasswordReset passwordReset = null;
            if (generateActivation) {
                passwordReset = passwordResetService.create(new PasswordReset(user));
                logger.trace("Created password reset for activation");
            }
            emailController.sendWelcomeEmail(user, creator, passwordReset);
        } catch (ConstraintViolationException | DataIntegrityViolationException | EntityExistsException ex) {
            errors = handleCreateUpdateException(ex, locale);
        } catch (final MailSendException e) {
            logger.error("Failed to send user activation e-mail.", e);
            model.addAttribute("mailFailure", true);
        }
    }
    if (!errors.isEmpty()) {
        model.addAttribute("errors", errors);
        model.addAttribute("given_username", user.getUsername());
        model.addAttribute("given_firstName", user.getFirstName());
        model.addAttribute("given_lastName", user.getLastName());
        model.addAttribute("given_email", user.getEmail());
        model.addAttribute("given_phoneNumber", user.getPhoneNumber());
        model.addAttribute("given_requireActivation", generateActivation);
        returnView = createUserPage(model);
    }
    return returnView;
}
Also used : Locale(java.util.Locale) DTUser(ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTUser) User(ca.corefacility.bioinformatics.irida.model.user.User) MailSendException(org.springframework.mail.MailSendException) HashMap(java.util.HashMap) EntityExistsException(ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) PasswordReset(ca.corefacility.bioinformatics.irida.model.user.PasswordReset) ConstraintViolationException(javax.validation.ConstraintViolationException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with PasswordReset

use of ca.corefacility.bioinformatics.irida.model.user.PasswordReset in project irida by phac-nml.

the class PasswordResetServiceImplIT method testDeletePasswordReset.

@Test(expected = EntityNotFoundException.class)
@WithMockUser(username = "tester", roles = "ADMIN")
public void testDeletePasswordReset() {
    PasswordReset pr = passwordResetService.read("12213-123123-123123-12312");
    assertNotNull(pr);
    passwordResetService.delete("12213-123123-123123-12312");
    passwordResetService.read("12213-123123-123123-12312");
}
Also used : PasswordReset(ca.corefacility.bioinformatics.irida.model.user.PasswordReset) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.Test)

Example 4 with PasswordReset

use of ca.corefacility.bioinformatics.irida.model.user.PasswordReset in project irida by phac-nml.

the class PasswordResetServiceImplIT method testCannotUpdateAPasswordReset.

@Test(expected = UnsupportedOperationException.class)
@WithMockUser(username = "tester", roles = "ADMIN")
public void testCannotUpdateAPasswordReset() {
    PasswordReset pr = passwordResetService.read("12213-123123-123123-12312");
    Map<String, Object> change = new HashMap<>();
    User u = userService.loadUserByEmail("manager@nowhere.com");
    change.put("user_id", u.getId());
    passwordResetService.updateFields(pr.getId(), change);
}
Also used : WithMockUser(org.springframework.security.test.context.support.WithMockUser) User(ca.corefacility.bioinformatics.irida.model.user.User) HashMap(java.util.HashMap) PasswordReset(ca.corefacility.bioinformatics.irida.model.user.PasswordReset) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.Test)

Example 5 with PasswordReset

use of ca.corefacility.bioinformatics.irida.model.user.PasswordReset in project irida by phac-nml.

the class PasswordResetServiceImplIT method testCreatePasswordReset.

@Test
@WithMockUser(username = "tester", roles = "ADMIN")
public void testCreatePasswordReset() {
    PasswordReset pw1 = pw();
    passwordResetService.create(pw1);
    PasswordReset pw2 = passwordResetService.read(pw1.getId());
    if (pw2 == null) {
        fail("Failed to store and retrieve a PasswordReset to the database");
    }
    assertEquals("User should be equal", pw1.getUser(), pw2.getUser());
}
Also used : PasswordReset(ca.corefacility.bioinformatics.irida.model.user.PasswordReset) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.Test)

Aggregations

PasswordReset (ca.corefacility.bioinformatics.irida.model.user.PasswordReset)15 User (ca.corefacility.bioinformatics.irida.model.user.User)11 Test (org.junit.Test)10 WithMockUser (org.springframework.security.test.context.support.WithMockUser)4 ExtendedModelMap (org.springframework.ui.ExtendedModelMap)4 HashMap (java.util.HashMap)3 Matchers.anyString (org.mockito.Matchers.anyString)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 DTUser (ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTUser)2 ConstraintViolationException (javax.validation.ConstraintViolationException)2 CredentialsExpiredException (org.springframework.security.authentication.CredentialsExpiredException)2 EntityExistsException (ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException)1 PasswordReusedException (ca.corefacility.bioinformatics.irida.exceptions.PasswordReusedException)1 Principal (java.security.Principal)1 Locale (java.util.Locale)1 Set (java.util.Set)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 ConstraintViolation (javax.validation.ConstraintViolation)1 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)1