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
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
;
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
;
49 public class TadpoleSystem_UserQuery
{
50 private static final Logger logger
= Logger
.getLogger(TadpoleSystem_UserQuery
.class);
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$
65 * 모든 유효한 유저 목록을 가져옵니다.
68 * @param strUserConfirm
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$
84 * 모든 유효한 유저 목록을 가져옵니다.
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$
95 * 모든 유효한 유저 목록을 가져옵니다.
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$
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()));
129 * @param is_email_certification
139 * @param strIsRegistDb
140 * @param strIsSharedDb
141 * @param intLimitAddDBCnt
142 * @param serviceStart
145 * @param timeChangedPasswdTime
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
);
189 throw new TadpoleRuntimeException(Messages
.get().TadpoleSystem_UserQuery_3
);
194 * 이메일이 중복된 사용자가 있는지 검사합니다.
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) {
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$
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$
239 * 연속 몇번 로그인을 실패 했을 경우
241 * @param intLastLoginCnt
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$
256 for (UserLoginHistoryDAO userLoginHistoryDAO
: listUser
) {
257 if(PublicTadpoleDefine
.YES_NO
.NO
.name().equals(userLoginHistoryDAO
.getSucces_yn())) {
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());
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$
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$
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
);
322 * 로그인시 email, 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
);
339 // if(!passwd.equals(CipherManager.getInstance().decryption(userInfo.getPasswd()))) {
340 // throw new TadpoleAuthorityException(Messages.get().TadpoleSystem_UserQuery_5);
348 * update email confirm
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
);
363 public static void saveLoginHistory(int userSeq
, String strIP
) {
364 saveLoginHistory(userSeq
, strIP
, "YES", "");
374 public static void saveLoginHistory(int userSeq
, String strIP
, String strYesNo
, String strReason
) {
376 if(LicenseValidator
.getLicense().isValidate()) {
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
);
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
));
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
);
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 정보를 리턴합니다.
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가 한명이라면 로그인 화면에서 기본 유저로 설정하기 위해...
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$
461 UserDAO userInfo
= (UserDAO
)sqlClient
.queryForObject("onlyOnUser"); //$NON-NLS-1$
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$
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 를 수정한다.
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$
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 * 시스템 어드민이 유저의 패스워드 변경
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$
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$
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$
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$
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$
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$