DCL 4.0
Loading...
Searching...
No Matches
EShopSession Class Reference

#include <EShopSession.h>

Public Member Functions

 EShopSession (HttpServletContextEx &ctx, SQLConnection *pSQLConn) __DCL_THROWS1(SQLException *)
bool login (ListedStringToStringArrayMap &params)
void logout ()
void onViewSessions (ListedStringToStringArrayMap &params, Writer &out, const String &strMainCommand, int nMainCommandID, const String &strCommandID, const EShopServlet &_servlet)
const String & sessionID () const
int userID () const
int groupID () const
const String & userName () const
bool isAdmin () const
bool isGuest () const
int command () const
void setCommand (int nCM)
SQLConnectionSQLConn () const

Protected Attributes

String m_strSessionID
int m_nCommand
int m_nUserID
int m_nGroupID
String m_strUserName
SQLConnectionm_pSQLConn

Detailed Description

Definition at line 8 of file EShopSession.h.

Constructor & Destructor Documentation

◆ EShopSession()

__DCL_BEGIN_NAMESPACE EShopSession::EShopSession ( HttpServletContextEx & ctx,
SQLConnection * pSQLConn )

Definition at line 22 of file EShopSession.cpp.

26{
27 m_pSQLConn = pSQLConn;
28
30 ListedStringToStringArrayMap::Iterator it = ctx.__queryMap.find(COMMAND_STR);
31 if ((it != ctx.__queryMap.end()) && (!(*it).value.isEmpty())) {
32 try {
33 m_nCommand = Int32::parse((*it).value[0]);
34 }
35 catch(Exception* e) {
36 e->destroy();
37 }
38 }
39
40 m_nUserID = 2;
41 m_nGroupID = 2; //
42 m_strSessionID = ctx.__cookieMap[L"sid"];
43 if (m_strSessionID.isEmpty()) {
44 StringBuilder str = ctx.remoteAddr();
45 str += String::valueOf(ctx.remotePort());
46 str += String::valueOf(time(NULL));
48
49 __DCL_TRACE2(L"sessionId [%zd][%ls]\n",
50 m_strSessionID.length(), m_strSessionID.data());
51
52 HttpSetCookie cookie(
53 L"sid",
55 0,
56 Files::dirname(ctx.path())
57 );
58 ctx.addHeader(cookie);
59
60 SQLQuery q(pSQLConn);
61 q.prepare(L""
62 "INSERT INTO ES_SESSION(SESSION_ID, LAST_USE) "
63 " VALUES(:SESSION_ID, CURRENT_TIMESTAMP)"
64 );
65 q.params()[0].setValue(m_strSessionID);
66 q.execute();
67// __DCL_TRACE1(L"%d\n", q.affectedRows());
68 }
69 else {
70 // user info
71 SQLQuery q(pSQLConn);
72 q.execute(L""
73 "SELECT USER_ID "
74 " FROM ES_SESSION "
75 " WHERE SESSION_ID = \'" + m_strSessionID + L'\''
76 );
77 q.fetch();
78 if (!q.eof()) {
79 q.execute(L""
80 "UPDATE ES_SESSION "
81 " SET LAST_USE = CURRENT_TIMESTAMP "
82 " WHERE SESSION_ID = \'" + m_strSessionID + L'\''
83 );
84 }
85 else {
86// if (q.affectedRows() == 0)
87// {
88 // mysql에서 1초 미만에 update 했더니 affectedRows() 가 0인 경우가 있다.
89 // 세션 시간이 초과되어 삭제 되었다.
90 // guest 세션을 생성한다.
91 q.prepare(L""
92 "INSERT INTO ES_SESSION(SESSION_ID, USER_ID, LAST_USE) "
93 " VALUES(:SESSION_ID, :USER_ID, CURRENT_TIMESTAMP)"
94 );
95
96 int nUserID = 2;
97 q.params()[0].setValue(m_strSessionID);
98 q.params()[1].setValue(nUserID); // guest id
99 q.execute();
100 __DCL_TRACE1(L"%d\n", q.affectedRows());
101 }
102
103 q.execute(L""
104 "SELECT S.USER_ID, U.GROUP_ID, U.USER_NAME "
105 " FROM ES_SESSION S "
106 " INNER JOIN ES_USER U ON (S.USER_ID = U.USER_ID) "
107 " WHERE SESSION_ID = \'" + m_strSessionID + L'\''
108 );
109 q.fetch();
110 __DCL_ASSERT(!q.eof());
111
112 m_nUserID = q.fields().byName(L"USER_ID").asInteger();
113 m_nGroupID = q.fields().byName(L"GROUP_ID").asInteger();
114 m_strUserName = q.fields().byName(L"USER_NAME").asString();
115 }
116}
#define NULL
Definition Config.h:340
#define COMMAND_STR
@ CM_ABOUT
#define __DCL_TRACE1(fmt, arg1)
Definition Object.h:376
#define __DCL_ASSERT(expr)
Definition Object.h:371
#define __DCL_TRACE2(fmt, arg1, arg2)
Definition Object.h:377
String m_strSessionID
String m_strUserName
SQLConnection * m_pSQLConn
virtual void destroy()
Definition Exception.cpp:74
static String dirname(const String &_path)
Definition Files.cpp:269
static int32_t parse(const wchar_t *_number, unsigned _base=10) __DCL_THROWS1(NumericConvertException *)
Definition Numeric.cpp:284
String final()
Definition MD5.cpp:80

Member Function Documentation

◆ command()

int EShopSession::command ( ) const
inline

Definition at line 34 of file EShopSession.h.

34{ return m_nCommand; }

◆ groupID()

int EShopSession::groupID ( ) const
inline

Definition at line 30 of file EShopSession.h.

30{ return m_nGroupID; }

◆ isAdmin()

bool EShopSession::isAdmin ( ) const
inline

Definition at line 32 of file EShopSession.h.

32{ return m_nGroupID == 1; }

◆ isGuest()

bool EShopSession::isGuest ( ) const
inline

Definition at line 33 of file EShopSession.h.

33{ return m_nUserID == 2; }

◆ login()

bool EShopSession::login ( ListedStringToStringArrayMap & params)

Definition at line 118 of file EShopSession.cpp.

119{
121
122 String strLogin;
123 String strPassword = L"";
124
125 ListedStringToStringArrayMap::Iterator it = params.find(L"login");
126 if ((it != params.end()) && (!(*it).value.isEmpty()))
127 strLogin = (*it).value[0];
128
129 it = params.find(L"password");
130 if ((it != params.end()) && (!(*it).value.isEmpty()))
131 strPassword = (*it).value[0];
132
133 if (strLogin.isEmpty())
134 return false;
135
136 SQLQuery q(m_pSQLConn);
137 q.prepare(L""
138 "SELECT USER_ID FROM ES_USER "
139 " WHERE LOGIN_ID LIKE :LOGIN_ID AND PASSWORD LIKE :PASSWORD"
140 );
141 q.params()[0].setValue(strLogin);
142 q.params()[1].setValue(strPassword);
143
144 q.execute();
145 q.fetch();
146 if (q.eof()) {
147 __DCL_TRACE0(L"false\n");
148 return false;
149 }
150
151 int nUserID = 0;
152 nUserID = q.fields()[0].asInteger();
153
154 __DCL_TRACE1(L"UserID : %d\n", nUserID);
155 q.prepare(L""
156 "UPDATE ES_SESSION SET USER_ID = :USER_ID "
157 " WHERE SESSION_ID LIKE :SESSION_ID"
158 );
159
160 q.params()[0].setValue(nUserID);
161 q.params()[1].setValue(m_strSessionID);
162 q.execute();
163
164 q.execute(L""
165 "SELECT S.USER_ID, U.GROUP_ID, U.USER_NAME "
166 " FROM ES_SESSION S "
167 " INNER JOIN ES_USER U ON (S.USER_ID = U.USER_ID) "
168 " WHERE SESSION_ID = \'" + m_strSessionID + L'\''
169 );
170 q.fetch();
171 __DCL_ASSERT(!q.eof());
172
173 m_nUserID = q.fields().byName(L"USER_ID").asInteger();
174 m_nGroupID = q.fields().byName(L"GROUP_ID").asInteger();
175 m_strUserName = q.fields().byName(L"USER_NAME").asString();
176
177 return true;
178}
#define __DCL_TRACE0(psz)
Definition Object.h:375

◆ logout()

void EShopSession::logout ( )

Definition at line 180 of file EShopSession.cpp.

181{
182 SQLQuery q(m_pSQLConn);
183 q.execute(L""
184 "DELETE FROM ES_SHOPPING_BASKET "
185 " WHERE SESSION_ID = \'" + m_strSessionID + L'\''
186 );
187
188 q.execute(L""
189 "UPDATE ES_SESSION SET USER_ID = 2 "
190 " WHERE SESSION_ID = \'" + m_strSessionID + L'\''
191 );
192
193 q.execute(L""
194 "SELECT S.USER_ID, U.GROUP_ID, U.USER_NAME "
195 " FROM ES_SESSION S "
196 " INNER JOIN ES_USER U ON (S.USER_ID = U.USER_ID) "
197 " WHERE SESSION_ID = \'" + m_strSessionID + L'\''
198 );
199 q.fetch();
200 __DCL_ASSERT(!q.eof());
201
202 m_nUserID = q.fields().byName(L"USER_ID").asInteger();
203 m_nGroupID = q.fields().byName(L"GROUP_ID").asInteger();
204 m_strUserName = q.fields().byName(L"USER_NAME").asString();
205}

◆ onViewSessions()

void EShopSession::onViewSessions ( ListedStringToStringArrayMap & params,
Writer & out,
const String & strMainCommand,
int nMainCommandID,
const String & strCommandID,
const EShopServlet & _servlet )

Definition at line 208 of file EShopSession.cpp.

216{
217 SQLQuery q(m_pSQLConn);
218
219 ListedStringToStringArrayMap::Iterator it = params.find(L"idle");
220 if (it != params.end())
221 {
222 int nMin = 0;
223 try
224 {
225 nMin = Int32::parse(((*it).value)[0]);
226 }
227 catch(Exception* e)
228 {
229 e->destroy();
230 }
231
232 q.prepare(L""
233 "DELETE FROM ES_SESSION "
234 "WHERE SESSION_ID != :SESSION_ID "
235 " AND (CURRENT_TIMESTAMP - LAST_USE) >= (:MIN * 60) "
236 );
237 q.params()[0].setValue(sessionID());
238 q.params()[1].setValue(nMin);
239 q.execute();
240 }
241
242 TextTemplate tpl(_servlet.readTemplate(L"view_session.html"));
243 String strHREF = String::format(L"?%ls=%d",
244 strMainCommand.data(), nMainCommandID
245 );
246
247 tpl.assign(L"ACTION_HREF", strHREF);
248
249 TextTemplate& row = tpl[L"ROW"];
250 q.execute(L""
251 "SELECT S.SESSION_ID, U.LOGIN_ID, U.USER_NAME,"
252 " S.LAST_USE, (CURRENT_TIMESTAMP - S.LAST_USE) / 60 AS IDLE_TIME\n"
253 "FROM ES_SESSION S\n"
254 " INNER JOIN ES_USER U ON (S.USER_ID = U.USER_ID)\n"
255 "ORDER BY S.LAST_USE"
256 );
257 q.fetch();
258 while(!q.eof())
259 {
260 row.assign(q.fields(), L"&nbsp;");
261 tpl.append(L"ROW", row);
262 q.fetch();
263 }
264
265 out << tpl;
266}
String readTemplate(const wchar_t *filename) const __DCL_THROWS1(IOException *)
const String & sessionID() const

◆ sessionID()

const String & EShopSession::sessionID ( ) const
inline

Definition at line 28 of file EShopSession.h.

28{ return m_strSessionID; }

◆ setCommand()

void EShopSession::setCommand ( int nCM)
inline

Definition at line 35 of file EShopSession.h.

35{ m_nCommand = nCM; }

◆ SQLConn()

SQLConnection * EShopSession::SQLConn ( ) const
inline

Definition at line 37 of file EShopSession.h.

37{ return m_pSQLConn; }

◆ userID()

int EShopSession::userID ( ) const
inline

Definition at line 29 of file EShopSession.h.

29{ return m_nUserID; }

◆ userName()

const String & EShopSession::userName ( ) const
inline

Definition at line 31 of file EShopSession.h.

31{ return m_strUserName; }

Member Data Documentation

◆ m_nCommand

int EShopSession::m_nCommand
protected

Definition at line 41 of file EShopSession.h.

◆ m_nGroupID

int EShopSession::m_nGroupID
protected

Definition at line 43 of file EShopSession.h.

◆ m_nUserID

int EShopSession::m_nUserID
protected

Definition at line 42 of file EShopSession.h.

◆ m_pSQLConn

SQLConnection* EShopSession::m_pSQLConn
protected

Definition at line 46 of file EShopSession.h.

◆ m_strSessionID

String EShopSession::m_strSessionID
protected

Definition at line 40 of file EShopSession.h.

◆ m_strUserName

String EShopSession::m_strUserName
protected

Definition at line 44 of file EShopSession.h.


The documentation for this class was generated from the following files: