Search in sources :

Example 1 with UserService

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());
}
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)

Aggregations

Auditor (amu.zhcet.auth.Auditor)1 PasswordReset (amu.zhcet.auth.password.PasswordReset)1 PasswordValidationException (amu.zhcet.auth.password.PasswordValidationException)1 User (amu.zhcet.data.user.User)1 UserService (amu.zhcet.data.user.UserService)1 Collections (java.util.Collections)1 Optional (java.util.Optional)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 PostMapping (org.springframework.web.bind.annotation.PostMapping)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 RequestParam (org.springframework.web.bind.annotation.RequestParam)1 RedirectAttributes (org.springframework.web.servlet.mvc.support.RedirectAttributes)1