@PUT @Path("changepswd") public Response changePassword(final User user) { User userFound = null; final EntityManager em = emf.createEntityManager(); if (UserType.EMAIL.equals(user.getUserType())) { userFound = UserUtil.findUserByKeyAndType(em, user.getEmail(), user.getUserType().name()); } else if (UserType.MOBILE.equals(user.getUserType())) { userFound = UserUtil.findUserByKeyAndType(em, user.getPhoneNumber(), user.getUserType().name()); } if (userFound != null) { final User userEntity = userFound; userEntity.setPassword(user.getPassword()); TransactionManager.manage( new Transactional(em) { @Override public void transact() { em.merge(userEntity); } }); return Response.ok(AuthenticationConstants.UPDATESUCCESS).build(); } else { String[] keysForLog = {user.getEmail(), user.getPhoneNumber(), user.getUserType().name()}; Logger.getLogger(UsersResource.class.getName()) .log( Level.INFO, "Change password faild: mail: {0}, phoneNumber: {1}, userType: {2}.", keysForLog); return Response.ok(AuthenticationConstants.UPDATEFAILURE).build(); } }
/** * 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); }
@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(); }
/** * Method for user register * * @param user * @return */ @POST @Consumes(MediaType.APPLICATION_JSON) public Response postUser(final User user) { String[] userKeysForLog = { user.getQq(), user.getWeibo(), user.getEmail(), user.getPhoneNumber() }; Logger.getLogger(UsersResource.class.getName()) .log( Level.INFO, "######### User registeration: QQ: {0}, Weibo: {1}, Mail: {2}, Mobile: {3}.", userKeysForLog); boolean userExist = false; switch (user.getUserType()) { case QQ: userExist = userExist(user.getQq(), user.getUserType()); break; case WEIBO: userExist = userExist(user.getWeibo(), user.getUserType()); break; case MOBILE: userExist = userExist(user.getPhoneNumber(), user.getUserType()); break; case EMAIL: userExist = userExist(user.getEmail(), user.getUserType()); break; default: break; } if (userExist) { Logger.getLogger(UsersResource.class.getName()) .log( Level.WARNING, "######### User registeration failed: QQ: {0}, Weibo: {1}, Mail: {2}, Mobile: {3}.", userKeysForLog); return Response.ok(AuthenticationConstants.DUPLICATEUSERID).entity(user).build(); } if (user.getUserType() == null || UserType.DUDU.equals(user.getUserType())) { Logger.getLogger(UsersResource.class.getName()) .log( Level.WARNING, "######### User registeration failed: QQ: {0}, Weibo: {1}, Mail: {2}, Mobile: {3}.", userKeysForLog); return Response.ok(AuthenticationConstants.USERTYPEWRONG).entity(user).build(); } if (user.getUserName() != null) { user.setUserName(user.getUserName().trim()); } final EntityManager em = emf.createEntityManager(); TransactionManager.manage( new Transactional(em) { @Override public void transact() { em.persist(user); Calendar today = Calendar.getInstance(); today.setTime(new Date()); Query queryCD = em.createNamedQuery("CouponDefinition.findValidCoupon"); queryCD.setParameter("expiryDate", today); List<CouponDefinition> couponDefinitions = queryCD.getResultList(); for (CouponDefinition cd : couponDefinitions) { if (cd.isForAllUser()) { UserCoupon userCoupon = new UserCoupon(user.getUserId()); userCoupon.setUser(user); userCoupon.setCouponDefinitionNumber(cd.getCouponDefinitionNumber()); userCoupon.setCouponDefinition(cd); em.persist(userCoupon); } } } }); return Response.ok(AuthenticationConstants.REGISTERSUCCESS).entity(user).build(); }