use of amu.zhcet.auth.password.PasswordValidationException in project zhcet-web by zhcet-amu.
the class PasswordChangeService method changePassword.
/**
* Changes password of a user
* @param user User whose password is to be changed
* @param passwordChange Password Container
* @throws PasswordValidationException If password is not of correct form
*/
@Transactional
public void changePassword(User user, PasswordChange passwordChange) throws PasswordValidationException {
ErrorUtils.requireNonNullUser(user);
Assert.notNull(passwordChange, "PasswordReset should not be null");
if (!user.isEmailVerified())
throw new PasswordValidationException("Cannot change password for unverified user");
// Validate and set the password
passwordValidator.validateAndSetPasswordChange(user, passwordChange);
authManager.updatePassword(user);
}
use of amu.zhcet.auth.password.PasswordValidationException 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.PasswordValidationException in project zhcet-web by zhcet-amu.
the class PasswordChangeController method savePassword.
@PostMapping
public String savePassword(@Valid PasswordChange passwordChange, BindingResult bindingResult, RedirectAttributes redirectAttributes) {
User user = userService.getLoggedInUser().orElseThrow(UserNotFoundException::new);
if (bindingResult.hasErrors()) {
redirectAttributes.addFlashAttribute("password", passwordChange);
redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.password", bindingResult);
} else {
try {
passwordChangeService.changePassword(user, passwordChange);
redirectAttributes.addFlashAttribute("flash_messages", Flash.success("Password was changed successfully"));
return "redirect:/profile/settings#account";
} catch (PasswordValidationException pve) {
redirectAttributes.addFlashAttribute("pass_errors", pve.getMessage());
}
}
return "redirect:/profile/password/change";
}
Aggregations