Example #1
0
  /**
   * Method for user reset password
   *
   * @param user
   * @return
   */
  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  @Path("resetpassword/mail")
  @Produces(MediaType.APPLICATION_JSON)
  public UserPasswordResetResponse resetPasswordWithMail(final User user) {
    String email = user.getEmail();
    final EntityManager em = emf.createEntityManager();
    Query queryE = em.createNamedQuery("User.findByEmail");
    queryE.setParameter("email", email);
    List<User> userList = queryE.getResultList();
    if (userList.isEmpty() || userList.size() > 1) {
      return new UserPasswordResetResponse(AuthenticationConstants.RESETPASSWORDFAILURE);
    }

    final User userEntity = userList.get(0);
    userEntity.setPassword(user.getPassword());

    String tokenGotFromServer = TokenHolder.userPasswordTokenMap.get(userEntity.getUserId());
    // The user is not request for reset password
    if (tokenGotFromServer == null) {
      return new UserPasswordResetResponse(AuthenticationConstants.RESETPASSWORDFAILURE);
    }
    // Remove the stake that holds the user password reset.
    TokenHolder.userPasswordTokenMap.remove(userEntity.getUserId());

    TransactionManager.manage(
        new Transactional(em) {
          @Override
          public void transact() {
            em.persist(userEntity);
          }
        });
    return new UserPasswordResetResponse(AuthenticationConstants.RESETPASSWORDSUCCESS);
  }
Example #2
0
 public void setUser(User user) {
   this.user = user;
   if (user != null) {
     this.userId = user.getUserId();
   } else {
     this.userId = null;
   }
 }
Example #3
0
  @GET
  @Produces(MediaType.APPLICATION_JSON)
  @Path("resetpassword/webpage")
  public Response getPasswordResetWebPage(@QueryParam("mail") String mail)
      throws MessagingException, IOException {
    if ((mail == null || mail.trim().equals(""))) {
      return Response.ok(AuthenticationConstants.EMAILPROVIDEDISNOTCORRECT).build();
    }

    final EntityManager em = emf.createEntityManager();
    Query queryE = em.createNamedQuery("User.findByEmail");
    queryE.setParameter("email", mail);
    List<User> userList = queryE.getResultList();
    if (userList.isEmpty() || userList.size() > 1) {
      return Response.ok(AuthenticationConstants.EMAILPROVIDEDISNOTCORRECT).build();
    }

    ServletContext sc = servletConfig.getServletContext();
    Properties tzMediaProperties = PropertiesUtils.getProperties(sc);
    String subject = tzMediaProperties.getProperty("password.retrival.mail.subject");
    String tail = tzMediaProperties.getProperty("password.retrival.mail.tail");

    final User userEntity = userList.get(0);
    String tokeyGenerated = TokenGenerator.nextToken();

    // Put the stake that holds the user password reset.
    TokenHolder.userPasswordTokenMap.put(userEntity.getUserId(), tokeyGenerated);
    String body =
        "<p>亲爱的"
            + userEntity.getEmail()
            + ",</p><p>"
            + "重新设置嘟嘟囔囔密码请点击下面的链接:</p>"
            // 链接
            + "<p><a href="
            + uriInfo.getBaseUri().toString().replace("resources", "#")
            + "resetuserpassword>"
            + uriInfo.getBaseUri().toString().replace("resources", "#")
            + "resetuserpassword</a>"
            + "</p>"
            // mail last text
            + tail;
    SendCloudMail.send(mail, subject, body);
    // TZMediaMail.send(mail, subject, body, null, sc);
    return Response.ok(AuthenticationConstants.EMAILSUCCESSFULLYSEND).build();
  }