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
.commons
.util
.Utils
;
35 import com
.hangum
.tadpole
.engine
.Messages
;
36 import com
.hangum
.tadpole
.engine
.initialize
.TadpoleEngineUserDB
;
37 import com
.hangum
.tadpole
.engine
.manager
.TadpoleSQLManager
;
38 import com
.hangum
.tadpole
.engine
.query
.dao
.system
.UserDAO
;
39 import com
.hangum
.tadpole
.engine
.query
.dao
.system
.UserLoginHistoryDAO
;
40 import com
.hangum
.tadpole
.preference
.define
.GetAdminPreference
;
41 import com
.ibatis
.sqlmap
.client
.SqlMapClient
;
50 public class TadpoleSystem_UserQuery
{
51 private static final Logger logger
= Logger
.getLogger(TadpoleSystem_UserQuery
.class);
57 * @throws TadpoleSQLManagerException
58 * @throws SQLException
60 public static UserDAO
getUser(int userSeq
)throws TadpoleSQLManagerException
, SQLException
{
61 SqlMapClient sqlClient
= TadpoleSQLManager
.getInstance(TadpoleEngineUserDB
.getUserDB());
62 return (UserDAO
)sqlClient
.queryForObject("getUser", userSeq
); //$NON-NLS-1$
66 * 모든 유효한 유저 목록을 가져옵니다.
69 * @param strUserConfirm
72 * @throws TadpoleSQLManagerException, SQLException
74 public static List
<UserDAO
> getAllUser(String strApproval
, String strUserConfirm
, String strDel
) throws TadpoleSQLManagerException
, SQLException
{
75 Map
<String
, String
> mapSearch
= new HashMap
<String
, String
>();
76 mapSearch
.put("Approval", strApproval
);
77 mapSearch
.put("UserConfirm", strUserConfirm
);
78 mapSearch
.put("Del", strDel
);
80 SqlMapClient sqlClient
= TadpoleSQLManager
.getInstance(TadpoleEngineUserDB
.getUserDB());
81 return sqlClient
.queryForList("getAllUserSearch", mapSearch
); //$NON-NLS-1$
85 * 모든 유효한 유저 목록을 가져옵니다.
88 * @throws TadpoleSQLManagerException, SQLException
90 public static List
<UserDAO
> getAllUser() throws TadpoleSQLManagerException
, SQLException
{
91 SqlMapClient sqlClient
= TadpoleSQLManager
.getInstance(TadpoleEngineUserDB
.getUserDB());
92 return sqlClient
.queryForList("getAllUser"); //$NON-NLS-1$
99 * @throws TadpoleSQLManagerException, SQLException
101 public static int getAllUserCount() throws TadpoleSQLManagerException
, SQLException
{
102 SqlMapClient sqlClient
= TadpoleSQLManager
.getInstance(TadpoleEngineUserDB
.getUserDB());
103 Integer intUserCnt
= (Integer
)sqlClient
.queryForObject("getAllUserCount"); //$NON-NLS-1$
109 * 모든 유효한 유저 목록을 가져옵니다.
113 * @throws TadpoleSQLManagerException, SQLException
115 public static List
<UserDAO
> getLiveAllUser() throws TadpoleSQLManagerException
, SQLException
{
116 SqlMapClient sqlClient
= TadpoleSQLManager
.getInstance(TadpoleEngineUserDB
.getUserDB());
117 return sqlClient
.queryForList("getLiveAllUser"); //$NON-NLS-1$
129 * @throws TadpoleSQLManagerException
130 * @throws SQLException
132 public static UserDAO
newLDAPUser(String userName
, String email
, String external_id
, String useOPT
) throws TadpoleSQLManagerException
, SQLException
, Exception
{
133 return newUser(PublicTadpoleDefine
.INPUT_TYPE
.LDAP
.name(), email
, "LDAP", "YES", Utils
.getUniqueDigit(12), PublicTadpoleDefine
.USER_ROLE_TYPE
.USER
.name(),
134 userName
, "KO", "Asia/Seoul", "YES", useOPT
, "", "*", external_id
, new Timestamp(System
.currentTimeMillis()));
143 * @param is_email_certification
153 * @param strIsRegistDb
154 * @param strIsSharedDb
155 * @param intLimitAddDBCnt
156 * @param serviceStart
159 * @param timeChangedPasswdTime
161 * @throws TadpoleSQLManagerException, SQLException
163 public static UserDAO
newUser(String inputType
, String email
, String email_key
, String is_email_certification
, String passwd
,
164 String roleType
, String name
, String language
, String timezone
, String approvalYn
, String use_otp
, String otp_secret
,
165 String strAllowIP
, String external_id
, Timestamp timeChangedPasswdTime
166 ) throws TadpoleSQLManagerException
, SQLException
, Exception
{
167 UserDAO loginDAO
= new UserDAO();
168 loginDAO
.setInput_type(inputType
);
169 loginDAO
.setEmail(email
);
170 loginDAO
.setEmail_key(email_key
);
171 loginDAO
.setIs_email_certification(is_email_certification
);
173 loginDAO
.setPasswd(SHA256Utils
.getSHA256(passwd
));
174 loginDAO
.setChanged_passwd_time(timeChangedPasswdTime
);//new Timestamp(System.currentTimeMillis()));
175 loginDAO
.setRole_type(roleType
);
177 loginDAO
.setName(name
);
178 loginDAO
.setLanguage(language
);
179 loginDAO
.setTimezone(timezone
);
180 loginDAO
.setApproval_yn(approvalYn
);
182 loginDAO
.setUse_otp(use_otp
);
183 loginDAO
.setOtp_secret(otp_secret
);
184 loginDAO
.setAllow_ip(strAllowIP
);
186 loginDAO
.setIs_regist_db(GetAdminPreference
.getIsAddDB());
187 loginDAO
.setIs_shared_db(GetAdminPreference
.getIsSharedDB());
188 loginDAO
.setLimit_add_db_cnt(NumberUtils
.toInt(GetAdminPreference
.getDefaultAddDBCnt()));
189 loginDAO
.setIs_modify_perference(GetAdminPreference
.getIsPreferenceModify());
190 loginDAO
.setService_start(new Timestamp(System
.currentTimeMillis()));
191 loginDAO
.setService_end(new Timestamp(DateUtil
.afterMonthToMillis(NumberUtils
.toInt(GetAdminPreference
.getServiceDurationDay()))));
192 loginDAO
.setExternal_id(external_id
);
194 SqlMapClient sqlClient
= TadpoleSQLManager
.getInstance(TadpoleEngineUserDB
.getUserDB());
195 List isUser
= sqlClient
.queryForList("isUser", email
); //$NON-NLS-1$
197 if(isUser
.isEmpty()) {
198 UserDAO userdb
= (UserDAO
)sqlClient
.insert("newUser", loginDAO
); //$NON-NLS-1$
199 TadpoleSystem_UserInfoData
.initializeUserPreferenceData(userdb
);
203 throw new TadpoleRuntimeException(Messages
.get().TadpoleSystem_UserQuery_3
);
208 * 이메일이 중복된 사용자가 있는지 검사합니다.
212 * @throws TadpoleSQLManagerException, SQLException
214 public static boolean isDuplication(String email
) throws TadpoleSQLManagerException
, SQLException
{
216 SqlMapClient sqlClient
= TadpoleSQLManager
.getInstance(TadpoleEngineUserDB
.getUserDB());
217 List isUser
= sqlClient
.queryForList("isUser", email
); //$NON-NLS-1$
219 if(isUser
.size() == 0) {
230 * @throws TadpoleSQLManagerException, SQLException
232 public static List
<UserDAO
> findLikeUser(String email
) throws TadpoleSQLManagerException
, SQLException
{
233 SqlMapClient sqlClient
= TadpoleSQLManager
.getInstance(TadpoleEngineUserDB
.getUserDB());
234 List
<UserDAO
> listUser
= sqlClient
.queryForList("findLikeUser", "%" + email
+ "%"); //$NON-NLS-1$
243 * @throws TadpoleSQLManagerException
244 * @throws SQLException
246 public static List
<UserDAO
> findExistUser(String email
) throws TadpoleSQLManagerException
, SQLException
{
247 SqlMapClient sqlClient
= TadpoleSQLManager
.getInstance(TadpoleEngineUserDB
.getUserDB());
248 List
<UserDAO
> listUser
= sqlClient
.queryForList("findEmailUser", email
); //$NON-NLS-1$
253 * 연속 몇번 로그인을 실패 했을 경우
255 * @param intLastLoginCnt
258 * @throws TadpoleSQLManagerException
259 * @throws SQLException
261 public static void failLoginCheck(int intLastLoginCnt
, int intUserSeq
, String email
) throws TadpoleSQLManagerException
, SQLException
{
262 Map
<String
, Object
> queryMap
= new HashMap
<String
, Object
>();
263 queryMap
.put("intUserSeq", intUserSeq
);
264 queryMap
.put("intLastLoginCnt", intLastLoginCnt
);
266 SqlMapClient sqlClient
= TadpoleSQLManager
.getInstance(TadpoleEngineUserDB
.getUserDB());
267 List
<UserLoginHistoryDAO
> listUser
= sqlClient
.queryForList("lastLoginCntHistory", queryMap
); //$NON-NLS-1$
270 for (UserLoginHistoryDAO userLoginHistoryDAO
: listUser
) {
271 if(PublicTadpoleDefine
.YES_NO
.NO
.name().equals(userLoginHistoryDAO
.getSucces_yn())) {
276 // 연속 intLastLoingCnt 틀리면 계정을 잠근다.
277 if(intFailCnt
== intLastLoginCnt
) {
278 final UserDAO userDAO
= new UserDAO();
279 userDAO
.setSeq(intUserSeq
);
281 if(logger
.isInfoEnabled()) logger
.info(String
.format("##### User account %s is lock", email
));
282 updateUserApproval(userDAO
, PublicTadpoleDefine
.YES_NO
.NO
.name());
290 * @throws TadpoleSQLManagerException
291 * @throws SQLException
293 public static List
<UserDAO
> findExistExternalUser(String external_id
) throws TadpoleSQLManagerException
, SQLException
{
294 SqlMapClient sqlClient
= TadpoleSQLManager
.getInstance(TadpoleEngineUserDB
.getUserDB());
295 List
<UserDAO
> listUser
= sqlClient
.queryForList("findExternalUser", external_id
); //$NON-NLS-1$
304 * @throws TadpoleSQLManagerException, SQLException
306 public static UserDAO
findUser(String email
) throws TadpoleSQLManagerException
, SQLException
{
307 SqlMapClient sqlClient
= TadpoleSQLManager
.getInstance(TadpoleEngineUserDB
.getUserDB());
308 List
<UserDAO
> listUser
= new ArrayList
<UserDAO
>();
309 // if(ApplicationArgumentUtils.isOnlineServer()) {
310 // listUser = sqlClient.queryForList("findEmailUser", email); //$NON-NLS-1$
312 listUser
= sqlClient
.queryForList("findLikeUser", "%" + email
+ "%"); //$NON-NLS-1$
315 if(listUser
.size() == 0) {
316 throw new TadpoleRuntimeException(Messages
.get().TadpoleSystem_UserQuery_0
);
319 return listUser
.get(0);
322 public static List
<UserDAO
> findUserList(String email
) throws TadpoleSQLManagerException
, SQLException
{
323 SqlMapClient sqlClient
= TadpoleSQLManager
.getInstance(TadpoleEngineUserDB
.getUserDB());
324 List
<UserDAO
> listUser
= new ArrayList
<UserDAO
>();
326 listUser
= sqlClient
.queryForList("findLikeUser", "%" + email
+ "%"); //$NON-NLS-1$
328 if(listUser
.size() == 0) {
329 throw new TadpoleRuntimeException(Messages
.get().TadpoleSystem_UserQuery_0
);
336 * 로그인시 email, passwd 확인
340 * @throws TadpoleSQLManagerException, SQLException
342 public static UserDAO
login(String email
, String passwd
) throws TadpoleAuthorityException
, TadpoleSQLManagerException
, SQLException
, Exception
{
343 UserDAO login
= new UserDAO();
344 login
.setEmail(email
);
345 login
.setPasswd(SHA256Utils
.getSHA256(passwd
));
347 SqlMapClient sqlClient
= TadpoleSQLManager
.getInstance(TadpoleEngineUserDB
.getUserDB());
348 UserDAO userInfo
= (UserDAO
)sqlClient
.queryForObject("login", login
); //$NON-NLS-1$
350 if(null == userInfo
) {
351 throw new TadpoleAuthorityException(Messages
.get().TadpoleSystem_UserQuery_5
);
353 // if(!passwd.equals(CipherManager.getInstance().decryption(userInfo.getPasswd()))) {
354 // throw new TadpoleAuthorityException(Messages.get().TadpoleSystem_UserQuery_5);
362 * update email confirm
365 * @throws TadpoleSQLManagerException, SQLException
367 public static void updateEmailConfirm(String email
) throws TadpoleSQLManagerException
, SQLException
{
368 SqlMapClient sqlClient
= TadpoleSQLManager
.getInstance(TadpoleEngineUserDB
.getUserDB());
369 sqlClient
.update("updateEmailConfirm", email
);
377 public static void saveLoginHistory(int userSeq
, String strIP
) {
378 saveLoginHistory(userSeq
, strIP
, "YES", "");
388 public static void saveLoginHistory(int userSeq
, String strIP
, String strYesNo
, String strReason
) {
390 if(LicenseValidator
.getLicense().isValidate()) {
392 UserLoginHistoryDAO historyDao
= new UserLoginHistoryDAO();
393 historyDao
.setLogin_ip(strIP
);
394 historyDao
.setUser_seq(userSeq
);
395 historyDao
.setSucces_yn(strYesNo
);
396 historyDao
.setFail_reason(strReason
);
398 SqlMapClient sqlClient
= TadpoleSQLManager
.getInstance(TadpoleEngineUserDB
.getUserDB());
399 sqlClient
.insert("saveLoginHistory", historyDao
);
400 } catch(Exception e
) {
401 logger
.error("save login history", e
);
414 public static List
<UserLoginHistoryDAO
> getLoginHistory(String strEmail
, String strYesNo
, long startTime
, long endTime
) throws TadpoleSQLManagerException
, SQLException
{
415 SqlMapClient sqlClient
= TadpoleSQLManager
.getInstance(TadpoleEngineUserDB
.getUserDB());
417 Map
<String
, Object
> queryMap
= new HashMap
<String
, Object
>();
418 queryMap
.put("email", strEmail
);
419 if(!"All".equals(strYesNo
)) queryMap
.put("succes_yn", strYesNo
);
421 if(ApplicationArgumentUtils
.isDBServer()) {
422 Date dateSt
= new Date(startTime
);
423 DateFormat formatter
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
424 queryMap
.put("startTime", formatter
.format(dateSt
));
426 Date dateEd
= new Date(endTime
);
427 queryMap
.put("endTime", formatter
.format(dateEd
));
429 Date dateSt
= new Date(startTime
);
430 DateFormat formatter
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
432 queryMap
.put("startTime", formatter
.format(dateSt
));
434 Date dateEd
= new Date(endTime
);
435 queryMap
.put("endTime", formatter
.format(dateEd
));
438 return (List
<UserLoginHistoryDAO
>)sqlClient
.queryForList("getLoginHistory", queryMap
);
445 * @throws TadpoleSQLManagerException, SQLException
447 public static UserDAO
getSystemAdmin() throws TadpoleSQLManagerException
, SQLException
{
448 SqlMapClient sqlClient
= TadpoleSQLManager
.getInstance(TadpoleEngineUserDB
.getUserDB());
449 return (UserDAO
)sqlClient
.queryForObject("getSystemAdmin"); //$NON-NLS-1$
453 * group의 manager 정보를 리턴합니다.
457 * @throws TadpoleSQLManagerException, SQLException
459 public static UserDAO
getGroupManager(int groupSeq
) throws TadpoleSQLManagerException
, SQLException
{
460 SqlMapClient sqlClient
= TadpoleSQLManager
.getInstance(TadpoleEngineUserDB
.getUserDB());
461 return (UserDAO
)sqlClient
.queryForObject("groupManager", groupSeq
); //$NON-NLS-1$
465 * admin user가 한명이라면 로그인 화면에서 기본 유저로 설정하기 위해...
468 * @throws TadpoleSQLManagerException, SQLException
470 public static UserDAO
loginUserCount() throws TadpoleSQLManagerException
, SQLException
{
471 SqlMapClient sqlClient
= TadpoleSQLManager
.getInstance(TadpoleEngineUserDB
.getUserDB());
472 Integer isUser
= (Integer
)sqlClient
.queryForObject("loginUserCount"); //$NON-NLS-1$
475 UserDAO userInfo
= (UserDAO
)sqlClient
.queryForObject("onlyOnUser"); //$NON-NLS-1$
486 * @throws TadpoleSQLManagerException, SQLException
488 public static void updateUserPersonToGroup(UserDAO user
) throws TadpoleSQLManagerException
, SQLException
, Exception
{
489 SqlMapClient sqlClient
= TadpoleSQLManager
.getInstance(TadpoleEngineUserDB
.getUserDB());
490 user
.setPasswd(SHA256Utils
.getSHA256(user
.getPasswd()));
491 user
.setChanged_passwd_time(new Timestamp(System
.currentTimeMillis()));
492 sqlClient
.update("updateUserPersonToGroup", user
); //$NON-NLS-1$
498 * @throws TadpoleSQLManagerException, SQLException
500 public static void updateUserData(UserDAO user
) throws TadpoleSQLManagerException
, SQLException
{
501 SqlMapClient sqlClient
= TadpoleSQLManager
.getInstance(TadpoleEngineUserDB
.getUserDB());
502 sqlClient
.update("updateUserPermission", user
); //$NON-NLS-1$
506 * 유저의 name, password 를 수정한다.
508 * @throws TadpoleSQLManagerException, SQLException
510 public static void updateUserNameEmail(UserDAO user
) throws TadpoleSQLManagerException
, SQLException
, Exception
{
511 SqlMapClient sqlClient
= TadpoleSQLManager
.getInstance(TadpoleEngineUserDB
.getUserDB());
512 sqlClient
.update("updateUserNameEmail", user
); //$NON-NLS-1$
518 * @throws TadpoleSQLManagerException, SQLException
520 public static void updateUserBasic(UserDAO user
) throws TadpoleSQLManagerException
, SQLException
, Exception
{
521 SqlMapClient sqlClient
= TadpoleSQLManager
.getInstance(TadpoleEngineUserDB
.getUserDB());
522 sqlClient
.update("updateUserBasic", user
); //$NON-NLS-1$
526 * 시스템 어드민이 유저의 패스워드 변경
528 * @throws TadpoleSQLManagerException, SQLException
530 public static void updateSystemAdminUserPassword(UserDAO user
) throws TadpoleSQLManagerException
, SQLException
, Exception
{
531 user
.setPasswd(SHA256Utils
.getSHA256(user
.getPasswd()));
533 // 10년전으로 패스워드 수정하였다고 기록한다.
534 // 그러면 강제로 패스워드 바꾸었다고 뜰거니까? ㅠㅠ --;;
535 user
.setChanged_passwd_time(new Timestamp(DateUtil
.beforeMonthToMillsMonth(12 * 10)));
537 SqlMapClient sqlClient
= TadpoleSQLManager
.getInstance(TadpoleEngineUserDB
.getUserDB());
538 sqlClient
.update("updateUserPassword", user
); //$NON-NLS-1$
544 * @throws TadpoleSQLManagerException, SQLException
546 public static void updateUserPassword(UserDAO user
) throws TadpoleSQLManagerException
, SQLException
, Exception
{
547 user
.setPasswd(SHA256Utils
.getSHA256(user
.getPasswd()));
548 user
.setChanged_passwd_time(new Timestamp(System
.currentTimeMillis()));
550 SqlMapClient sqlClient
= TadpoleSQLManager
.getInstance(TadpoleEngineUserDB
.getUserDB());
551 sqlClient
.update("updateUserPassword", user
); //$NON-NLS-1$
557 * @throws TadpoleSQLManagerException, SQLException
559 public static void updateUserPasswordWithID(UserDAO user
) throws TadpoleSQLManagerException
, SQLException
, Exception
{
560 user
.setPasswd(SHA256Utils
.getSHA256(user
.getPasswd()));
561 user
.setChanged_passwd_time(new Timestamp(System
.currentTimeMillis()));
563 SqlMapClient sqlClient
= TadpoleSQLManager
.getInstance(TadpoleEngineUserDB
.getUserDB());
564 sqlClient
.update("updateUserPasswordWithID", user
); //$NON-NLS-1$
571 * @throws TadpoleSQLManagerException, SQLException
573 public static void updateUserOTPCode(UserDAO user
) throws TadpoleSQLManagerException
, SQLException
{
574 SqlMapClient sqlClient
= TadpoleSQLManager
.getInstance(TadpoleEngineUserDB
.getUserDB());
575 sqlClient
.update("updateUserOTPCode", user
); //$NON-NLS-1$
583 * @throws TadpoleSQLManagerException, SQLException
585 public static UserDAO
getUserInfo(int userSeq
) throws TadpoleSQLManagerException
, SQLException
{
586 SqlMapClient sqlClient
= TadpoleSQLManager
.getInstance(TadpoleEngineUserDB
.getUserDB());
587 return (UserDAO
)sqlClient
.queryForObject("getUserInfo", userSeq
); //$NON-NLS-1$
595 public static void updateUserApproval(UserDAO userDAO
, String yesNo
) throws TadpoleSQLManagerException
, SQLException
{
596 UserDAO tmpUser
= new UserDAO();
597 tmpUser
.setSeq(userDAO
.getSeq());
598 tmpUser
.setApproval_yn(yesNo
);
600 SqlMapClient sqlClient
= TadpoleSQLManager
.getInstance(TadpoleEngineUserDB
.getUserDB());
601 sqlClient
.update("updateUserApproval", tmpUser
); //$NON-NLS-1$