forked from nfsnfs/COSCUP-System
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoscup.py
519 lines (407 loc) · 16.2 KB
/
coscup.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import traceback
import copy
import pymongo
from flask import Flask, jsonify, request, abort
from mongokit import *
from datetime import datetime, timedelta
from pymongo.errors import DuplicateKeyError
from util.crypto import *
from util.userperm import *
from tasks import *
from db.permission import Permission
from db.account import Account
from db.userdata import UserData
from db.userdatalog import UserDataLog
import ses.awsses
import config
app = Flask(__name__)
api_ver = r'0.1'
sys.stdout = sys.stderr
'''
Welcome
'''
@app.route('/')
def hello_coscup():
return jsonify({ 'msg': 'Hello COSCUP', 'ver': api_ver })
'''
Login
POST /login
'''
@app.route('/login', methods=['POST'])
def login():
if not request.json or not all (key in request.json for key in ('user', 'passwd')):
abort(400)
user = request.json['user']
passwd = request.json['passwd']
connection = Connection()
connection.register([Account])
account = connection.Account.find_one({'id': user})
if account:
# check password
if is_correct_passwd(account['passwd'], passwd, config.SALT):
expired_time = datetime.now() + timedelta(hours=1)
message = { 'user': account['id'], 'expired': expired_time.strftime('%Y-%m-%d %H:%M:%S') }
return jsonify({ 'token': generate_token(message, config.TOKEN_SECRET, config.TOKEN_ALGO),
'role': account['role'], 'data': account['data'] })
return jsonify({ 'exception': 'wrong id or password' })
'''
Apply account
POST /apply
'''
@app.route('/apply/<temp_token>', methods=['POST', 'GET'])
def apply(temp_token):
# decrypt the token and get the email
try:
user_temp = decrypt_token(temp_token, config.TOKEN_SECRET, config.TOKEN_ALGO)
except:
return jsonify({ 'exception': 'token error' })
if request.method == 'POST':
if not request.json or not all (key in request.json for key in ('user', 'passwd', 'email')):
abort(400)
connection = Connection()
connection.register([Account])
# check if the token is used
if connection.Account.find_one({ 'email': user_temp['email'] }):
return jsonify({ 'exception': 'token was used' })
user = request.json['user']
passwd = request.json['passwd']
email = user_temp['email']
role = user_temp['team']
data = False
if not connection.Account.find_one({ 'id': user }):
new_account = connection.Account()
new_account['id'] = user
new_account['passwd'] = hash_passwd(passwd, config.SALT)
new_account['email'] = email
new_account['role'] = role
new_account['data'] = data
new_account.save()
return jsonify({ 'msg': 'ok' })
else:
return jsonify({ 'exception': 'account existed' })
elif request.method == 'GET':
return jsonify({ 'email': user_temp['email'], 'team': user_temp['team'] })
'''
User personal information detail
POST/GET/PUT /user/<target_user>
if there is no target_user, this endpoint would GET/POST/PUT the his/her own data
'''
@app.route('/user', methods=['GET', 'POST', 'PUT'], defaults={ 'target_user': '' })
@app.route('/user/<target_user>', methods=['GET', 'POST', 'PUT'])
def userInfo(target_user):
try:
user = get_user_from_token(request.headers['Token'], config.TOKEN_SECRET, config.TOKEN_ALGO)
except:
return jsonify({ 'execption': 'token error' })
self = False
if target_user == '' or target_user == user:
target_user = user
self = True
connection = Connection()
connection.register([Permission, UserData, Account, UserDataLog])
if request.method == 'GET':
response = {}
# get my user information
try:
userdata = connection.UserData.find_one({ 'id': user })
target_userdata = connection.UserData.find_one({ 'id': target_user })
permission = get_permission(target_userdata['role'])
for key in permission:
if key in target_userdata:
if check_read_permission(userdata['role'], permission[key]['read'], self):
response[key] = target_userdata[key]
except Exception as e:
print traceback.format_exc()
return jsonify({ 'exception': 'user not found' })
return jsonify(response)
elif request.method == 'POST':
# POST method only deals with 'self' data, instead of target_user
# insert my user information
try:
account = connection.Account.find_one({ 'id': user })
new_userdata = connection.UserData()
permission = get_permission(['self'])
for key in permission:
if 'self' in permission[key]['write'] and key in request.json:
new_userdata[key] = request.json[key]
role = list(set(account['role'] + request.json['team']))
new_userdata['id'] = user
new_userdata['role'] = role
new_userdata.save()
account['role'] = role
account['data'] = True
account.save()
except RequireFieldError:
return jsonify({ 'exception': 'missing field(s)' })
except SchemaTypeError:
return jsonify({ 'exception': 'field(s) type error' })
except DuplicateKeyError:
return jsonify({ 'exception': 'user existed' })
except Exception as e:
print traceback.format_exc()
return jsonify({ 'exception': 'system error' })
# celery: deferred send mail to admin
notify_info = {
'date': datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
'id': user,
'target_email': new_userdata['email'],
'redmine': new_userdata['redmine'],
'team': new_userdata['team'],
'first_name': new_userdata['first_name'],
'last_name': new_userdata['last_name']
}
for mail in config.ADMIN_EMAIL:
notify_info['email'] = mail
notify_new_user_to_admin.delay(notify_info)
# send welcome mail
info = { 'email': new_userdata['email'], 'nickname': new_userdata['nickname'] }
r = ses.awsses.send_welcome(info)
if r is None:
return jsonify({ 'msg': 'ok', 'exception': 'cannot send welcome mail' })
return jsonify({ 'msg': 'ok' })
elif request.method == 'PUT':
# update my user information
try:
userdata = connection.UserData.find_one({ 'id': user })
target_userdata = connection.UserData.find_one({ 'id': target_user })
old_target_userdata = copy.deepcopy(target_userdata)
permission = get_permission(target_userdata['role'])
log_entry = connection.UserDataLog()
log_entry['who'] = user
log_entry['id'] = target_user
log_entry['modified'] = list()
for key in permission:
if check_write_permission(userdata['role'], permission[key]['write'], self) and key in request.json:
target_userdata[key] = request.json[key]
if old_target_userdata.get(key) is None or target_userdata[key] != old_target_userdata[key]:
modified = { key: {'old': old_target_userdata.get(key), 'new': target_userdata[key]} }
log_entry['modified'].append(modified)
target_userdata.save()
log_entry['datetime'] = datetime.now()
if log_entry['modified']:
log_entry.save()
except:
print traceback.format_exc()
return jsonify({ 'exception': 'system error' })
return jsonify({ 'msg': 'ok' })
'''
Reset password
POST
'''
@app.route('/resetpasswd', methods=['POST'], defaults={ 'token': None })
@app.route('/resetpasswd/<token>', methods=['POST'])
def resetPassword(token):
connection = Connection()
connection.register([Account, UserData])
if token is not None:
# reset password
try:
user = get_user_from_reset_passwd(token, config.TOKEN_SECRET, config.TOKEN_ALGO)
except Exception as e:
return jsonify({ 'exception': e })
try:
new_passwd = request.json['passwd']
except:
return jsonify({ 'exception': 'missing field(s)' })
# print user
user_account = connection.Account.find_one({ 'id': user })
user_account['passwd'] = hash_passwd(new_passwd, config.SALT)
user_account.save()
return jsonify({ 'msg': 'ok' })
else:
# send mail for resetting password
try:
user = request.json['user']
#email = request.json['email']
except:
return jsonify({ 'exception': 'missing field(s)' })
try:
#user_account = connection.Account.find_one({ 'id': user })
user_data = connection.UserData.find_one({ 'id': user })
expired_time = datetime.now() + timedelta(hours=1)
#token_data = { 'id': user_account['id'], 'email': user_account['email'],
# 'reset': 1, 'expired': expired_time.strftime('%Y-%m-%d %H:%M:%S') }
token_data = { 'id': user_data['id'], 'email': user_data['email'],
'reset': 1, 'expired': expired_time.strftime('%Y-%m-%d %H:%M:%S') }
# celery: send mail to user
notify_info = {
'user': user_data['id'],
'email': user_data['email'],
'url': config.BASEURL + '/?r=' + generate_token(token_data, config.TOKEN_SECRET, config.TOKEN_ALGO) + '#reset'
}
forget_passwd(notify_info)
return jsonify({ 'msg': 'ok' })
except:
print traceback.format_exc()
return jsonify({ 'exception': 'cannot find user' })
return jsonify({ 'msg': 'ok'})
'''
Send First Mail
POST
'''
@app.route('/invite', methods=['POST'])
def send_checkin_mail():
try:
user = get_user_from_token(request.headers['Token'], config.TOKEN_SECRET, config.TOKEN_ALGO)
except:
return jsonify({ 'exception': 'token error' })
response = {'msg': 'ok', 'email': []}
# only admin can do this !
connection = Connection()
connection.register([Account])
account = connection.Account.find_one({ 'id': user })
try:
if 'admin' in account['role']:
for tmp in request.json:
url = config.BASEURL + '/?apply='
url += generate_token(tmp, config.TOKEN_SECRET, config.TOKEN_ALGO)
url += '#apply'
tmp['url'] = url
r = ses.awsses.send_first(tmp)
if r != None:
response['email'].append(tmp['email'])
else:
raise Exception('something error')
except Exception as e:
print traceback.format_exc()
return jsonify({ 'exception': 'system error' })
return jsonify(response)
'''
Users
GET
'''
@app.route('/users/', methods=['GET'], defaults={ 'team': '' })
@app.route('/users/<team>', methods=['GET'])
def users(team):
try:
user = get_user_from_token(request.headers['Token'], config.TOKEN_SECRET, config.TOKEN_ALGO)
except:
return jsonify({ 'exception': 'token error' })
connection = Connection()
connection.register([UserData])
response = { 'msg': 'ok', 'users': [] }
if team == '':
# get all users
for user in connection.UserData.find():
response['users'].append({ 'id': user['id'], 'team': user['team'] })
else:
# get for a certain team
for user in connection.UserData.find({ 'team': team }):
response['users'].append({ 'id': user['id'], 'team': user['team'] })
return jsonify(response)
'''
Search
GET /search/?<type>=<value>
if value is empty, that means "" or not existed
'''
@app.route('/search/', methods=['GET'])
def search():
try:
user = get_user_from_token(request.headers['Token'], config.TOKEN_SECRET, config.TOKEN_ALGO)
except:
return jsonify({ 'exception': 'token error' })
search_dict = {}
search_list = []
response = { 'users': [], 'msg': 'ok' }
connection = Connection()
connection.register([Permission, UserData])
# get the role of current user
try:
userdata = connection.UserData.find_one({ 'id': user })
role = userdata['role']
except:
jsonify({ 'exception': 'user not found'})
# get the query parameters
for key in request.args.keys():
tmp_value = request.args.get(key, '')
# workaround
if key == 'team':
# parse this value to list
if tmp_value != '':
search_dict.update({ key: {'$all': tmp_value.split(',')}})
search_list.append(key)
continue;
if tmp_value == 'null':
search_dict.update({ '$or': [{key: {'$exists': False}}, {key: ''}]})
search_list.append(key)
elif tmp_value.lower() == 'true':
search_dict.update({ key: True })
search_list.append(key)
elif tmp_value.lower() == 'false':
search_dict.update({ key: False })
search_list.append(key)
elif tmp_value != '':
search_dict.update({ key: tmp_value })
search_list.append(key)
try:
result = connection.UserData.find(search_dict).sort('id', 1)
for user_result in result:
temp_userdata = {}
if user_result['id'] == user:
self = True
else:
self = False
permission = get_permission(user_result['role'])
if all(check_read_permission(userdata['role'], permission[search_key]['read'], self) for search_key in search_list):
for key in permission:
if check_read_permission(userdata['role'], permission[key]['read'], self):
temp_userdata.update({ key: user_result.get(key, '') })
response['users'].append(temp_userdata)
except Exception as e:
print traceback.format_exc()
return jsonify({ 'exception': 'error' })
return jsonify(response)
'''
try:
user = get_user_from_token(request.headers['Token'], config.TOKEN_SECRET, config.TOKEN_ALGO)
except:
return jsonify({ 'exception': 'token error' })
search_type = request.args.get('type', '')
search_value = request.args.get('value', '')
for test in request.args:
print request.args[test]
print search_type
print search_value
response = { 'result': [], 'msg': 'ok' }
connection = Connection()
connection.register([Permission, UserData])
try:
userdata = connection.UserData.find_one({ 'id': user })
for user_result in connection.UserData.find({ search_type: search_value }):
temp_userdata = {}
self = False
if user_result['id'] == user:
self = True
permission = get_permission(user_result['role'])
if check_read_permission(userdata['role'], permission[search_type]['read'], self):
for key in permission:
if check_read_permission(userdata['role'], permission[key]['read'], self):
if key in user_result:
temp_userdata[key] = user_result[key]
response['result'].append(temp_userdata)
except Exception as e:
print traceback.format_exc()
return jsonify({ 'exception': 'error' })
return jsonify(response)
'''
'''
Permission
GET /perm/<role>
'''
@app.route('/perm/<role>', methods=['GET'])
def perm(role):
response = {}
connection = Connection()
connection.register([Permission])
try:
permission = connection.Permission.find_one({ 'role': role })
response = permission['fields']
except Exception as e:
print traceback.format_exc()
return jsonify({ 'exception': 'error' })
return jsonify(response)
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)