コード例 #1
0
  @RequestMapping(method = RequestMethod.POST)
  public String processEdit(
      @ModelAttribute User user,
      BindingResult result,
      SessionStatus status,
      HttpServletRequest request) {
    new UserValidator(roleService).validate(user, result);
    if (result.hasErrors()) {
      return "config/users/password";
    } else {

      if (user.getUnencryptedPassword() == null
          || user.getUnencryptedPassword().trim().equals("")) {
        result.rejectValue("password", null, "You must enter a new password.");
        return "config/users/password";
      }

      String currentUserName = SecurityContextHolder.getContext().getAuthentication().getName();

      User databaseUser = userService.loadUser(user.getName());
      if (databaseUser != null && !databaseUser.getId().equals(user.getId())) {
        // TODO check this out
        result.rejectValue(
            "currentPassword", "The user has changed since starting this procedure.");
        return "config/users/password";
      }

      if (userService.isCorrectPassword(databaseUser, user.getCurrentPassword())) {
        user.setHasChangedInitialPassword(true);
        user.setLastPasswordChangedDate(new Date());
        Object currentUserObject =
            SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (currentUserObject instanceof ThreadFixUserDetails) {
          ThreadFixUserDetails details = (ThreadFixUserDetails) currentUserObject;
          details.setHasChangedInitialPassword(true);
        }
        userService.storeUser(user);
        status.setComplete();
        log.info("The User " + currentUserName + " has completed the password change.");
        ControllerUtils.addSuccessMessage(request, "The password change was successful.");
        return "redirect:/configuration/users/password";

      } else {
        log.info("An incorrect password was submitted during a password change attempt.");
        result.rejectValue("currentPassword", null, "That was not the correct password.");
        return "config/users/password";
      }
    }
  }
コード例 #2
0
  @RequestMapping(method = RequestMethod.GET)
  public ModelAndView editForm(HttpServletRequest request) {

    String userName = SecurityContextHolder.getContext().getAuthentication().getName();

    User user = null;

    Object successMessage = ControllerUtils.getSuccessMessage(request);

    if (userName != null) {
      user = userService.loadUser(userName);
    }

    if (user == null) {
      log.warn(ResourceNotFoundException.getLogMessage("User", userName));
      throw new ResourceNotFoundException();
    }

    ModelAndView mav = new ModelAndView("config/users/password");
    mav.addObject(user);
    mav.addObject("successMessage", successMessage);
    return mav;
  }