Java tutorial
/* * Copyright (c) 2013, Helome and/or its affiliates. All rights reserved. * Helome PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * * Created on 2014-2-24 */ package ext.usercenter; import javax.persistence.EntityManager; import models.User; import org.apache.commons.lang3.StringUtils; import play.Logger; import play.Logger.ALogger; import play.mvc.Http.Context; import play.mvc.Http.Session; import utils.DeviceUtil; import utils.jpa.IndieTransactionCall; import utils.jpa.JPAUtil; import ext.usercenter.UserAuthService.LoginResult; /** * * * @ClassName: UserCenterService * @Description: ? * @date 2014-2-24 ?6:44:01 * @author ShenTeng * */ public class UserCenterService { private static final ALogger LOGGER = Logger.of(UserCenterService.class); /** * ???? * * @param session HTTP Session * @param email * @param userpassword ? * @param userId Id * @param realname ?? * @return SUCCESS?USERNAME_DUPLICATEemail??UNKNOWN_ERROR */ public static RegisterResult registerByEmail(Session session, String email, String userpassword, Long userId, String realname) { if (null == session || StringUtils.isBlank(email) || StringUtils.isBlank(userpassword) || userId == null || StringUtils.isBlank(realname)) { throw new IllegalArgumentException("illegal method input param. params: session=" + session + ", email=" + email + ", userpassword is " + (userpassword == null ? "" : "not") + " null, userId = " + userId + ", realname = " + realname); } String device = DeviceUtil.getDeviceByUrl(Context.current().request().path()); String ip = Context.current().request().remoteAddress(); UCResult<UCUser> ucResult = UCClient.register(userpassword, null, realname, email, null, device, ip, userId); if (ucResult.isSuccess()) { LoginResult loginResult = UserAuthService.login(session, email, userpassword); if (!loginResult.state.equals(LoginResult.STATE.SUCCESS)) { LOGGER.error("register error: register success but login fail."); return new RegisterResult(RegisterResult.STATE.UNKNOWN_ERROR, null); } else { return new RegisterResult(RegisterResult.STATE.SUCCESS, ucResult.data.userpassword); } } else if (ucResult.duplicateData()) { return new RegisterResult(RegisterResult.STATE.USERNAME_DUPLICATE, null); } else { LOGGER.error("register error: " + ucResult); } return new RegisterResult(RegisterResult.STATE.UNKNOWN_ERROR, null); } /** * ???? * * @param session HTTP Session * @param username ?????? * @param userpassword ? * @param userId Id * @param realname ?? * @return SUCCESS?USERNAME_DUPLICATEusername??UNKNOWN_ERROR */ public static RegisterResult registerByUsername(Session session, String username, String userpassword, Long userId, String realname) { if (null == session || StringUtils.isBlank(username) || StringUtils.isBlank(userpassword) || userId == null || StringUtils.isBlank(realname)) { throw new IllegalArgumentException("illegal method input param. params: session=" + session + ", username=" + username + ", userpassword is " + (userpassword == null ? "" : "not") + " null, userId = " + userId + ", realname = " + realname); } String device = DeviceUtil.getDeviceByUrl(Context.current().request().path()); String ip = Context.current().request().remoteAddress(); UCResult<UCUser> ucResult = UCClient.register(userpassword, username, realname, null, null, device, ip, userId); if (ucResult.isSuccess()) { LoginResult loginResult = UserAuthService.login(session, username, userpassword); if (!loginResult.state.equals(LoginResult.STATE.SUCCESS)) { LOGGER.error("register error: register success but login fail."); return new RegisterResult(RegisterResult.STATE.UNKNOWN_ERROR, null); } else { return new RegisterResult(RegisterResult.STATE.SUCCESS, ucResult.data.userpassword); } } else if (ucResult.duplicateData()) { return new RegisterResult(RegisterResult.STATE.USERNAME_DUPLICATE, null); } else { LOGGER.error("register error: " + ucResult); } return new RegisterResult(RegisterResult.STATE.UNKNOWN_ERROR, null); } /** * email? * * @param email * @return EMAIL_EXISTemailEMAIL_NOT_EXIST:emailUNKNOWN_ERROR: */ public static EmailExistResult validateEmailExist(String email) { UCResult<UCUser> ucResult = UCClient.queryUsernameExist(email); if (ucResult.isSuccess() && null != ucResult.data) { return EmailExistResult.EMAIL_EXIST; } else if (ucResult.noMatchData()) { return EmailExistResult.EMAIL_NOT_EXIST; } else { LOGGER.error("queryUsernameExist error: " + ucResult); } return EmailExistResult.UNKNOWN_ERROR; } /** * ?? * * @param phoneNum ? * @return EXIST?NOT_EXIST:?UNKNOWN_ERROR: */ public static PhoneNumExistResult validatePhoneNumExist(String phoneNum) { UCResult<UCUser> ucResult = UCClient.queryUsernameExist(phoneNum); if (ucResult.isSuccess() && null != ucResult.data) { return PhoneNumExistResult.EXIST; } else if (ucResult.noMatchData()) { return PhoneNumExistResult.NOT_EXIST; } else { LOGGER.error("queryUsernameExist error: " + ucResult); } return PhoneNumExistResult.UNKNOWN_ERROR; } /** * ??? * * @param loginUsername ?? * @return EXIST??NOT_EXIST:??UNKNOWN_ERROR: */ public static LoginUsernameExistResult validateLoginUsernameExist(String loginUsername) { UCResult<UCUser> ucResult = UCClient.queryUsernameExist(loginUsername); if (ucResult.isSuccess() && null != ucResult.data) { return LoginUsernameExistResult.EXIST; } else if (ucResult.noMatchData()) { return LoginUsernameExistResult.NOT_EXIST; } else { LOGGER.error("queryUsernameExist error: " + ucResult); } return LoginUsernameExistResult.UNKNOWN_ERROR; } /** * ? * * @param username ?????????? * @param newPassword ? * @return SUCCESS?USERNAME_NOT_EXIST??? UNKNOWN_ERROR */ public static UserCenterService.ModifyPasswordResult modifyPassword(final Long userId, String username, String newPassword) { if (null == userId || StringUtils.isBlank(username) || StringUtils.isBlank(newPassword)) { throw new IllegalArgumentException("param can't be null or blank."); } final UCResult<UCUser> ucResult = UCClient.modifyPassword(username, newPassword); if (ucResult.isSuccess()) { JPAUtil.indieTransaction(new IndieTransactionCall() { @Override public void call(EntityManager em) { User.updateEncryptedPasswordById(userId, ucResult.data.userpassword); } }); return new UserCenterService.ModifyPasswordResult(UserCenterService.ModifyPasswordResult.STATE.SUCCESS, ucResult.data.userpassword); } else if (ucResult.noMatchData()) { return new UserCenterService.ModifyPasswordResult( UserCenterService.ModifyPasswordResult.STATE.USERNAME_NOT_EXIST, null); } else { LOGGER.error("modifyPassword error: " + ucResult); } return new UserCenterService.ModifyPasswordResult( UserCenterService.ModifyPasswordResult.STATE.UNKNOWN_ERROR, null); } /** * ???<br> * ???????????<br> * ?? * * @param session HTTP session * @param password ? * @return CORRECT??INCORRECT???NOT_LOGINUNKNOWN_ERROR */ public static ValidatePasswordResult validatePasswordForModifyUsername(Session session, String password) { if (!UserAuthService.isLogin(session)) { return ValidatePasswordResult.NOT_LOGIN; } String token = UserAuthService.getTokenInSession(session); UCResult<Void> ucResult = UCClient.checkPassword4ModifyUsername(token, password); if (ucResult.isSuccess()) { return ValidatePasswordResult.CORRECT; } else if (ucResult.noMatchData()) { return ValidatePasswordResult.INCORRECT; } else { LOGGER.error("checkPassword4ModifyUsername error: " + ucResult); } return ValidatePasswordResult.UNKNOWN_ERROR; } /** * ???<br> * ???<br> * ?? * * @param session HTTP session * @param password ? * @return CORRECT??INCORRECT???NOT_LOGINUNKNOWN_ERROR */ public static ValidatePasswordResult validatePasswordForModifyPassword(Session session, String password) { if (!UserAuthService.isLogin(session)) { return ValidatePasswordResult.NOT_LOGIN; } String token = UserAuthService.getTokenInSession(session); UCResult<Void> ucResult = UCClient.checkPassword4ModifyPassword(token, password); if (ucResult.isSuccess()) { return ValidatePasswordResult.CORRECT; } else if (ucResult.noMatchData()) { return ValidatePasswordResult.INCORRECT; } else { LOGGER.error("checkPassword4ModifyPassword error: " + ucResult); } return ValidatePasswordResult.UNKNOWN_ERROR; } /** * ???<br> * ? * * @param session HTTP session * @return ? null - */ public static PasswordSecurityGrade getPasswordSecurityGrade(Session session) { if (!UserAuthService.isLogin(session)) { return null; } String token = UserAuthService.getTokenInSession(session); UCResult<String> ucResult = UCClient.passwordSecurityGrade(token); if (ucResult.isSuccess()) { return PasswordSecurityGrade.getFromKey(ucResult.data); } else if (ucResult.noMatchData()) { return null; } else { LOGGER.error("passwordSecurityGrade error: " + ucResult); } return null; } /** * <br> * ? * * @param session HTTP session * @param newEmail * @return ModifyEmailResult */ public static ModifyEmailResult modifyEmail(Session session, String newEmail) { if (!UserAuthService.isLogin(session)) { return ModifyEmailResult.NOT_LOGIN; } String token = UserAuthService.getTokenInSession(session); UCResult<Void> ucResult = UCClient.modifyEmail(token, newEmail); if (ucResult.isSuccess()) { return ModifyEmailResult.SUCCESS; } else if (ucResult.duplicateData()) { return ModifyEmailResult.EMAIL_EXIST; } else { LOGGER.error("modifyEmail error: " + ucResult); } return ModifyEmailResult.UNKNOWN_ERROR; } /** * ?? * * @param session HTTP session * @param realname ?? * @return */ public static ModifyRealnameResult modifyRealname(Session session, String realname) { if (!UserAuthService.isLogin(session)) { return ModifyRealnameResult.NOT_LOGIN; } String token = UserAuthService.getTokenInSession(session); UCResult<Void> ucResult = UCClient.modifyRealname(token, realname); if (ucResult.isSuccess()) { return ModifyRealnameResult.SUCCESS; } else { LOGGER.error("modifyRealname error: " + ucResult); } return ModifyRealnameResult.UNKNOWN_ERROR; } /** * ?<br> * ? * * @param session HTTP session * @param newPhoneNumber ? * @return BindPhoneNumberResult */ public static BindPhoneNumberResult bindPhoneNumber(Session session, String newPhoneNumber) { if (!UserAuthService.isLogin(session)) { return BindPhoneNumberResult.NOT_LOGIN; } String token = UserAuthService.getTokenInSession(session); UCResult<Void> ucResult = UCClient.bindingPhoneNumber(token, newPhoneNumber); if (ucResult.isSuccess()) { return BindPhoneNumberResult.SUCCESS; } else if (ucResult.duplicateData()) { return BindPhoneNumberResult.PHONE_NUM_EXIST; } else if (ucResult.illegalParam()) { return BindPhoneNumberResult.ILLEGAL_PHONE_NUM; } else { LOGGER.error("bindingPhoneNumber error: " + ucResult); } return BindPhoneNumberResult.UNKNOWN_ERROR; } public enum EmailExistResult { EMAIL_EXIST, EMAIL_NOT_EXIST, UNKNOWN_ERROR } public enum PhoneNumExistResult { EXIST, NOT_EXIST, UNKNOWN_ERROR } public enum LoginUsernameExistResult { EXIST, NOT_EXIST, UNKNOWN_ERROR } public enum ModifyEmailResult { SUCCESS, EMAIL_EXIST, NOT_LOGIN, UNKNOWN_ERROR } public enum ModifyRealnameResult { SUCCESS, NOT_LOGIN, UNKNOWN_ERROR } public enum BindPhoneNumberResult { SUCCESS, PHONE_NUM_EXIST, NOT_LOGIN, ILLEGAL_PHONE_NUM, UNKNOWN_ERROR } public enum ValidatePasswordResult { CORRECT, INCORRECT, NOT_LOGIN, UNKNOWN_ERROR } public enum PasswordSecurityGrade { WEAK("weak"), MEDIUM("medium"), STRONG("strong"); private String key; PasswordSecurityGrade(String key) { this.key = key; } public static PasswordSecurityGrade getFromKey(String key) { PasswordSecurityGrade[] gradeArray = PasswordSecurityGrade.values(); for (PasswordSecurityGrade grade : gradeArray) { if (null != grade.key && grade.key.equals(key)) { return grade; } } return null; } } public static class ModifyPasswordResult { public STATE state; /** * ?? */ public String encryptedPassword; private ModifyPasswordResult(STATE state, String encryptedPassword) { this.state = state; this.encryptedPassword = encryptedPassword; } public enum STATE { SUCCESS, USERNAME_NOT_EXIST, UNKNOWN_ERROR } } public static class RegisterResult { public STATE state; /** * ?? */ public String encryptedPassword; private RegisterResult(STATE state, String encryptedPassword) { this.state = state; this.encryptedPassword = encryptedPassword; } public enum STATE { SUCCESS, USERNAME_DUPLICATE, UNKNOWN_ERROR } } }