-
Notifications
You must be signed in to change notification settings - Fork 0
/
bp_admin_user.py
446 lines (407 loc) · 18.7 KB
/
bp_admin_user.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
from sanic import Blueprint
import asyncio
import datetime
import aiomysql
from sanic import Sanic
from sanic import response
from sanic.response import json
from db_support import db_select, db_insert, db_delete, db_update, alchemyencoder
from db_support import aio_post, aio_delete, aio_put, aio_patch
import db
import os
import aiofiles
import uuid
import xlrd
import hashlib
import aiohttp
from sanic.response import stream, text
import zipfile
import json as innerjson
import time
import signal
from log_support import do_log
from config import *
bp_admin_user = Blueprint('bp_admin_user')
@bp_admin_user.post("/admin/user")
async def add_user(request):
try:
data = request.json
except:
do_log(4, "add_soft:参数缺失或不规范")
return json({'state': -1, 'info': '参数缺失或不规范'})
time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
try:
password = data['stu_number']
md5 = hashlib.md5()
md5.update(password.encode('utf-8'))
password = md5.hexdigest()
if await db_insert("user", created_at = time, updated_at = time, name = data['name'], email = data['email'], resource_path = data['resource_path'], passwd = password,stu_number = data['stu_number'], phone = data['phone']):
u_id= await db_select("user",find=["id"],updated_at=time,created_at=time, stu_number = data['stu_number'], name = data['name'])
#########
do_log(2, "add_user:用户添加成功![name=%s]" % (data['name'],))
return json({'state': 0, 'info': '成功', 'id': u_id})
#########
else:
do_log(4, "add_user:数据库操作失败[name=%s]" % (data['name'],))
return json({'state': -2, 'info': '数据库操作失败'})
except Exception as e:
do_log(4, "add_user:参数缺失或不规范")
return json({'state': -1, 'info': '参数缺失或不规范'})
@bp_admin_user.get("/admin/user/fresh")
async def add_user(request):
total = 0
unlist = []
try:
users = await db_select("user")
for user in users:
if(user['op_user']==None or user['op_user']==''):
total += 1
unlist.append(user)
do_log(2, "get_invalid:查询成功![admin]")
return json({'state': 0, 'total': total, 'list': unlist})
except Exception as e:
do_log(4, "add_soft:参数缺失或不规范")
return json({'state': -1, 'info': 'database select error.'})
@bp_admin_user.post("/admin/user/fresh")
async def add_user(request):
total = 0
success = 0
userTodo = []
try:
users = await db_select("user")
for user in users:
if(user['op_user']==None or user['op_user']==''):
userTodo.append(user)
total += 1
for todo in userTodo:
httpdata={}
resp = await aio_post(vmURL+'user?user_id='+str(todo['id']), httpdata)
#print(resp)
if(resp['state'] != 1):
do_log(4, "初始化环境中断![admin]")
return json({'state': -1, 'info':'初始化环境中断!','done': success,'total':total})
success += 1
do_log(2, "valid:初始化环境成功![admin]")
return json({'state': 0, 'total': total})
except Exception as e:
#print(e)
do_log(4, "Valid:OpenStack无响应")
return json({'state': -2, 'info': 'Valid:OpenStack无响应'})
@bp_admin_user.post("/admin/user/role")
async def add_user(request):
try:
data = request.json
except:
do_log(4, "add_ur:参数缺失或不规范")
return json({'state': -1, 'info': '参数缺失或不规范'})
time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
try:
if await db_insert("ur", created_at = time, updated_at = time, u_id = int(data['u_id']), r_id = int(data['r_id'])):
do_log(2, "add_ur:ur添加成功![user_id=%s]" % (data['u_id'],))
return json({'state': 0, 'info': '成功'})
else:
do_log(4, "add_ur:数据库操作失败[user_id=%s]" % (data['u_id'],))
return json({'state': -2, 'info': '数据库操作失败'})
except Exception as e:
do_log(4, "add_ur:参数缺失或不规范")
return json({'state': -1, 'info': '参数缺失或不规范'})
@bp_admin_user.route("/admin/user", methods=['PATCH'])
async def update_user(request):
try:
get_info = request.raw_args
id = int(get_info['id'])
data = request.json
except:
do_log(4, "update_user:参数缺失或不规范")
return json({'state': -1, 'info': '参数缺失或不规范'})
time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
try:
if await db_update("user",set={ "updated_at":time, "name":data['name'], "email":data['email'], "resource_path":data['resource_path'], "stu_number":data['stu_number'], "phone":data['phone'],}, id=id):
do_log(2, "update_user:用户信息修改成功![id=%d]" % (id,))
return json({'state': 0, 'info': '成功'})
else:
do_log(4, "update_user:数据库操作失败[id=%d]" % (id,))
return json({'state': -2, 'info': '数据库操作失败'})
except:
do_log(4, "update_user:参数缺失或不规范")
return json({'state': -1, 'info': '参数缺失或不规范'})
@bp_admin_user.route("/admin/user/mul/del", methods=['GET'])
async def delete_role(request):
get_info = request.raw_args
try:
id = int(get_info['id'])
except:
do_log(4, "delete_role:参数缺失或不规范")
return json({'state': -1, 'info': '参数缺失或不规范'})
try:
total = 0
success = 0
uids = await db_select("ur", find=["u_id"], r_id=id)
dictId = []
for uid in uids:
dictId.append(uid['u_id'])
try:
tids = await db_select("target", find=["template"], user=uid['u_id'], deleted=0)
for tid in tids:
resp = await aio_delete(vmURL+'environment?'+'template_id='+str(tid['template'])+'&user_id='+str(uid['u_id']))
if resp['state'] == 1:
break
else:
do_log(4, "delete_user:用户存在未关闭的实验,且删除环境失败。")
return json({'state': -1, 'info': '用户存在未关闭的实验,且删除环境失败。'})
except Exception as e:
do_log(4, "delete_user:用户存在未关闭的实验,且删除环境失败。")
return json({'state': -1, 'info': '用户存在未关闭的实验,且删除环境失败。'})
except Exception as e:
do_log(4, "delete_user:用户存在未关闭的实验,且删除环境失败。")
return json({'state': -1, 'info': '用户存在未关闭的实验,且删除环境失败。'})
try:
data={"user_id_list": dictId}
resp = await aio_put(vmURL+'user', data)
if(resp['state'] != 1):
do_log(4, "删除OpenStack后台账户失败!")
return json({'state': -1, 'info':'删除OpenStack后台账户失败!'})
except:
do_log(4, "delete_user:OpenStack交互失败!")
return json({'state': -1, 'info': 'OpenStack交互失败!'})
do_log(2, "delete_role:批量删除成功[id=%d]" % (id,))
return json({'state': 0, 'info': '成功'})
@bp_admin_user.route("/admin/user/passwd", methods=['PATCH'])
async def update_user(request):
try:
get_info = request.raw_args
id = int(get_info['id'])
data = request.json
except:
do_log(4, "update_user:参数缺失或不规范")
return json({'state': -1, 'info': '参数缺失或不规范'})
time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
try:
if await db_update("user",set={ "updated_at":time, "passwd":data['passwd']}, id=id):
do_log(2, "update_user:用户信息修改成功![id=%d]" % (id,))
return json({'state': 0, 'info': '成功'})
else:
do_log(4, "update_user:数据库操作失败[id=%d]" % (id,))
return json({'state': -2, 'info': '数据库操作失败'})
except:
do_log(4, "update_user:参数缺失或不规范")
return json({'state': -1, 'info': '参数缺失或不规范'})
@bp_admin_user.route("/admin/user/main", methods=['PATCH'])
async def update_user(request):
try:
get_info = request.raw_args
id = int(get_info['id'])
data = request.json
except:
do_log(4, "update_user:参数缺失或不规范")
return json({'state': -1, 'info': '参数缺失或不规范'})
time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
try:
if await db_update("user",set={ "updated_at":time, "name":data['name'], "email":data['email'], "stu_number":data['stu_number'], "phone":data['phone'],}, id=id):
do_log(2, "update_user:用户信息修改成功![id=%d]" % (id,))
return json({'state': 0, 'info': '成功'})
else:
do_log(4, "update_user:数据库操作失败[id=%d]" % (id,))
return json({'state': -2, 'info': '数据库操作失败'})
except:
do_log(4, "update_user:参数缺失或不规范")
return json({'state': -1, 'info': '参数缺失或不规范'})
@bp_admin_user.route("/admin/user/role", methods=['PATCH'])
async def update_user(request):
try:
get_info = request.raw_args
id = int(get_info['id'])
data = request.json
except:
do_log(4, "update_userRole:参数缺失或不规范")
return json({'state': -1, 'info': '参数缺失或不规范'})
time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
try:
if await db_update("ur",set={ "updated_at":time, "u_id" : int(data['u_id']), "r_id" : int(data['r_id'])}, id=id):
do_log(2, "update_userRole:用户信息修改成功![id=%d]" % (id,))
return json({'state': 0, 'info': '成功'})
else:
do_log(4, "update_userRole:数据库操作失败[id=%d]" % (id,))
return json({'state': -2, 'info': '数据库操作失败'})
except:
do_log(4, "update_userRole:参数缺失或不规范")
return json({'state': -1, 'info': '参数缺失或不规范'})
@bp_admin_user.route("/admin/user/del", methods=['GET'])
async def delete_user(request):
get_info = request.raw_args
try:
id = int(get_info['id'])
except:
do_log(4, "delete_user:参数缺失或不规范")
return json({'state': -1, 'info': '参数缺失或不规范'})
try:
tids = await db_select("target", find=["template"], user=id, deleted=0)
for tid in tids:
resp = await aio_delete(vmURL+'environment?'+'template_id='+str(tid['template'])+'&user_id='+str(id))
if resp['state'] == 1:
break
else:
do_log(4, "delete_user:用户存在未关闭的实验,且删除环境失败。")
return json({'state': -1, 'info': '用户存在未关闭的实验,且删除环境失败。'})
except Exception as e:
do_log(4, "delete_user:用户存在未关闭的实验,且删除环境失败。")
return json({'state': -1, 'info': '用户存在未关闭的实验,且删除环境失败。'})
try:
dictId = [id]
data={"user_id_list": dictId}
resp = await aio_put(vmURL+'user', data)
if(resp['state'] != 1):
do_log(4, "删除OpenStack后台账户失败!")
return json({'state': -1, 'info':'删除OpenStack后台账户失败!'})
do_log(2, "delete_user:用户删除成功[id=%d]" % (id,))
return json({'state': 0, 'info': '成功'})
except:
do_log(4, "delete_user:OpenStack交互失败!")
return json({'state': -1, 'info': 'OpenStack交互失败!'})
@bp_admin_user.get("/admin/user")
async def get_user(request):
"""
get_info = request.raw_args
try:
id = int(get_info['id'])
except:
do_log(4, "get_user:参数缺失或不规范")
return json({'state': -1, 'info': '参数缺失或不规范'})
return_info = await db_select("userView", user_id = id )
"""
return_info = await db_select("userView")
do_log(2, "get_user:用户信息查询成功![admin]")
return json({'state': 0, 'user': return_info})
@bp_admin_user.post("/admin/searchUser")
async def search_user(request):
try:
data=request.json
info=data['search']
except:
do_log(4, "search_user:参数缺失或不规范")
return json({'state': -1, 'info': '参数缺失或不规范'})
sql="""select * from userView where user_name like "%%%s%%" or user_number like "%%%s%%" """ % (info,info)
# #print(sql)
try:
conn = await aiomysql.connect(**db.config)
cur = await conn.cursor(cursor=aiomysql.cursors.DictCursor)
await cur.execute(sql)
re=await cur.fetchall()
await cur.close()
conn.close()
for r in re:
r['created_at'] = str(r['created_at'])
except Exception as e:
#print(e)
do_log(4, "search_user:数据库操作失败[search:%s]" % (info,))
return json({"state":-2,'info':'数据库操作失败'})
do_log(2, "search_user:用户查找成功![search:%s]" % (info,))
return json({'state':0,'userCourse':re})
################################ROLE###################################################################################
@bp_admin_user.get("/admin/userGroup")
async def get_role(request):
return_info = await db_select("role")
do_log(2, "get_role:用户组查询成功![admin]")
return json({'state': 0, 'userGroup': return_info})
@bp_admin_user.post("/admin/userGroup")
async def add_role(request):
try:
data = request.json
except:
do_log(4, "add_software:参数缺失或不规范")
return json({'state': -1, 'info': '参数缺失或不规范'})
time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
try:
if await db_insert("role", updated_at = time, created_at = time, name = data['name'], description = data['description'], privilege = data['privilege']):
do_log(2, "add_role:用户组添加成功!")
return json({'state': 0, 'info': '成功'})
else:
do_log(4, "add_role:数据库操作失败")
return json({'state': -2, 'info': '数据库操作失败'})
except:
do_log(4, "add_role:参数缺失或不规范")
return json({'state': -1, 'info': '参数缺失或不规范'})
@bp_admin_user.route("/admin/userGroup/check", methods=['GET'])
async def update_role(request):
try:
get_info = request.raw_args
role_id = int(get_info['id'])
except:
do_log(4, "update_role:参数缺失或不规范")
return json({'state': -1, 'info': '参数缺失或不规范'})
try:
info = await db_select("ur", find=["u_id"], r_id=role_id)
if info == None or info == []:
do_log(2, "checkrole:用户组用户检查成功![id=%d]" % (role_id,))
return json({'state': 0, 'info': '用户组下无用户!'})
else:
do_log(4, "checkrole:用户组用户检查成功![id=%d]" % (role_id,))
return json({'state': -2, 'info': '用户组下有用户!','total': len(info)})
except:
do_log(4, "update_role:参数缺失或不规范")
return json({'state': -1, 'info': '参数缺失或不规范'})
@bp_admin_user.route("/admin/userGroup", methods=['PATCH'])
async def update_role(request):
try:
get_info = request.raw_args
id = int(get_info['id'])
data = request.json
except:
do_log(4, "update_role:参数缺失或不规范")
return json({'state': -1, 'info': '参数缺失或不规范'})
time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
try:
if await db_update("role",set={ "updated_at":time, "name":data['name'], "description":data['description'], "privilege":data['privilege'],}, id=id):
do_log(2, "update_role:用户组修改成功![id=%d]" % (id,))
return json({'state': 0, 'info': '成功'})
else:
do_log(4, "update_role:数据库操作失败[id=%d]" % (id,))
return json({'state': -2, 'info': '数据库操作失败'})
except:
do_log(4, "update_role:参数缺失或不规范")
return json({'state': -1, 'info': '参数缺失或不规范'})
@bp_admin_user.route("/admin/userGroup/del", methods=['GET'])
async def delete_role(request):
get_info = request.raw_args
try:
id = int(get_info['id'])
except:
do_log(4, "delete_role:参数缺失或不规范")
return json({'state': -1, 'info': '参数缺失或不规范'})
try:
total = 0
success = 0
uids = await db_select("ur", find=["u_id"], r_id=id)
dictId = []
for uid in uids:
dictId.append(uid['u_id'])
try:
tids = await db_select("target", find=["template"], user=uid['u_id'], deleted=0)
for tid in tids:
resp = await aio_delete(vmURL+'environment?'+'template_id='+str(tid['template'])+'&user_id='+str(uid['u_id']))
if resp['state'] == 1:
break
else:
do_log(4, "delete_user:用户存在未关闭的实验,且删除环境失败。")
return json({'state': -1, 'info': '用户存在未关闭的实验,且删除环境失败。'})
except Exception as e:
do_log(4, "delete_user:用户存在未关闭的实验,且删除环境失败。")
return json({'state': -1, 'info': '用户存在未关闭的实验,且删除环境失败。'})
except Exception as e:
do_log(4, "delete_user:用户存在未关闭的实验,且删除环境失败。")
return json({'state': -1, 'info': '用户存在未关闭的实验,且删除环境失败。'})
try:
data={"user_id_list": dictId}
resp = await aio_put(vmURL+'user', data)
if(resp['state'] != 1):
do_log(4, "删除OpenStack后台账户失败!")
return json({'state': -1, 'info':'删除OpenStack后台账户失败!'})
except:
do_log(4, "delete_user:OpenStack交互失败!")
return json({'state': -1, 'info': 'OpenStack交互失败!'})
if await db_delete("role", id=id):
do_log(2, "delete_role:用户组删除成功[id=%d]" % (id,))
return json({'state': 0, 'info': '成功'})
else:
do_log(4, "delete_role:数据库操作失败[id=%d]" % (id,))
return json({'state': -2, 'info': '数据库操作失败'})