56 lines
1.6 KiB
Plaintext
56 lines
1.6 KiB
Plaintext
import json
|
|
|
|
|
|
def _parse_roleids(roleid):
|
|
if roleid is None:
|
|
return []
|
|
if isinstance(roleid, list):
|
|
return [i for i in roleid if i]
|
|
if isinstance(roleid, str):
|
|
roleid = roleid.strip()
|
|
if not roleid:
|
|
return []
|
|
try:
|
|
roleids = json.loads(roleid)
|
|
except Exception:
|
|
roleids = [roleid]
|
|
if isinstance(roleids, list):
|
|
return [i for i in roleids if i]
|
|
return [roleids] if roleids else []
|
|
return [roleid]
|
|
|
|
|
|
async def add_user_role(ns):
|
|
"""
|
|
用户分配角色
|
|
:param userid: 用户id
|
|
:param roleid: 角色id列表
|
|
:return:
|
|
"""
|
|
if not ns or not ns.get('userid') or 'roleid' not in ns:
|
|
return {'status': False, 'msg': '参数不正确'}
|
|
|
|
userid = ns.get('userid')
|
|
roleids = _parse_roleids(ns.get('roleid'))
|
|
db = DBPools()
|
|
async with db.sqlorContext('kboss') as sor:
|
|
userroles = await sor.R('userrole', {'userid': userid})
|
|
for userrole in userroles:
|
|
if userrole.get('roleid') not in roleids:
|
|
await sor.U('userrole', {'del_flg': '1', 'id': userrole['id']})
|
|
|
|
for roleid in roleids:
|
|
roles = {'userid': userid, 'roleid': roleid}
|
|
reacs = await sor.R('userrole', roles)
|
|
if len(reacs) >= 1:
|
|
await sor.U('userrole', {'id': reacs[0]['id'], 'del_flg': '0'})
|
|
continue
|
|
await sor.C('userrole', {'id': uuid(), 'userid': userid, 'roleid': roleid})
|
|
return {'status': True, 'msg': '添加成功'}
|
|
|
|
return {'status': False, 'msg': '添加失败'}
|
|
|
|
|
|
ret = await add_user_role(params_kw)
|
|
return ret
|