This commit is contained in:
yumoqing 2024-11-26 14:42:14 +08:00
parent bfedcb2977
commit 1cbe90f898
4 changed files with 53 additions and 38 deletions

View File

@ -40,13 +40,13 @@ def get_apikey_from_token(token):
except: except:
return None return None
async def get_user_roles(): async def get_user_roles(userid):
userid = await get_user()
sql = "select b.name from userrole a, role b where a.userid=${userid}$ and a.roleid = b.id" sql = "select b.name from userrole a, role b where a.userid=${userid}$ and a.roleid = b.id"
db = DBPools() db = DBPools()
roles = [] roles = []
dbname = await get_dbname()
async with db.sqlorContext(dbname) as sor: async with db.sqlorContext(dbname) as sor:
recs = sor.sqlExe(sql, {'userid':userid}) recs = await sor.sqlExe(sql, {'userid':userid})
if len(recs) < 1: if len(recs) < 1:
return roles return roles
for r in recs: for r in recs:
@ -115,15 +115,15 @@ async def getAuthenticationUserid(sor, request):
return None return None
return recs[0].userid return recs[0].userid
async def objcheckperm(obj, userid, path, request): async def objcheckperm(obj, request, userid, path):
debug(f'check permission: {userid=}, {path=}')
sql = """select distinct a.*, c.userid from sql = """select distinct a.*, c.userid from
(select id, path from permission where path=${path}$ and del_flg='0') a (select id, path from permission where path=${path}$) a
right join right join
rolepermission b on a.id = b.permid rolepermission b on a.id = b.permid
right join userrole c on b.roleid = c.roleid right join userrole c on b.roleid = c.roleid
where c.userid = ${userid}$ where c.userid = ${userid}$
and b.del_flg='0' """
and c.del_flg='0'"""
rf = RegisterFunction() rf = RegisterFunction()
dbname = await rf.exe('get_module_dbname', 'rbac') dbname = await rf.exe('get_module_dbname', 'rbac')
@ -134,7 +134,7 @@ and c.del_flg='0'"""
debug(f'{path=} not found in permission, can access') debug(f'{path=} not found in permission, can access')
return True return True
if userid is None: if userid is None:
userid = await getAPIkeyUserid(sor, request) userid = await getAuthenticationUserid(sor, request)
if userid is None: if userid is None:
debug(f'{userid=} is None, can not access {path=}') debug(f'{userid=} is None, can not access {path=}')
return False return False

View File

@ -1,5 +1,7 @@
import os import os
import sys import sys
import argparse
from traceback import format_exc from traceback import format_exc
import asyncio import asyncio
from appPublic.uniqueID import getID from appPublic.uniqueID import getID
@ -10,6 +12,7 @@ from appPublic.folderUtils import listFile
from appPublic.registerfunction import RegisterFunction from appPublic.registerfunction import RegisterFunction
from sqlor.dbpools import DBPools from sqlor.dbpools import DBPools
from rbac.check_perm import mypassword
databases = { databases = {
"sage":{ "sage":{
"driver":"aiomysql", "driver":"aiomysql",
@ -38,6 +41,8 @@ async def insert_perm(path):
av_folders = [ av_folders = [
'/index.ui', '/index.ui',
'/user.ui', '/user.ui',
'/menu.ui',
'/get_code.dspy',
'/top.ui', '/top.ui',
'/center', '/center',
'/bottom.ui', '/bottom.ui',
@ -64,6 +69,7 @@ rbac_tables = [
role_perms = { role_perms = {
'superuser': [ 'superuser': [
'/get_code.dspy',
'/rbac/add_adminuser.ui', '/rbac/add_adminuser.ui',
'/rbac/add_adminuser.dspy', '/rbac/add_adminuser.dspy',
'/rbac/role/index.ui', '/rbac/role/index.ui',
@ -96,7 +102,9 @@ role_perms = {
] ]
} }
async def init_perms(sor): async def init_perms():
db = DBPools()
async with db.sqlorContext('sage') as sor:
for rn, pths in role_perms.items(): for rn, pths in role_perms.items():
roles = await sor.sqlExe('select * from role where name=${rn}$', {'rn':rn}) roles = await sor.sqlExe('select * from role where name=${rn}$', {'rn':rn})
if len(roles) == 0: if len(roles) == 0:
@ -137,10 +145,9 @@ async def init_rbac(passwd):
'roleid':roleid 'roleid':roleid
} }
await sor.C('userrole', ns1) await sor.C('userrole', ns1)
await init_perms(sor)
async def add_permissions(): async def add_permissions(workdir):
root = os.path.abspath('../wwwroot') root = os.path.join(workdir, 'wwwroot')
for f in listFile(root, rescursive=True): for f in listFile(root, rescursive=True):
cnt = len(root) cnt = len(root)
pth = f[cnt:] pth = f[cnt:]
@ -153,14 +160,22 @@ async def add_permissions():
if act: if act:
await insert_perm(pth) await insert_perm(pth)
async def main(passwd): async def main(workdir, passwd):
await add_permissions() await add_permissions(workdir)
await init_rbac(passwd) await init_rbac(passwd)
if __name__ == '__main__': await init_perms()
if len(sys.argv) < 2:
print(f'Usage:\n{sys.argv[0]} super_user_passwd\n') if __name__ == '__main__':
sys.exit(1) parser = argparse.ArgumentParser(prog='RBAC init')
supassword = sys.argv[1] parser.add_argument('-w', '--workdir')
DBPools(databases) parser.add_argument('password')
asyncio.get_event_loop().run_until_complete(main(supassword)) args = parser.parse_args()
if args.password is None:
parser.usage()
sys.exit(1)
print(f'{args=}')
config = getConfig(args.workdir, {'workdir':args.workdir})
supassword = mypassword(args.password)
DBPools(config.databases)
asyncio.get_event_loop().run_until_complete(main(args.workdir, supassword))

View File

@ -1,11 +1,11 @@
username = params_kw.get('username') username = params_kw.get('username')
passwd = params_kw.get('password') passwd = params_kw.get('passwd')
if not passwd: if not passwd:
return Error(title='Login failed', message='Password is required') return UiError(title='Login failed', message='Password is required')
passwd = password(passwd) passwd = password(passwd)
rzt = await check_user_password(username, passwd): rzt = await check_user_password(request, username, passwd)
if rzt: if rzt:
return Message(title='Logined', message=f'Welcome back {get_usernme()}') return UiMessage(title='Logined', message=f'Welcome back ')
return Error(title='login failed', message='user and password mismatch') return UiError(title='login failed', message='user and password mismatch')

View File

@ -4,8 +4,8 @@
"options":{ "options":{
"auto_open":true, "auto_open":true,
"anthor":"cc", "anthor":"cc",
"width":"70%", "cwidth":20,
"height":"70%" "cheight":"14"
}, },
"subwidgets":[ "subwidgets":[
{ {