rbac/script/init.py

186 lines
4.6 KiB
Python
Raw Normal View History

2024-11-10 13:26:34 +08:00
import os
import sys
2024-11-26 14:42:14 +08:00
import argparse
2024-11-10 13:26:34 +08:00
from traceback import format_exc
import asyncio
from appPublic.uniqueID import getID
from appPublic.timeUtils import days_between, strdate_add
from appPublic.jsonConfig import getConfig
from random import randint, random
from appPublic.folderUtils import listFile
from appPublic.registerfunction import RegisterFunction
from sqlor.dbpools import DBPools
2024-11-26 14:42:14 +08:00
from rbac.check_perm import mypassword
2024-11-10 13:26:34 +08:00
databases = {
"sage":{
"driver":"aiomysql",
"async_mode":True,
"coding":"utf8",
"maxconn":100,
"dbname":"sage",
"kwargs":{
"user":"test",
"db":"sage",
"password":"QUZVcXg5V1p1STMybG5Ia6mX9D0v7+g=",
"host":"localhost"
}
}
}
async def insert_perm(path):
db = DBPools()
async with db.sqlorContext('sage') as sor:
ns = {
'id':getID(),
'path':path
}
await sor.C('permission', ns)
av_folders = [
'/index.ui',
'/user.ui',
2024-11-26 14:42:14 +08:00
'/menu.ui',
'/get_code.dspy',
2024-11-10 13:26:34 +08:00
'/top.ui',
'/center',
'/bottom.ui',
'/app_panel.ui',
'/bricks',
'/imgs',
'/login.ui',
'/up_login.dspy',
'/public',
'/tmp'
]
orgtypes = ['owner', 'reseller']
type_roles = {
'owner':['superuser', 'customer'],
'reseller':['admin','operator', 'sale', 'maintainer', 'accountant'],
'customer':['admin', 'customer']
}
rbac_tables = [
"organization", "orgtypes", "role", "users", "userrole", "rolepermission", "permission",
"userapp", "userdepartment" ]
role_perms = {
'superuser': [
2024-11-26 14:42:14 +08:00
'/get_code.dspy',
2024-11-10 13:26:34 +08:00
'/rbac/add_adminuser.ui',
'/rbac/add_adminuser.dspy',
'/rbac/role/index.ui',
'/rbac/role/get_role.dspy',
'/rbac/role/update_role.dspy',
2024-11-26 15:24:01 +08:00
'/rbac/role/delete_role.dspy',
2024-11-10 13:26:34 +08:00
'/rbac/role/add_role.dspy',
'/rbac/permission/add_permission.dspy',
'/rbac/permission/delete_permission.dspy',
'/rbac/permission/update_permission.dspy',
'/rbac/permission/get_permission.dspy',
'/rbac/permission/index.ui',
'/rbac/rolepermission/add_rolepermission.dspy',
'/rbac/rolepermission/delete_rolepermission.dspy',
'/rbac/rolepermission/update_rolepermission.dspy',
'/rbac/rolepermission/get_rolepermission.dspy',
'/rbac/rolepermission/index.ui'
],
'admin':[
'/rbac/users/add_users.dspy',
'/rbac/users/delete_users.dspy',
'/rbac/users/update_users.dspy',
'/rbac/users/get_users.dspy',
'/rbac/users/index.ui',
'/rbac/userrole/add_userrole.dspy',
'/rbac/userrole/delete_userrole.dspy',
'/rbac/userrole/update_userrole.dspy',
'/rbac/userrole/get_userrole.dspy',
'/rbac/userrole/index.ui'
]
}
2024-11-26 14:42:14 +08:00
async def init_perms():
db = DBPools()
async with db.sqlorContext('sage') as sor:
for rn, pths in role_perms.items():
roles = await sor.sqlExe('select * from role where name=${rn}$', {'rn':rn})
if len(roles) == 0:
2024-11-26 15:24:01 +08:00
print(f'{rn=} not exists')
2024-11-26 14:42:14 +08:00
continue
for p in pths:
2024-11-26 15:24:01 +08:00
print(f'{rn=} set {p=}')
2024-11-26 14:42:14 +08:00
perms = await sor.sqlExe('select * from permission where path=${p}$', {'p':p})
if len(perms):
await sor.C('rolepermission', {'id':getID(),
'roleid':roles[0].id,
'permid':perms[0].id})
2024-11-26 15:24:01 +08:00
else:
print(f'{p} is not exists')
2024-11-10 13:26:34 +08:00
async def init_rbac(passwd):
db = DBPools()
async with db.sqlorContext('sage') as sor:
recs = await sor.R('organization', {'id':'0'})
if len(recs) > 0:
return
await sor.C('organization',{'id':'0'})
await sor.C('orgtypes', {'id':getID(), 'orgid':'0', 'orgtypeid':'owner'})
await sor.C('orgtypes', {'id':getID(), 'orgid':'0', 'orgtypeid':'reseller'})
roleid = ''
for orgtype, roles in type_roles.items():
for r in roles:
id = getID()
await sor.C('role', {'id':id,'orgtypeid':orgtype,'name':r})
if orgtype == 'owner' and r == 'superuser':
roleid = id
ns = {
'id':getID(),
'username':'superuser',
'orgid':'0',
'password':passwd
}
await sor.C('users', ns)
ns1 = {
'id':getID(),
'userid':ns['id'],
'roleid':roleid
}
await sor.C('userrole', ns1)
2024-11-26 14:42:14 +08:00
async def add_permissions(workdir):
root = os.path.join(workdir, 'wwwroot')
2024-11-10 13:26:34 +08:00
for f in listFile(root, rescursive=True):
cnt = len(root)
pth = f[cnt:]
print(f'{f=},{root=},{pth=}, {cnt=}')
act = True
for f in av_folders:
if pth.startswith(f):
act = False
break
if act:
await insert_perm(pth)
2024-11-26 14:42:14 +08:00
async def main(workdir, passwd):
await add_permissions(workdir)
2024-11-10 13:26:34 +08:00
await init_rbac(passwd)
2024-11-26 14:42:14 +08:00
await init_perms()
2024-11-10 13:26:34 +08:00
if __name__ == '__main__':
2024-11-26 14:42:14 +08:00
parser = argparse.ArgumentParser(prog='RBAC init')
parser.add_argument('-w', '--workdir')
parser.add_argument('password')
args = parser.parse_args()
if args.password is None:
parser.usage()
2024-11-10 13:26:34 +08:00
sys.exit(1)
2024-11-26 14:42:14 +08:00
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))
2024-11-10 13:26:34 +08:00