- 오픈소스에서 저장안되는 오류 수정.
[Tadpole.git] / com.hangum.tadpole.commons.sql / src / com / hangum / tadpole / engine / query / sql / TadpoleSystem_UserQuery.java
blob163b0c3f9a1fda3e886b1204e43afc8f112b5288
1 /*******************************************************************************
2 * Copyright (c) 2013 hangum.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the GNU Lesser Public License v2.1
5 * which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
7 *
8 * Contributors:
9 * hangum - initial API and implementation
10 ******************************************************************************/
11 package com.hangum.tadpole.engine.query.sql;
13 import java.sql.SQLException;
14 import java.sql.Timestamp;
15 import java.text.DateFormat;
16 import java.text.SimpleDateFormat;
17 import java.util.ArrayList;
18 import java.util.Date;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
23 import org.apache.commons.lang.math.NumberUtils;
24 import org.apache.log4j.Logger;
26 import com.hangum.tadpole.commons.exception.TadpoleAuthorityException;
27 import com.hangum.tadpole.commons.exception.TadpoleRuntimeException;
28 import com.hangum.tadpole.commons.exception.TadpoleSQLManagerException;
29 import com.hangum.tadpole.commons.libs.core.define.PublicTadpoleDefine;
30 import com.hangum.tadpole.commons.libs.core.utils.LicenseValidator;
31 import com.hangum.tadpole.commons.libs.core.utils.SHA256Utils;
32 import com.hangum.tadpole.commons.util.ApplicationArgumentUtils;
33 import com.hangum.tadpole.commons.util.DateUtil;
34 import com.hangum.tadpole.engine.Messages;
35 import com.hangum.tadpole.engine.initialize.TadpoleSystemInitializer;
36 import com.hangum.tadpole.engine.manager.TadpoleSQLManager;
37 import com.hangum.tadpole.engine.query.dao.system.UserDAO;
38 import com.hangum.tadpole.engine.query.dao.system.UserLoginHistoryDAO;
39 import com.hangum.tadpole.preference.define.GetAdminPreference;
40 import com.ibatis.sqlmap.client.SqlMapClient;
43 /**
44 * Define User query.
46 * @author hangum
49 public class TadpoleSystem_UserQuery {
50 private static final Logger logger = Logger.getLogger(TadpoleSystem_UserQuery.class);
52 /**
54 * @param userSeq
55 * @return
56 * @throws TadpoleSQLManagerException
57 * @throws SQLException
59 public static UserDAO getUser(int userSeq)throws TadpoleSQLManagerException, SQLException {
60 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleSystemInitializer.getUserDB());
61 return (UserDAO)sqlClient.queryForObject("getUser", userSeq); //$NON-NLS-1$
64 /**
65 * 모든 유효한 유저 목록을 가져옵니다.
67 * @param strApproval
68 * @param strUserConfirm
69 * @param strDel
70 * @return
71 * @throws TadpoleSQLManagerException, SQLException
73 public static List<UserDAO> getAllUser(String strApproval, String strUserConfirm, String strDel) throws TadpoleSQLManagerException, SQLException {
74 Map<String, String> mapSearch = new HashMap<String, String>();
75 mapSearch.put("Approval", strApproval);
76 mapSearch.put("UserConfirm", strUserConfirm);
77 mapSearch.put("Del", strDel);
79 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleSystemInitializer.getUserDB());
80 return sqlClient.queryForList("getAllUserSearch", mapSearch); //$NON-NLS-1$
83 /**
84 * 모든 유효한 유저 목록을 가져옵니다.
86 * @return
87 * @throws TadpoleSQLManagerException, SQLException
89 public static List<UserDAO> getAllUser() throws TadpoleSQLManagerException, SQLException {
90 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleSystemInitializer.getUserDB());
91 return sqlClient.queryForList("getAllUser"); //$NON-NLS-1$
94 /**
95 * 모든 유효한 유저 목록을 가져옵니다.
97 * @param delyn
98 * @return
99 * @throws TadpoleSQLManagerException, SQLException
101 public static List<UserDAO> getLiveAllUser() throws TadpoleSQLManagerException, SQLException {
102 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleSystemInitializer.getUserDB());
103 return sqlClient.queryForList("getLiveAllUser"); //$NON-NLS-1$
107 * add ldap user
109 * @param userName
110 * @param email
111 * @param external_id
112 * @param useOTP
114 * @return
115 * @throws TadpoleSQLManagerException
116 * @throws SQLException
118 public static UserDAO newLDAPUser(String userName, String email, String external_id, String useOPT) throws TadpoleSQLManagerException, SQLException, Exception {
119 return newUser(PublicTadpoleDefine.INPUT_TYPE.NORMAL.toString(), email, "LDAP", "YES", "TadpoleLDAPLogin", PublicTadpoleDefine.USER_ROLE_TYPE.USER.toString(),
120 userName, "KO", "Asia/Seoul", "YES", useOPT, "", "*", external_id, new Timestamp(System.currentTimeMillis()));
124 * 신규 유저를 등록합니다.
126 * @param inputType
127 * @param email
128 * @param email_key
129 * @param is_email_certification
130 * @param passwd
131 * @param roleType
132 * @param name
133 * @param language
134 * @param timezone
135 * @param approvalYn
136 * @param use_otp
137 * @param otp_secret
138 * @param strAllowIP
139 * @param strIsRegistDb
140 * @param strIsSharedDb
141 * @param intLimitAddDBCnt
142 * @param serviceStart
143 * @param serviceEnd
144 * @param external_id
145 * @param timeChangedPasswdTime
146 * @return
147 * @throws TadpoleSQLManagerException, SQLException
149 public static UserDAO newUser(String inputType, String email, String email_key, String is_email_certification, String passwd,
150 String roleType, String name, String language, String timezone, String approvalYn, String use_otp, String otp_secret,
151 String strAllowIP, String external_id, Timestamp timeChangedPasswdTime
152 ) throws TadpoleSQLManagerException, SQLException, Exception {
153 UserDAO loginDAO = new UserDAO();
154 loginDAO.setInput_type(inputType);
155 loginDAO.setEmail(email);
156 loginDAO.setEmail_key(email_key);
157 loginDAO.setIs_email_certification(is_email_certification);
159 loginDAO.setPasswd(SHA256Utils.getSHA256(passwd));
160 loginDAO.setChanged_passwd_time(timeChangedPasswdTime);//new Timestamp(System.currentTimeMillis()));
161 loginDAO.setRole_type(roleType);
163 loginDAO.setName(name);
164 loginDAO.setLanguage(language);
165 loginDAO.setTimezone(timezone);
166 loginDAO.setApproval_yn(approvalYn);
168 loginDAO.setUse_otp(use_otp);
169 loginDAO.setOtp_secret(otp_secret);
170 loginDAO.setAllow_ip(strAllowIP);
172 loginDAO.setIs_regist_db(GetAdminPreference.getIsAddDB());
173 loginDAO.setIs_shared_db(GetAdminPreference.getIsSharedDB());
174 loginDAO.setLimit_add_db_cnt(NumberUtils.toInt(GetAdminPreference.getDefaultAddDBCnt()));
175 loginDAO.setIs_modify_perference(GetAdminPreference.getIsPreferenceModify());
176 loginDAO.setService_start(new Timestamp(System.currentTimeMillis()));
177 loginDAO.setService_end(new Timestamp(DateUtil.afterMonthToMillis(NumberUtils.toInt(GetAdminPreference.getServiceDurationDay()))));
178 loginDAO.setExternal_id(external_id);
180 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleSystemInitializer.getUserDB());
181 List isUser = sqlClient.queryForList("isUser", email); //$NON-NLS-1$
183 if(isUser.isEmpty()) {
184 UserDAO userdb = (UserDAO)sqlClient.insert("newUser", loginDAO); //$NON-NLS-1$
185 TadpoleSystem_UserInfoData.initializeUserPreferenceData(userdb);
187 return userdb;
188 } else {
189 throw new TadpoleRuntimeException(Messages.get().TadpoleSystem_UserQuery_3);
194 * 이메일이 중복된 사용자가 있는지 검사합니다.
196 * @param email
197 * @return
198 * @throws TadpoleSQLManagerException, SQLException
200 public static boolean isDuplication(String email) throws TadpoleSQLManagerException, SQLException {
202 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleSystemInitializer.getUserDB());
203 List isUser = sqlClient.queryForList("isUser", email); //$NON-NLS-1$
205 if(isUser.size() == 0) {
206 return true;
209 return false;
213 * search like email
214 * @param email
215 * @return
216 * @throws TadpoleSQLManagerException, SQLException
218 public static List<UserDAO> findLikeUser(String email) throws TadpoleSQLManagerException, SQLException {
219 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleSystemInitializer.getUserDB());
220 List<UserDAO> listUser = sqlClient.queryForList("findLikeUser", "%" + email + "%"); //$NON-NLS-1$
222 return listUser;
226 * 유저를 넘겨 받는다.
227 * @param email
228 * @return
229 * @throws TadpoleSQLManagerException
230 * @throws SQLException
232 public static List<UserDAO> findExistUser(String email) throws TadpoleSQLManagerException, SQLException {
233 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleSystemInitializer.getUserDB());
234 List<UserDAO> listUser = sqlClient.queryForList("findEmailUser", email); //$NON-NLS-1$
235 return listUser;
239 * 연속 몇번 로그인을 실패 했을 경우
241 * @param intLastLoginCnt
242 * @param intUserSeq
243 * @param email
244 * @throws TadpoleSQLManagerException
245 * @throws SQLException
247 public static void failLoginCheck(int intLastLoginCnt, int intUserSeq, String email) throws TadpoleSQLManagerException, SQLException {
248 Map<String, Object> queryMap = new HashMap<String, Object>();
249 queryMap.put("intUserSeq", intUserSeq);
250 queryMap.put("intLastLoginCnt", intLastLoginCnt);
252 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleSystemInitializer.getUserDB());
253 List<UserLoginHistoryDAO> listUser = sqlClient.queryForList("lastLoginCntHistory", queryMap); //$NON-NLS-1$
255 int intFailCnt = 0;
256 for (UserLoginHistoryDAO userLoginHistoryDAO : listUser) {
257 if(PublicTadpoleDefine.YES_NO.NO.name().equals(userLoginHistoryDAO.getSucces_yn())) {
258 intFailCnt++;
262 // 연속 intLastLoingCnt 틀리면 계정을 잠근다.
263 if(intFailCnt == intLastLoginCnt) {
264 final UserDAO userDAO = new UserDAO();
265 userDAO.setSeq(intUserSeq);
267 if(logger.isInfoEnabled()) logger.info(String.format("##### User account %s is lock", email));
268 updateUserApproval(userDAO, PublicTadpoleDefine.YES_NO.NO.name());
273 * 유저를 넘겨 받는다.
274 * @param email
275 * @return
276 * @throws TadpoleSQLManagerException
277 * @throws SQLException
279 public static List<UserDAO> findExistExternalUser(String external_id) throws TadpoleSQLManagerException, SQLException {
280 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleSystemInitializer.getUserDB());
281 List<UserDAO> listUser = sqlClient.queryForList("findExternalUser", external_id); //$NON-NLS-1$
282 return listUser;
286 * 사용자 정보를 찾습니다.
288 * @param email
289 * @return
290 * @throws TadpoleSQLManagerException, SQLException
292 public static UserDAO findUser(String email) throws TadpoleSQLManagerException, SQLException {
293 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleSystemInitializer.getUserDB());
294 List<UserDAO> listUser = new ArrayList<UserDAO>();
295 if(ApplicationArgumentUtils.isOnlineServer()) {
296 listUser = sqlClient.queryForList("findEmailUser", email); //$NON-NLS-1$
297 } else {
298 listUser = sqlClient.queryForList("findLikeUser", "%" + email + "%"); //$NON-NLS-1$
301 if(listUser.size() == 0) {
302 throw new TadpoleRuntimeException(Messages.get().TadpoleSystem_UserQuery_0);
305 return listUser.get(0);
308 public static List<UserDAO> findUserList(String email) throws TadpoleSQLManagerException, SQLException {
309 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleSystemInitializer.getUserDB());
310 List<UserDAO> listUser = new ArrayList<UserDAO>();
312 listUser = sqlClient.queryForList("findLikeUser", "%" + email + "%"); //$NON-NLS-1$
314 if(listUser.size() == 0) {
315 throw new TadpoleRuntimeException(Messages.get().TadpoleSystem_UserQuery_0);
318 return listUser;
322 * 로그인시 email, passwd 확인
324 * @param email
325 * @param passwd
326 * @throws TadpoleSQLManagerException, SQLException
328 public static UserDAO login(String email, String passwd) throws TadpoleAuthorityException, TadpoleSQLManagerException, SQLException, Exception {
329 UserDAO login = new UserDAO();
330 login.setEmail(email);
331 login.setPasswd(SHA256Utils.getSHA256(passwd));
333 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleSystemInitializer.getUserDB());
334 UserDAO userInfo = (UserDAO)sqlClient.queryForObject("login", login); //$NON-NLS-1$
336 if(null == userInfo) {
337 throw new TadpoleAuthorityException(Messages.get().TadpoleSystem_UserQuery_5);
338 // } else {
339 // if(!passwd.equals(CipherManager.getInstance().decryption(userInfo.getPasswd()))) {
340 // throw new TadpoleAuthorityException(Messages.get().TadpoleSystem_UserQuery_5);
341 // }
344 return userInfo;
348 * update email confirm
350 * @param email
351 * @throws TadpoleSQLManagerException, SQLException
353 public static void updateEmailConfirm(String email) throws TadpoleSQLManagerException, SQLException {
354 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleSystemInitializer.getUserDB());
355 sqlClient.update("updateEmailConfirm", email);
359 * save login history
361 * @param userSeq
363 public static void saveLoginHistory(int userSeq, String strIP) {
364 saveLoginHistory(userSeq, strIP, "YES", "");
368 * save login history
370 * @param userSeq
371 * @param strYesNo
372 * @param strReason
374 public static void saveLoginHistory(int userSeq, String strIP, String strYesNo, String strReason) {
376 if(LicenseValidator.getLicense().isValidate()) {
377 try {
378 UserLoginHistoryDAO historyDao = new UserLoginHistoryDAO();
379 historyDao.setLogin_ip(strIP);
380 historyDao.setUser_seq(userSeq);
381 historyDao.setSucces_yn(strYesNo);
382 historyDao.setFail_reason(strReason);
384 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleSystemInitializer.getUserDB());
385 sqlClient.insert("saveLoginHistory", historyDao);
386 } catch(Exception e) {
387 logger.error("save login history", e);
393 * get login history
395 * @param strEmail
396 * @param strYesNo
397 * @param startTime
398 * @param endTime
400 public static List<UserLoginHistoryDAO> getLoginHistory(String strEmail, String strYesNo, long startTime, long endTime) throws TadpoleSQLManagerException, SQLException {
401 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleSystemInitializer.getUserDB());
403 Map<String, Object> queryMap = new HashMap<String, Object>();
404 queryMap.put("email", strEmail);
405 if(!"All".equals(strYesNo)) queryMap.put("succes_yn", strYesNo);
407 if(ApplicationArgumentUtils.isDBServer()) {
408 Date dateSt = new Date(startTime);
409 DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
410 queryMap.put("startTime", formatter.format(dateSt));
412 Date dateEd = new Date(endTime);
413 queryMap.put("endTime", formatter.format(dateEd));
414 } else {
415 Date dateSt = new Date(startTime);
416 DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
418 queryMap.put("startTime", formatter.format(dateSt));
420 Date dateEd = new Date(endTime);
421 queryMap.put("endTime", formatter.format(dateEd));
424 return (List<UserLoginHistoryDAO>)sqlClient.queryForList("getLoginHistory", queryMap);
428 * get admin
430 * @return
431 * @throws TadpoleSQLManagerException, SQLException
433 public static UserDAO getSystemAdmin() throws TadpoleSQLManagerException, SQLException {
434 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleSystemInitializer.getUserDB());
435 return (UserDAO)sqlClient.queryForObject("getSystemAdmin"); //$NON-NLS-1$
439 * group의 manager 정보를 리턴합니다.
441 * @param groupSeq
442 * @return
443 * @throws TadpoleSQLManagerException, SQLException
445 public static UserDAO getGroupManager(int groupSeq) throws TadpoleSQLManagerException, SQLException {
446 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleSystemInitializer.getUserDB());
447 return (UserDAO)sqlClient.queryForObject("groupManager", groupSeq); //$NON-NLS-1$
451 * admin user가 한명이라면 로그인 화면에서 기본 유저로 설정하기 위해...
453 * @return UserDAO
454 * @throws TadpoleSQLManagerException, SQLException
456 public static UserDAO loginUserCount() throws TadpoleSQLManagerException, SQLException {
457 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleSystemInitializer.getUserDB());
458 Integer isUser = (Integer)sqlClient.queryForObject("loginUserCount"); //$NON-NLS-1$
460 if(isUser == 1) {
461 UserDAO userInfo = (UserDAO)sqlClient.queryForObject("onlyOnUser"); //$NON-NLS-1$
462 return userInfo;
465 return null;
469 * 개인 사용자가 그룹 사용자로 수정
471 * @param user
472 * @throws TadpoleSQLManagerException, SQLException
474 public static void updateUserPersonToGroup(UserDAO user) throws TadpoleSQLManagerException, SQLException, Exception {
475 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleSystemInitializer.getUserDB());
476 user.setPasswd(SHA256Utils.getSHA256(user.getPasswd()));
477 user.setChanged_passwd_time(new Timestamp(System.currentTimeMillis()));
478 sqlClient.update("updateUserPersonToGroup", user); //$NON-NLS-1$
482 * 유저 데이터를 수정
483 * @param user
484 * @throws TadpoleSQLManagerException, SQLException
486 public static void updateUserData(UserDAO user) throws TadpoleSQLManagerException, SQLException {
487 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleSystemInitializer.getUserDB());
488 sqlClient.update("updateUserPermission", user); //$NON-NLS-1$
492 * 유저의 name, password 를 수정한다.
493 * @param user
494 * @throws TadpoleSQLManagerException, SQLException
496 public static void updateUserNameEmail(UserDAO user) throws TadpoleSQLManagerException, SQLException, Exception {
497 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleSystemInitializer.getUserDB());
498 sqlClient.update("updateUserNameEmail", user); //$NON-NLS-1$
502 * 유저의 기본정보를 수정
503 * @param user
504 * @throws TadpoleSQLManagerException, SQLException
506 public static void updateUserBasic(UserDAO user) throws TadpoleSQLManagerException, SQLException, Exception {
507 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleSystemInitializer.getUserDB());
508 sqlClient.update("updateUserBasic", user); //$NON-NLS-1$
512 * 시스템 어드민이 유저의 패스워드 변경
513 * @param user
514 * @throws TadpoleSQLManagerException, SQLException
516 public static void updateSystemAdminUserPassword(UserDAO user) throws TadpoleSQLManagerException, SQLException, Exception {
517 user.setPasswd(SHA256Utils.getSHA256(user.getPasswd()));
519 // 10년전으로 패스워드 수정하였다고 기록한다.
520 // 그러면 강제로 패스워드 바꾸었다고 뜰거니까? ㅠㅠ --;;
521 user.setChanged_passwd_time(new Timestamp(DateUtil.beforeMonthToMillsMonth(12 * 10)));
523 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleSystemInitializer.getUserDB());
524 sqlClient.update("updateUserPassword", user); //$NON-NLS-1$
528 * 유저의 패스워드 변경
529 * @param user
530 * @throws TadpoleSQLManagerException, SQLException
532 public static void updateUserPassword(UserDAO user) throws TadpoleSQLManagerException, SQLException, Exception {
533 user.setPasswd(SHA256Utils.getSHA256(user.getPasswd()));
534 user.setChanged_passwd_time(new Timestamp(System.currentTimeMillis()));
536 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleSystemInitializer.getUserDB());
537 sqlClient.update("updateUserPassword", user); //$NON-NLS-1$
541 * 유저의 패스워드 번경
542 * @param user
543 * @throws TadpoleSQLManagerException, SQLException
545 public static void updateUserPasswordWithID(UserDAO user) throws TadpoleSQLManagerException, SQLException, Exception {
546 user.setPasswd(SHA256Utils.getSHA256(user.getPasswd()));
547 user.setChanged_passwd_time(new Timestamp(System.currentTimeMillis()));
549 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleSystemInitializer.getUserDB());
550 sqlClient.update("updateUserPasswordWithID", user); //$NON-NLS-1$
554 * 사용자 힌트 변경
556 * @param user
557 * @throws TadpoleSQLManagerException, SQLException
559 public static void updateUserOTPCode(UserDAO user) throws TadpoleSQLManagerException, SQLException {
560 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleSystemInitializer.getUserDB());
561 sqlClient.update("updateUserOTPCode", user); //$NON-NLS-1$
565 * 사용자 정보.
567 * @param userSeq
568 * @return
569 * @throws TadpoleSQLManagerException, SQLException
571 public static UserDAO getUserInfo(int userSeq) throws TadpoleSQLManagerException, SQLException {
572 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleSystemInitializer.getUserDB());
573 return (UserDAO)sqlClient.queryForObject("getUserInfo", userSeq); //$NON-NLS-1$
577 * 사용자 정보 값을 수정합니다.
578 * @param userDAO
579 * @param name
581 public static void updateUserApproval(UserDAO userDAO, String yesNo) throws TadpoleSQLManagerException, SQLException {
582 UserDAO tmpUser = new UserDAO();
583 tmpUser.setSeq(userDAO.getSeq());
584 tmpUser.setApproval_yn(yesNo);
586 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleSystemInitializer.getUserDB());
587 sqlClient.update("updateUserApproval", tmpUser); //$NON-NLS-1$