-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
360 lines (319 loc) · 11.4 KB
/
server.py
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import os.path
import torndb
import tornado.escape
import tornado.httpserver
import tornado.ioloop
import tornado.options
import os
from binascii import hexlify
import tornado.web
import datetime
from tornado.options import define, options
define("port", default=6060, help="run on the given port", type=int)
define("mysql_host", default="127.0.0.1:3306", help="database host")
define("mysql_database", default="iutnet", help="database name")
define("mysql_user", default="root", help="database user")
define("mysql_password", default="", help="database password")
class Application(tornado.web.Application):
def __init__(self):
handlers = [
#GET METHOD :
(r"/signup", signup),
(r"/login", login),
(r"/logout", logout),
(r"/sendticket", sendticket),
(r"/getticketcli", getticketcli),
(r"/closeticket", closeticket),
(r"/getticketmod", getticketmod),
(r"/restoticketmod", restoticketmod),
(r"/changestatus", chagnestatus),
(r"/h", h), # testing
(r".*", defaulthandler),
]
settings = dict()
super(Application, self).__init__(handlers, **settings)
self.db = torndb.Connection(
host=options.mysql_host, database=options.mysql_database,
user=options.mysql_user, password=options.mysql_password)
class BaseHandler(tornado.web.RequestHandler):
@property
def db(self):
return self.application.db
def check_user(self,user):
resuser = self.db.get("SELECT * from users where username = %s",user)
if resuser:
return True
else :
return False
def check_api(self,api):
resuser = self.db.get("SELECT * from users where api = %s", api)
if resuser:
return True
else:
return False
def check_auth(self,username,password):
resuser = self.db.get("SELECT * from users where username = %s and password = %s", username,password)
if resuser:
return True
else:
return False
def is_admin(self,token):
res = int(self.db.get("select role from users where api=%s",token)['role'])
if res == 1:
return True
else:
return False
class defaulthandler(BaseHandler):
def get(self):
output = {'message':'Wrong Command'
,'code':'400'}
self.set_status(400)
self.write(output)
def post(self, *args, **kwargs):
output = {'status':'Wrong Command',
'code':'400'}
self.set_status(400)
self.write(output)
class signup(BaseHandler):
def get(self,*args):
username = str(self.get_argument('username'))
password = str(self.get_argument('password'))
fname = str(self.get_argument('firstname',''))
lanme = str(self.get_argument('lastname',''))
if not self.check_user(username):
api_token = str(hexlify(os.urandom(16)))
user_id = self.db.execute("INSERT INTO users (username, password, fname, lname ,api, role) "
"values (%s,%s,%s,%s,%s,0) "
, username,password,fname,lanme,api_token)
output = { 'message': 'Signed Up Successfully',
'code': '200' }
self.write(output)
else:
output = {'message': 'User Exist',
'code':'406'}
self.set_status(406)
self.write(output)
class login(BaseHandler):
def get(self):
username = str(self.get_argument('username'))
password = str(self.get_argument('password'))
if self.check_auth(username,password):
api_token = str(hexlify(os.urandom(16)))
self.db.execute("update users set api=%s where username=%s",api_token,username)
output = {
'message' : 'Logged in Successfully',
'code' : '200',
'token' : api_token
}
self.write(output)
else:
output = {
'message' : 'Invalid username or password',
'code' : '401'
}
self.set_status(401)
self.write(output)
class logout(BaseHandler):
def get(self):
username = str(self.get_argument('username'))
password = str(self.get_argument('password'))
if self.check_auth(username,password):
api_token = str(hexlify(os.urandom(16)))
self.db.execute("update users set api=%s where username=%s",api_token,username)
output = {
'message' : 'Logged out Successfully',
'code' : '200',
}
self.write(output)
else:
output = {
'message' : 'Invalid username or password',
'code' : '401'
}
self.set_status(401)
self.write(output)
# SendTIcket
class sendticket(BaseHandler):
def get(self):
token = str(self.get_argument('token'))
subject = str(self.get_argument('subject'))
body = str(self.get_argument('body'))
if self.check_api(token):
user_id = int(self.db.get("select ID from users where api=%s",token)['ID'])
currentDT = datetime.datetime.now()
ticket_id = self.db.execute("INSERT INTO tickets (title, body, userID, status, date) "
"values (%s,%s,%s,%s,%s) "
, subject,body,user_id,0,currentDT.strftime("%Y-%m-%d %H:%M:%S"))
output = {
'message':'Tikcet Snet Successfully',
'id':ticket_id,
'code':'200'
}
self.write(output)
else:
output = {
'message':'Invalid token',
'code':'401'
}
self.set_status(401)
self.write(output)
def getStatus(code):
if code == 0:
return 'Open'
elif code == 1:
return 'Wating'
else:
return 'Closed'
# Get Ticket CLI
class getticketcli(BaseHandler):
def get(self):
token = str(self.get_argument('token'))
if self.check_api(token):
user_id = int(self.db.get("select ID from users where api=%s",token)['ID'])
tickets = self.db.query("select * from tickets where userID=%s",user_id)
tickets_num = len(tickets)
output = {
'tickets':'There are -'+str(tickets_num)+'- Tickets',
'code' : '200',
}
i=0
for ticket in tickets:
out = {
'subject' : ticket.title,
'body' : ticket.body,
'status' : getStatus(ticket.status),
'id' : ticket.ID,
'date': ticket.date.strftime("%Y-%m-%d %H:%M:%S"),
}
output['block '+str(i)] = out
i+=1
self.write(output)
else:
output = {
'message':'Invalid token',
'code':'401'
}
self.set_status(401)
self.write(output)
class h(BaseHandler):
def get(self):
row = self.db.get("SELECT * from users where username = %s",'amir')
self.write({'u':row['username']})
# Close Ticket
class closeticket(BaseHandler):
def get(self):
token = str(self.get_argument('token'))
id = str(self.get_argument('id'))
if self.check_api(token):
self.db.execute("update tickets set status=2 where id=%s",id)
output = {
'message':'Ticket with id -'+id+'- Closed Successfully',
'code':'200',
}
self.write(output)
else:
output = {
'message':'Invalid token',
'code':'401'
}
self.set_status(401)
self.write(output)
class getticketmod(BaseHandler):
def get(self):
token = str(self.get_argument('token'))
if self.check_api(token):
if self.is_admin(token):
tickets = self.db.query("select * from tickets")
tickets_num = len(tickets)
output = {
'tickets':'There are -'+str(tickets_num)+'- Tickets',
'code' : '200',
}
i=0
for ticket in tickets:
out = {
'subject' : ticket.title,
'body' : ticket.body,
'status' : getStatus(ticket.status),
'id' : ticket.ID,
'date': ticket.date.strftime("%Y-%m-%d %H:%M:%S"),
}
output['block '+str(i)] = out
i+=1
self.write(output)
else:
output = {
'message':'Forbidden',
'code':'403'
}
self.set_status(403)
self.write(output)
else:
output = {
'message':'Invalid token',
'code':'401'
}
self.set_status(401)
self.write(output)
#restoticketmod
class restoticketmod(BaseHandler):
def get(self):
token = str(self.get_argument('token'))
id = int(self.get_argument('id'))
body = str(self.get_argument('body'))
if self.check_api(token):
if self.is_admin(token):
self.db.execute("update tickets set answare=%s where id=%s",body,id)
output = {
'message' : 'Response To Ticket -'+str(id)+'- Sent Successfully',
'code': '200'
}
self.write(output)
else:
output = {
'message':'Forbidden',
'code':'403'
}
self.set_status(403)
self.write(output)
else:
output = {
'message':'Invalid token',
'code':'401'
}
self.set_status(401)
self.write(output)
class chagnestatus(BaseHandler):
def get(self):
token = str(self.get_argument('token'))
id = str(self.get_argument('id'))
status = str(int(self.get_argument('status')))
if self.check_api(token):
if self.is_admin(token):
self.db.execute("update tickets set status=%s where id=%s",status,id)
output = {
'message':'Status Ticket with id -'+str(id)+'- Changed Successfully',
'code':'200'
}
self.write(output)
else:
output = {
'message':'Forbidden',
'code':'403'
}
self.set_status(403)
self.write(output)
else:
output = {
'message':'Invalid token',
'code':'401'
}
self.set_status(401)
self.write(output)
def main():
tornado.options.parse_command_line()
http_server = tornado.httpserver.HTTPServer(Application())
http_server.listen(options.port)
tornado.ioloop.IOLoop.current().start()
if __name__ == "__main__":
main()