refactor: use wildcard % in load_path.py for auto-coverage
This commit is contained in:
parent
c1aec68ce3
commit
dc658fbd9c
@ -1,69 +1,112 @@
|
|||||||
"""Generate RBAC permissions for msp module paths.
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
Run from Sage root with Sage venv:
|
|
||||||
cd ~/repos/sage && ./py3/bin/python ../msp/scripts/load_path.py
|
|
||||||
"""
|
"""
|
||||||
|
msp 模块 RBAC 权限管理脚本
|
||||||
|
|
||||||
|
使用方法:
|
||||||
|
cd ~/repos/sage
|
||||||
|
./py3/bin/python ~/repos/msp/scripts/load_path.py
|
||||||
|
"""
|
||||||
|
|
||||||
|
import subprocess
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import asyncio
|
|
||||||
|
|
||||||
sage_root = os.environ.get('SAGE_ROOT')
|
|
||||||
if sage_root and sage_root not in sys.path:
|
|
||||||
sys.path.insert(0, sage_root)
|
|
||||||
|
|
||||||
from sqlor.dbpools import DBPools
|
|
||||||
from appPublic.jsonConfig import getConfig
|
|
||||||
from appPublic.dictObject import DictObject
|
|
||||||
from appPublic.uniqueID import getID
|
|
||||||
|
|
||||||
|
|
||||||
paths = [
|
def find_sage_root():
|
||||||
("/msp", "logined"),
|
candidates = [
|
||||||
("/msp/index.ui", "logined"),
|
os.path.expanduser("~/repos/sage"),
|
||||||
("/msp/menu.ui", "any"),
|
os.path.expanduser("~/sage"),
|
||||||
("/msp/accordion.ui", "logined"),
|
os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))),
|
||||||
("/msp/app_panel.ui", "logined"),
|
]
|
||||||
("/msp/bottom.ui", "logined"),
|
for c in candidates:
|
||||||
("/msp/center.ui", "logined"),
|
if os.path.isdir(os.path.join(c, "py3")) and os.path.isdir(os.path.join(c, "wwwroot")):
|
||||||
("/msp/connecthost.ui", "logined"),
|
return c
|
||||||
("/msp/connecthost.xterm", "logined"),
|
return None
|
||||||
("/msp/keypress.ui", "logined"),
|
|
||||||
("/msp/msp_profile.md", "logined"),
|
|
||||||
("/msp/msp_profile.ui", "logined"),
|
SAGE_ROOT = find_sage_root()
|
||||||
("/msp/sagelog.ui", "logined"),
|
if not SAGE_ROOT:
|
||||||
("/msp/sagelog.xterm", "logined"),
|
print("ERROR: Cannot find Sage root directory")
|
||||||
("/msp/test.ui", "logined"),
|
sys.exit(1)
|
||||||
("/msp/top.ui", "logined"),
|
|
||||||
("/msp/get_code.dspy", "logined"),
|
PYTHON = os.path.join(SAGE_ROOT, "py3", "bin", "python")
|
||||||
("/msp/imgs/%", "any"),
|
SET_PERM_SCRIPT = os.path.join(SAGE_ROOT, "set_role_perm.py")
|
||||||
|
|
||||||
|
MOD = "msp"
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# 权限路径定义
|
||||||
|
# ============================================================
|
||||||
|
|
||||||
|
# any — 无需登录
|
||||||
|
PATHS_ANY = [
|
||||||
|
f"/{MOD}/menu.ui",
|
||||||
|
f"/{MOD}/imgs/%",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
# logined — 所有已登录用户
|
||||||
|
PATHS_LOGINED = [
|
||||||
|
# 模块入口
|
||||||
|
f"/{MOD}",
|
||||||
|
f"/{MOD}/index.ui",
|
||||||
|
|
||||||
async def main():
|
# 顶层 .ui 页面
|
||||||
config = getConfig('.')
|
f"/{MOD}/msp_profile.ui",
|
||||||
DBPools(config.databases)
|
f"/{MOD}/msp_profile.md",
|
||||||
dbname = 'sage'
|
f"/{MOD}/accordion.ui",
|
||||||
async with DBPools().sqlorContext(dbname) as sor:
|
f"/{MOD}/app_panel.ui",
|
||||||
cnt = 0
|
f"/{MOD}/connecthost.ui",
|
||||||
for path, role in paths:
|
f"/{MOD}/connecthost.xterm",
|
||||||
r = await sor.sqlExe(
|
f"/{MOD}/keypress.ui",
|
||||||
'select * from permission where permcode = ${permcode}$',
|
f"/{MOD}/sagelog.ui",
|
||||||
{'permcode': path}
|
f"/{MOD}/sagelog.xterm",
|
||||||
)
|
f"/{MOD}/test.ui",
|
||||||
if len(r) == 0:
|
|
||||||
await sor.sqlExe(
|
# 老式布局文件(兼容)
|
||||||
'''insert into permission (id, permcode, permname, permtype)
|
f"/{MOD}/top.ui",
|
||||||
values (${id}$, ${permcode}$, ${permname}$, ${permtype}$)''',
|
f"/{MOD}/center.ui",
|
||||||
{
|
f"/{MOD}/bottom.ui",
|
||||||
'id': getID(),
|
|
||||||
'permcode': path,
|
# 顶层 .dspy
|
||||||
'permname': path,
|
f"/{MOD}/get_code.dspy",
|
||||||
'permtype': role,
|
|
||||||
}
|
# CRUD 子目录 — 通配
|
||||||
)
|
f"/{MOD}/devgroup/%",
|
||||||
cnt += 1
|
f"/{MOD}/hostdev/%",
|
||||||
print(f'{cnt} path(s) inserted for msp')
|
f"/{MOD}/mspcatelog/%",
|
||||||
|
f"/{MOD}/serve_status/%",
|
||||||
|
f"/{MOD}/service_log/%",
|
||||||
|
f"/{MOD}/techservice/%",
|
||||||
|
]
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# 执行注册
|
||||||
|
# ============================================================
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
def run_set_perm(role, path):
|
||||||
asyncio.run(main())
|
cmd = [PYTHON, SET_PERM_SCRIPT, role, path]
|
||||||
|
result = subprocess.run(cmd, capture_output=True, text=True)
|
||||||
|
return result.returncode == 0
|
||||||
|
|
||||||
|
|
||||||
|
def register_role_paths(role, paths):
|
||||||
|
count = 0
|
||||||
|
for p in paths:
|
||||||
|
if run_set_perm(role, p):
|
||||||
|
count += 1
|
||||||
|
print(f" {role}: {count}/{len(paths)} paths registered")
|
||||||
|
return count
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
print(f"Sage root: {SAGE_ROOT}")
|
||||||
|
total = 0
|
||||||
|
total += register_role_paths("any", PATHS_ANY)
|
||||||
|
total += register_role_paths("logined", PATHS_LOGINED)
|
||||||
|
print(f"\nDone. Total {total} permission entries registered.")
|
||||||
|
print("NOTE: Restart Sage after permission changes to reload RBAC cache.")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user