Пример #1
0
 /** Test redirect status detection. */
 @Test
 public void isRedirectStatus() {
   assertFalse(Redirect.isRedirectStatus(200));
   assertTrue(Redirect.isRedirectStatus(300));
   assertTrue(Redirect.isRedirectStatus(301));
   assertTrue(Redirect.isRedirectStatus(302));
   assertTrue(Redirect.isRedirectStatus(303));
   assertFalse(Redirect.isRedirectStatus(304));
   assertFalse(Redirect.isRedirectStatus(305));
   assertFalse(Redirect.isRedirectStatus(306));
   assertTrue(Redirect.isRedirectStatus(307));
   assertFalse(Redirect.isRedirectStatus(400));
 }
  /**
   * After recover password
   *
   * @return
   */
  @Override
  public String recover() {
    logger.debug("recover");
    try {
      if (!ValidatorHelper.isRequired(this.token)) {
        WebHelper.addMessageFromBundle(
            keyRepository.getResourceBundle(),
            "message.token.invalid",
            null,
            FacesMessage.SEVERITY_ERROR);
        return null;
      }

      if (!ValidatorHelper.isSamePassword(this.newPassword, this.newPasswordVerifier)) {
        WebHelper.addMessageFromBundle(
            keyRepository.getResourceBundle(),
            "error.account.password.differents",
            null,
            FacesMessage.SEVERITY_ERROR);
        return null;
      }

      this.passwordRecoveryBO.resetPassword(this.token, this.newPassword);
      return Redirect.redirect("/login");
    } catch (Exception e) {
      logger.error("revery failed", e);
    }
    return null;
  }
  // OVERRIDES
  ///////////////////////////////////////////////////////////////////////////////////////////////
  @Override
  @Secured(Secure.ROLE_USER)
  public String edit() {
    logger.debug("go to edit");
    Account account = this.accountContext.getCurrentAccount();
    if (account == null) throw new AccessDeniedException("Null account or account not found");

    this.setRendered(true);
    this.data = account.getUser().getPassword();
    return Redirect.redirect("/jsp/user/editPassword");
  }
  @Override
  public String send() {
    logger.debug("send password recovery token");
    try {
      // data = email

      if (!ValidatorHelper.isEmail(this.email)) {
        WebHelper.addMessageFromBundle(
            keyRepository.getResourceBundle(),
            "message.email.error",
            null,
            FacesMessage.SEVERITY_FATAL);
        return null;
      }
      this.passwordRecoveryBO.generateAndSendUrl(this.email);

      WebHelper.addMessageFromBundle(
          keyRepository.getResourceBundle(),
          "message.send.email.token",
          null,
          FacesMessage.SEVERITY_INFO);
      return Redirect.redirect("/jsp/user/editPassword");
    } catch (ServiceException e) {
      WebHelper.addMessageFromBundle(
          keyRepository.getResourceBundle(),
          "message.recovery.internal.error",
          null,
          FacesMessage.SEVERITY_ERROR);
      logger.error("Send password recovery token failed", e);
    } catch (NotFoundException e) {
      WebHelper.addMessageFromBundle(
          keyRepository.getResourceBundle(),
          "message.account.not.found",
          null,
          FacesMessage.SEVERITY_ERROR);
      logger.error("Send password recovery token failed", e);
    } catch (Exception e) {
      WebHelper.addMessageFromBundle(
          keyRepository.getResourceBundle(),
          "message.recovery.internal.error",
          null,
          FacesMessage.SEVERITY_ERROR);
      logger.error("Send password recovery token failed", e);
    }
    return null;
  }
  /*
   * (non-Javadoc)
   * @see fr.dorian.web.PasswordRecoveryBean#save()
   */
  @Override
  @Secured(Secure.ROLE_USER)
  public String save() {
    logger.debug("save");
    try {

      if (!ValidatorHelper.isRequired(this.data)) {
        WebHelper.addMessageFromBundle(
            keyRepository.getResourceBundle(),
            "message.token.invalid",
            null,
            FacesMessage.SEVERITY_ERROR);
        return null;
      }

      if (!ValidatorHelper.isSamePassword(this.newPassword, this.newPasswordVerifier)) {
        WebHelper.addMessageFromBundle(
            keyRepository.getResourceBundle(),
            "error.account.password.differents",
            null,
            FacesMessage.SEVERITY_ERROR);
        return null;
      }

      this.accountBO.changePassword(
          this.accountContext.getCurrentAccount().getId(), this.newPassword);
      WebHelper.addMessageFromBundle(
          keyRepository.getResourceBundle(), "message.save.ok", null, FacesMessage.SEVERITY_INFO);
      return Redirect.redirect("/jsp/user/account");
    } catch (ServiceException e) {
      e.printStackTrace();
    } catch (Exception e) {
      WebHelper.addMessageFromBundle(
          keyRepository.getResourceBundle(),
          "message.recovery.internal.error",
          null,
          FacesMessage.SEVERITY_ERROR);
      logger.error("Send password recovery token failed", e);
    }
    return null;
  }