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);
}
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;
}
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");
}
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);
}
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());
}
Aggregations