Search in sources :

Example 1 with PasswordValidationException

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);
}
Also used : PasswordValidationException(amu.zhcet.auth.password.PasswordValidationException) Transactional(javax.transaction.Transactional)

Example 2 with PasswordValidationException

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());
}
Also used : User(amu.zhcet.data.user.User) RedirectAttributes(org.springframework.web.servlet.mvc.support.RedirectAttributes) PostMapping(org.springframework.web.bind.annotation.PostMapping) RequestParam(org.springframework.web.bind.annotation.RequestParam) PasswordValidationException(amu.zhcet.auth.password.PasswordValidationException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) BindingResult(org.springframework.validation.BindingResult) Controller(org.springframework.stereotype.Controller) Valid(javax.validation.Valid) Slf4j(lombok.extern.slf4j.Slf4j) Model(org.springframework.ui.Model) PasswordReset(amu.zhcet.auth.password.PasswordReset) GetMapping(org.springframework.web.bind.annotation.GetMapping) Optional(java.util.Optional) UserService(amu.zhcet.data.user.UserService) Auditor(amu.zhcet.auth.Auditor) Authentication(org.springframework.security.core.Authentication) Collections(java.util.Collections) User(amu.zhcet.data.user.User) PasswordValidationException(amu.zhcet.auth.password.PasswordValidationException) PostMapping(org.springframework.web.bind.annotation.PostMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 3 with PasswordValidationException

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";
}
Also used : UserNotFoundException(amu.zhcet.data.user.UserNotFoundException) User(amu.zhcet.data.user.User) PasswordValidationException(amu.zhcet.auth.password.PasswordValidationException) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Aggregations

PasswordValidationException (amu.zhcet.auth.password.PasswordValidationException)3 User (amu.zhcet.data.user.User)2 PostMapping (org.springframework.web.bind.annotation.PostMapping)2 Auditor (amu.zhcet.auth.Auditor)1 PasswordReset (amu.zhcet.auth.password.PasswordReset)1 UserNotFoundException (amu.zhcet.data.user.UserNotFoundException)1 UserService (amu.zhcet.data.user.UserService)1 Collections (java.util.Collections)1 Optional (java.util.Optional)1 Transactional (javax.transaction.Transactional)1 Valid (javax.validation.Valid)1 Slf4j (lombok.extern.slf4j.Slf4j)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 Authentication (org.springframework.security.core.Authentication)1 Controller (org.springframework.stereotype.Controller)1 Model (org.springframework.ui.Model)1 BindingResult (org.springframework.validation.BindingResult)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 RequestParam (org.springframework.web.bind.annotation.RequestParam)1