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
.session
.manager
;
13 import java
.sql
.Timestamp
;
14 import java
.text
.MessageFormat
;
15 import java
.util
.ArrayList
;
16 import java
.util
.Enumeration
;
17 import java
.util
.List
;
20 import javax
.servlet
.http
.HttpServletRequest
;
21 import javax
.servlet
.http
.HttpSession
;
22 import javax
.servlet
.http
.HttpSessionContext
;
24 import org
.apache
.commons
.lang
.StringUtils
;
25 import org
.apache
.log4j
.Logger
;
26 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
27 import org
.eclipse
.core
.runtime
.IStatus
;
28 import org
.eclipse
.core
.runtime
.Status
;
29 import org
.eclipse
.core
.runtime
.jobs
.Job
;
30 import org
.eclipse
.rap
.rwt
.RWT
;
31 import org
.eclipse
.rap
.rwt
.client
.service
.JavaScriptExecutor
;
32 import org
.eclipse
.ui
.IWorkbenchWindow
;
33 import org
.eclipse
.ui
.PlatformUI
;
35 import com
.hangum
.tadpole
.commons
.libs
.core
.define
.PublicTadpoleDefine
;
36 import com
.hangum
.tadpole
.engine
.TadpoleEngineActivator
;
37 import com
.hangum
.tadpole
.engine
.manager
.TadpoleSQLManager
;
38 import com
.hangum
.tadpole
.engine
.manager
.TadpoleSQLTransactionManager
;
39 import com
.hangum
.tadpole
.engine
.query
.dao
.ManagerListDTO
;
40 import com
.hangum
.tadpole
.engine
.query
.dao
.system
.UserDAO
;
41 import com
.hangum
.tadpole
.engine
.query
.dao
.system
.UserDBDAO
;
42 import com
.hangum
.tadpole
.engine
.query
.dao
.system
.UserInfoDataDAO
;
43 import com
.hangum
.tadpole
.engine
.query
.sql
.TadpoleSystem_UserInfoData
;
44 import com
.hangum
.tadpole
.engine
.utils
.HttpSessionCollectorUtil
;
47 * tadpole의 session manager입니다
52 public class SessionManager
{
54 * Logger for this class
56 private static final Logger logger
= Logger
.getLogger(SessionManager
.class);
58 /** login ip를 가져온 경로를 지정한다 */
59 public static enum LOGIN_IP_TYPE
{BROWSER_IP
, SERVLET_REQUEST
};
63 * MANAGER_SEQ는 그룹의 manager 권한 사용자의 seq 입니다. seq로 그룹의 db list를 얻기위해 미리 가져옵니다.
68 public static enum NAME
{
69 /** webrtc, request.getRemoteAddrress */
73 /* 자신의 유저 seq */ USER_SEQ
,
86 /* 대표적인 권한 타입 */ REPRESENT_ROLE_TYPE
,
89 USE_OTP
, OTP_SECRET_KEY
,
98 * UserManager Object list를 설정한다.
102 public static void initManagerDBList() {
103 HttpSession sStore
= RWT
.getRequest().getSession();
104 sStore
.setAttribute(NAME
.ALL_MANAGER_DB_LIST
.name(), new ArrayList
<ManagerListDTO
>());
108 * UserManager Object list를 설정한다.
112 public static void setManagerDBList(List
<ManagerListDTO
> listManagerDTO
) {
113 HttpSession sStore
= RWT
.getRequest().getSession();
114 sStore
.setAttribute(NAME
.ALL_MANAGER_DB_LIST
.name(), listManagerDTO
);
118 * UserManager object list를 가져온다.
122 public static List
<ManagerListDTO
> getManagerDBList() {
123 HttpSession sStore
= RWT
.getRequest().getSession();
124 Object listObj
= sStore
.getAttribute(NAME
.ALL_MANAGER_DB_LIST
.name());
125 if(listObj
== null) {
126 return new ArrayList
<ManagerListDTO
>();
128 return (List
<ManagerListDTO
>)listObj
;
137 public static boolean isLogin() {
138 if(getUserSeq() == 0) return false;
143 * Update session information.<br>
145 * Session uses the information in multiple places(preference, user info etc.).
146 * So when updating the information stored in the Session,
147 * you must update the information given session.
149 * @param key Session Attribute name
150 * @param value Object
152 public static void updateSessionAttribute(String key
, Object value
) {
153 HttpSession sStore
= RWT
.getRequest().getSession();
154 sStore
.setAttribute(key
, value
);
164 public static void addSession(UserDAO userDao
, String loginType
, String ip
) {
165 HttpSession sStore
= RWT
.getRequest().getSession();
167 sStore
.setAttribute(NAME
.LOGIN_IP_TYPE
.name(), loginType
);
168 sStore
.setAttribute(NAME
.LOGIN_IP
.name(), ip
);
169 sStore
.setAttribute(NAME
.REPRESENT_ROLE_TYPE
.name(), userDao
.getRole_type());
170 sStore
.setAttribute(NAME
.USER_SEQ
.name(), userDao
.getSeq());
171 sStore
.setAttribute(NAME
.LOGIN_EMAIL
.name(), userDao
.getEmail());
172 // sStore.setAttribute(NAME.LOGIN_PASSWORD.name(), userDao.getPasswd());
173 sStore
.setAttribute(NAME
.LOGIN_NAME
.name(), userDao
.getName());
174 sStore
.setAttribute(NAME
.IS_REGIST_DB
.name(), userDao
.getIs_regist_db());
175 sStore
.setAttribute(NAME
.LANGUAGE
.name(), userDao
.getLanguage());
176 sStore
.setAttribute(NAME
.TIMEZONE
.name(), userDao
.getTimezone());
178 sStore
.setAttribute(NAME
.IS_SHARED_DB
.name(), userDao
.getIs_shared_db());
179 sStore
.setAttribute(NAME
.IS_MODIFY_PERFERENCE
.name(), userDao
.getIs_modify_perference());
180 sStore
.setAttribute(NAME
.LIMIT_ADD_DB_CNT
.name(), userDao
.getLimit_add_db_cnt());
181 sStore
.setAttribute(NAME
.SERVICE_END
.name(), userDao
.getService_end());
183 sStore
.setAttribute(NAME
.PERSPECTIVE
.name(), "default");
185 sStore
.setAttribute(NAME
.USE_OTP
.name(), userDao
.getUse_otp());
186 sStore
.setAttribute(NAME
.OTP_SECRET_KEY
.name(), userDao
.getOtp_secret());
188 sStore
.setAttribute(NAME
.UNLOCK_DB_LIST
.name(), new ArrayList
<Integer
>());
194 * @param getLoginIpType
196 public static String
getLoginIpType() {
197 HttpSession sStore
= RWT
.getRequest().getSession();
198 return (String
)sStore
.getAttribute(NAME
.LOGIN_IP_TYPE
.name());
205 public static String
getLoginIp() {
206 HttpSession sStore
= RWT
.getRequest().getSession();
207 return (String
)sStore
.getAttribute(NAME
.LOGIN_IP
.name());
213 // * @param strPasswd
215 // public static void setPassword(String strPasswd) {
216 // HttpSession sStore = RWT.getRequest().getSession();
217 // sStore.setAttribute(NAME.LOGIN_PASSWORD.name(), strPasswd);
220 public static void setUesrSeq(int seq
) {
221 HttpSession sStore
= RWT
.getRequest().getSession();
222 sStore
.setAttribute(NAME
.USER_SEQ
.name(), seq
);
224 public static int getUserSeq() {
225 HttpSession sStore
= RWT
.getRequest().getSession();
226 Object obj
= sStore
.getAttribute(NAME
.USER_SEQ
.name());
228 if(obj
== null) return 0;
229 else return (Integer
)obj
;
232 public static String
getEMAIL() {
233 HttpSession sStore
= RWT
.getRequest().getSession();
235 return (String
)sStore
.getAttribute(NAME
.LOGIN_EMAIL
.name());
238 // public static String getPassword() {
239 // HttpSession sStore = RWT.getRequest().getSession();
240 // return (String)sStore.getAttribute(NAME.LOGIN_PASSWORD.name());
243 public static String
getName() {
244 HttpSession sStore
= RWT
.getRequest().getSession();
245 return (String
)sStore
.getAttribute(NAME
.LOGIN_NAME
.name());
247 public static String
getIsRegistDB() {
248 HttpSession sStore
= RWT
.getRequest().getSession();
249 return (String
)sStore
.getAttribute(NAME
.IS_REGIST_DB
.name());
252 public static String
getIsSharedDB() {
253 HttpSession sStore
= RWT
.getRequest().getSession();
254 return (String
)sStore
.getAttribute(NAME
.IS_SHARED_DB
.name());
257 public static Integer
getLimitAddDBCnt() {
258 HttpSession sStore
= RWT
.getRequest().getSession();
259 return (Integer
)sStore
.getAttribute(NAME
.LIMIT_ADD_DB_CNT
.name());
262 public static String
getIsModifyPerference() {
263 HttpSession sStore
= RWT
.getRequest().getSession();
264 return (String
)sStore
.getAttribute(NAME
.IS_MODIFY_PERFERENCE
.name());
267 public static Timestamp
getServiceEnd() {
268 HttpSession sStore
= RWT
.getRequest().getSession();
269 return (Timestamp
)sStore
.getAttribute(NAME
.SERVICE_END
.name());
272 public static String
getUseOTP() {
273 HttpSession sStore
= RWT
.getRequest().getSession();
274 return (String
)sStore
.getAttribute(NAME
.USE_OTP
.name());
276 public static String
getOTPSecretKey() {
277 HttpSession sStore
= RWT
.getRequest().getSession();
278 return (String
)sStore
.getAttribute(NAME
.OTP_SECRET_KEY
.name());
280 public static String
getLangeage() {
281 HttpSession sStore
= RWT
.getRequest().getSession();
282 return (String
)sStore
.getAttribute(NAME
.LANGUAGE
.name());
284 public static String
getTimezone() {
285 HttpSession sStore
= RWT
.getRequest().getSession();
286 return (String
)sStore
.getAttribute(NAME
.TIMEZONE
.name());
294 * admin이면서 manager일수는 없습니다.
300 * group당 manager권한은 반듯이 하나입니다.
301 * manager권한이 정지되면 그룹을 수정 못하는 것으로.
306 public static String
getRepresentRole() {
307 HttpSession sStore
= RWT
.getRequest().getSession();
308 return (String
)sStore
.getAttribute(NAME
.REPRESENT_ROLE_TYPE
.name());
311 public static boolean isSystemAdmin() {
312 return PublicTadpoleDefine
.DB_USER_ROLE_TYPE
.SYSTEM_ADMIN
.name().equals(getRepresentRole()) ?
true : false;
316 * 초기 접속시 사용자의 모든 프리퍼런스 데이터를 설정합니다.
318 public static void setUserAllPreferenceData(Map
<String
, Object
> mapUserInfo
) {
319 HttpSession sStore
= RWT
.getRequest().getSession();
320 sStore
.setAttribute(NAME
.USER_INFO_DATA
.name(), mapUserInfo
);
328 public static void setUserInfo(String key
, String obj
) {
330 HttpSession sStore
= RWT
.getRequest().getSession();
331 Map
<String
, Object
> mapUserInfoData
= (Map
<String
, Object
>)sStore
.getAttribute(NAME
.USER_INFO_DATA
.name());
332 UserInfoDataDAO userInfoDataDAO
= (UserInfoDataDAO
)mapUserInfoData
.get(key
);
333 if(userInfoDataDAO
== null) {
334 userInfoDataDAO
= new UserInfoDataDAO(SessionManager
.getUserSeq(), key
, obj
);
337 TadpoleSystem_UserInfoData
.insertUserInfoData(userInfoDataDAO
);
338 } catch(Exception e
) {
339 logger
.error("User data save exception [key]" + key
+ "[value]" + obj
, e
);
342 userInfoDataDAO
.setValue0(obj
);
345 mapUserInfoData
.put(key
, userInfoDataDAO
);
346 sStore
.setAttribute(NAME
.USER_INFO_DATA
.name(), mapUserInfoData
);
356 public static UserInfoDataDAO
getUserInfo(String key
, String value
) {
357 HttpSession sStore
= RWT
.getRequest().getSession();
358 Map
<String
, Object
> mapUserInfoData
= (Map
<String
, Object
>)sStore
.getAttribute(NAME
.USER_INFO_DATA
.name());
360 UserInfoDataDAO userData
= (UserInfoDataDAO
)mapUserInfoData
.get(key
);
361 if(userData
== null) {
362 userData
= new UserInfoDataDAO(SessionManager
.getUserSeq(), key
, value
);
364 TadpoleSystem_UserInfoData
.insertUserInfoData(userData
);
365 } catch(Exception e
) {
366 logger
.error("User data save exception [key]" + key
+ "[value]" + value
, e
);
369 mapUserInfoData
.put(key
, userData
);
380 public static boolean setUnlokDB(final UserDBDAO userDB
) {
381 HttpSession sStore
= RWT
.getRequest().getSession();
382 List
<UserDBDAO
> listUnlockDB
= (List
)sStore
.getAttribute(NAME
.UNLOCK_DB_LIST
.name());
384 return listUnlockDB
.add(userDB
);
392 public static boolean isUnlockDB(final UserDBDAO userDB
) {
393 HttpSession sStore
= RWT
.getRequest().getSession();
394 List
<UserDBDAO
> listUnlockDB
= (List
)sStore
.getAttribute(NAME
.UNLOCK_DB_LIST
.name());
396 return listUnlockDB
.contains(userDB
);
404 public static void removeUnlockDB(final UserDBDAO userDB
) {
405 HttpSession sStore
= RWT
.getRequest().getSession();
406 List
<UserDBDAO
> listUnlockDB
= (List
)sStore
.getAttribute(NAME
.UNLOCK_DB_LIST
.name());
408 listUnlockDB
.remove(userDB
);
414 public static void logout(final String strID
) {
415 HttpSessionCollectorUtil
.getInstance().sessionDestroyed(strID
);
417 HttpServletRequest request
= RWT
.getRequest();
419 HttpSession sStore
= request
.getSession();
420 // sStore.setAttribute(NAME.USER_SEQ.toString(), 0);
422 } catch(Throwable e
) {
427 // fixed https://github.com/hangum/TadpoleForDBTools/issues/708
428 // ps - 사용자 session id를 보여주고 싶지 않아서 배열을 이용해서 뺐어요. - hangum
429 String
[] arryRequestURL
= StringUtils
.split(request
.getRequestURL().toString(), ";");
430 String browserText
= MessageFormat
.format("parent.window.location.href = \"{0}\";", arryRequestURL
[0]);
431 JavaScriptExecutor executor
= RWT
.getClient().getService( JavaScriptExecutor
.class );
432 executor
.execute("setTimeout('"+browserText
+"', 100)" );
433 } catch(Exception e
) {
434 logger
.error("loguout", e
);
436 // removeConnection(strID);
445 public static void removeConnection(final String strID
) {
446 removeTransactionInstance(strID
);
447 removeNonTransactionInstance(strID
);
455 private static void removeTransactionInstance(final String strID
) {
456 Job job
= new Job("Remove transaction instance") { //$NON-NLS-1$
458 public IStatus
run(IProgressMonitor monitor
) {
461 TadpoleSQLTransactionManager
.executeAllRollback(strID
);
462 } catch(Exception e
) {
463 logger
.error("removeTransactionInstance connection instance", e
);
464 return new Status(Status
.WARNING
, TadpoleEngineActivator
.PLUGIN_ID
, e
.getMessage(), e
);
468 return Status
.OK_STATUS
;
473 job
.setName("RemoveTransaction instance");
483 private static void removeNonTransactionInstance(final String strID
) {
484 Job job
= new Job("Remove non transaction connection instance") { //$NON-NLS-1$
486 public IStatus
run(IProgressMonitor monitor
) {
489 TadpoleSQLManager
.removeAllInstance(strID
);
490 } catch(Exception e
) {
491 logger
.error("remove user connection instance", e
);
492 return new Status(Status
.WARNING
, TadpoleEngineActivator
.PLUGIN_ID
, e
.getMessage(), e
);
496 return Status
.OK_STATUS
;
501 job
.setName("Remove normal instance");
507 * 사용자 session을 invalidate시킵니다.
509 public static void invalidate() {
511 HttpSession sStore
= RWT
.getRequest().getSession();
513 HttpSessionContext hsc
= sStore
.getSessionContext();
514 Enumeration ids
= hsc
.getIds();
515 while(ids
.hasMoreElements()) {
516 String id
= (String
)ids
.nextElement();
518 if(logger
.isDebugEnabled()) logger
.debug("==========================> " + hsc
.getSession(id
));
521 } catch(Exception e
) {
522 logger
.error("user session invalidate", e
);
526 // public static String getPerspective() {
527 // UserInfoDataDAO userInfo = SessionManager.getUserInfo(NAME.PERSPECTIVE.name(), "default");
528 // return userInfo.getValue0();
531 // public static void setPerspective(String persp) {
534 // TadpoleSystem_UserInfoData.updateUserInfoData(NAME.PERSPECTIVE.name(), persp);
536 // SessionManager.setUserInfo(NAME.PERSPECTIVE.name(), persp);
537 // SessionManager.resetPerspective();
538 // } catch (Exception e) {
539 // logger.error("Error change perspective", e);
543 public static void resetPerspective() {
544 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
545 if (window
!= null) {
546 window
.getActivePage().resetPerspective();