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