-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession_helper.e
181 lines (140 loc) · 4.08 KB
/
session_helper.e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
note
description: "Summary description for {SESSION_HELPER}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
SESSION_HELPER
create
make
feature
-- Handlers
db: DATABASE_HELPER
json: JSON_HELPER
feature {NONE}
make(db_conn: DATABASE_HELPER)
do
db := db_conn
create json.default_create
end
feature
-- DB cleaner
check_db
local
removed_count: INTEGER
do
removed_count := db.modify ("DELETE FROM sessions WHERE update_time < datetime('now', 'localtime', '-2 hour')")
Io.put_string ("Cleaned " + removed_count.out + " sessions rows")
Io.new_line
end
extend_current(req: WSF_REQUEST; res: WSF_RESPONSE)
local
params: HASH_TABLE[STRING, STRING]
data: ARRAY[ARRAY[STRING]]
cookie: WSF_COOKIE
datet: DATE_TIME
dtd: DATE_TIME_DURATION
do
create params.make (1)
if attached req.cookie ("sess_id") as sess_id then
params["sess_id"] := sess_id.as_string.string_representation
data := db.select_all (db.query_escape ("SELECT * FROM sessions WHERE id = {{sess_id}}", params))
if data.count > 0 then
create datet.make_now
create dtd.make(0, 0, 0, 2, 0, 0)
datet.add (dtd)
create cookie.make ("sess_id", sess_id.as_string.string_representation)
cookie.set_path ("/")
cookie.set_expiration_date(datet)
db.just_modify (db.query_escape ("UPDATE sessions SET update_time = datetime('now', 'localtime') WHERE id = {{sess_id}}", params))
else
Io.put_string ("Removing current session")
Io.new_line
create datet.make_now
create dtd.make(-1, 0, 0, 0, 0, 0)
datet.add (dtd)
create cookie.make ("sess_id", "__remove__")
cookie.set_path ("/")
cookie.set_expiration_date(datet)
end
res.add_cookie (cookie)
end
end
feature
-- Public
start(name: STRING): STRING
local
rand: V_RANDOM
crypt: MD5
sid: STRING
do
create rand.default_create
create crypt.make
check_db
from
crypt.update_from_string (name + rand.long_item.out)
sid := crypt.digest_as_string
until
db.select_all ("SELECT * FROM sessions WHERE id = '" + sid + "'").count = 0
loop
crypt.update_from_string (name + rand.long_item.out)
sid := crypt.digest_as_string
end
db.just_insert ("INSERT INTO sessions (id, data, update_time) VALUES('" + sid + "', '" + name + "', datetime('now', 'localtime'))")
Result := sid
end
get(req: WSF_REQUEST; res: WSF_RESPONSE): detachable STRING
local
params: HASH_TABLE[STRING, STRING]
data: ARRAY[ARRAY[STRING]]
do
Result := Void
check_db
if attached req.cookie ("sess_id") as sess_id then
extend_current (req, res)
create params.make (1)
params["sess_id"] := sess_id.as_string.string_representation
data := db.select_all (db.query_escape ("SELECT data FROM sessions WHERE id = {{sess_id}}", params))
if data.count > 0 then
extend_current (req, res)
Result := data.at (1).at (1)
end
end
end
destroy(req: WSF_REQUEST; res: WSF_RESPONSE)
local
params: HASH_TABLE[STRING, STRING]
datet: DATE_TIME
dtd: DATE_TIME_DURATION
cookie: WSF_COOKIE
do
check_db
if attached req.cookie ("sess_id") as sess_id then
create params.make (1)
params["sess_id"] := sess_id.as_string.string_representation
db.just_modify (db.query_escape ("DELETE FROM sessions WHERE id = {{sess_id}}", params))
create datet.make_now
create dtd.make(0, 0, 0, -2, 0, 0)
datet.add (dtd)
create cookie.make ("sess_id", "")
cookie.set_path ("/")
cookie.set_expiration_date(datet)
end
end
is_logged_in(req: WSF_REQUEST; res: WSF_RESPONSE): BOOLEAN
local
params: HASH_TABLE[STRING, STRING]
do
Result := false
check_db
create params.make (1)
if attached req.cookie ("sess_id") as sess_id then
params["sess_id"] := sess_id.as_string.string_representation
Io.put_string (db.query_escape ("SELECT * FROM sessions WHERE id = {{sess_id}}", params))
if db.select_all (db.query_escape ("SELECT * FROM sessions WHERE id = {{sess_id}}", params)).count > 0 then
extend_current (req, res)
Result := true
end
end
end
end