rbac/script/roleperm.py

65 lines
1.7 KiB
Python
Raw Normal View History

2025-01-16 17:53:23 +08:00
import sys
import os
import asyncio
from sqlor.dbpools import DBPools
from appPublic.jsonConfig import getConfig
from appPublic.uniqueID import getID
from appPublic.asynciorun import run
async def set_role_perm(dbname, module, orgtype, role, tblname):
db = DBPools()
async with db.sqlorContext(dbname) as sor:
paths = [
f'/{module}/{tblname}/index.ui',
f'/{module}/{tblname}/get_{tblname}.dspy',
f'/{module}/{tblname}/add_{tblname}.dspy',
f'/{module}/{tblname}/delete_{tblname}.dspy',
f'/{module}/{tblname}/update_{tblname}.dspy'
]
for pat in paths:
recs = await sor.R('permission', {'path': pat})
if len(recs) == 0:
permid = getID()
await sor.C('permission', {'id':permid, 'path':pat})
else:
permid = recs[0].id
recs = await sor.R('role', {'orgtypeid':orgtype, 'name':role})
if len(recs) == 0:
roleid = getID()
await sor.C('role', {
'id':roleid,
'name':role,
'orgtypeid':orgtype
})
else:
roleid = recs[0].id
await sor.C('rolepermission', {
'id':getID(),
'roleid':roleid,
'permid':permid
})
print(f'{orgtype=}, {role=}, {tblname=} permission configured')
async def main():
if len(sys.argv) < 6:
print(f'{sys.argv[0]} dbname module orgtype role tblname ...\n')
sys.exit(1)
dbname = sys.argv[1]
module = sys.argv[2]
orgtype = sys.argv[3]
role = sys.argv[4]
for tblname in sys.argv[5:]:
await set_role_perm(dbname, module, orgtype, role, tblname)
def run(coro):
p = '.'
config = getConfig(p, {'woridir':p})
DBPools(config.databases)
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(coro())
if __name__ == '__main__':
run(main)