}
@Transactional
@RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
public String updatePassword(String email, String password, String passwordConfirm, String oneTimeToken, HttpServletRequest request, Model model, RedirectAttributes redirectAttributes) throws UnsupportedEncodingException {
User user = userDAO.findByEmail(email);
if (hasInvalidToken(user, oneTimeToken, request, redirectAttributes)) {
return "redirect:/message";
}
boolean passwordFormatError = !PASSWORD_MATCHER.matcher(String.valueOf(password)).matches();
boolean passwordsMatchError = !String.valueOf(password).equals(passwordConfirm);
if (passwordFormatError || passwordsMatchError) {
model.addAttribute("passwordPattern", User.PASSWORD_PATTERN);
model.addAttribute("environment", environment);
model.addAttribute("email", email);
model.addAttribute("oneTimeToken", oneTimeToken);
List<String> errors = new ArrayList<>();
if (passwordFormatError) {
errors.add("validation.user.password");
}
if (passwordsMatchError) {
errors.add("validation.user.passwordNonMatching");
}
model.addAttribute("validationErrors", errors);
logger.info("Validation error while trying to update password for " + email + "\n" + errors);
return "updatePassword";
}
user.setPassword(passwordEncoder.encode(password));
userDAO.save(user);
redirectAttributes.addFlashAttribute("message", "Your password has been updated");
redirectAttributes.addFlashAttribute("title", "Password Updated");
logger.info("Password updated for " + email);