use of amu.zhcet.data.user.UserService in project zhcet-web by zhcet-amu.
the class PasswordResetController method savePassword.
@PostMapping
@PreAuthorize("hasAuthority('PASSWORD_CHANGE_PRIVILEGE')")
public String savePassword(@Valid PasswordReset passwordReset, BindingResult bindingResult, RedirectAttributes redirectAttributes) {
Optional<User> optionalUser = Auditor.getLoggedInAuthentication().map(Authentication::getPrincipal).filter(principal -> !principal.getClass().isAssignableFrom(User.class)).map(principal -> ((User) principal).getUserId()).flatMap(userService::findById);
if (!optionalUser.isPresent()) {
redirectAttributes.addAttribute("error", "Unknown Error");
} else {
User user = optionalUser.get();
if (bindingResult.hasErrors()) {
redirectAttributes.addFlashAttribute("password", passwordReset);
redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.password", bindingResult);
} else {
try {
passwordResetService.resetPassword(user, passwordReset);
redirectAttributes.addFlashAttribute("reset_success", true);
return "redirect:/login";
} catch (TokenValidationException tve) {
log.warn("Token Verification : Password Reset : {}", tve.getMessage());
redirectAttributes.addAttribute("error", tve.getMessage());
} catch (PasswordValidationException pve) {
log.debug("Password Verification Exception", pve);
redirectAttributes.addFlashAttribute("pass_errors", pve.getMessage());
}
}
}
return String.format("redirect:/login/password/reset?hash=%s&auth=%s", passwordReset.getHash(), passwordReset.getToken());
}
Aggregations