use of amu.zhcet.auth.password.PasswordReset 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());
}
use of amu.zhcet.auth.password.PasswordReset in project zhcet-web by zhcet-amu.
the class PasswordResetController method resetPassword.
@GetMapping
public String resetPassword(Model model, @RequestParam String hash, @RequestParam("auth") String token) {
try {
passwordResetService.grantAccess(hash, token);
if (!model.containsAttribute("password")) {
PasswordReset passwordReset = new PasswordReset();
passwordReset.setHash(hash);
passwordReset.setToken(token);
model.addAttribute("password", passwordReset);
model.addAttribute("blacklist", Collections.EMPTY_LIST);
}
} catch (TokenValidationException tve) {
log.warn("Token Verification : Password Reset : {}", tve);
model.addAttribute("error", tve.getMessage());
}
return "user/reset_password";
}
Aggregations