69 lines
2.1 KiB
Python
69 lines
2.1 KiB
Python
#!/usr/bin/env python3
|
|
"""pcc RBAC 权限管理"""
|
|
import subprocess, os, sys, json, glob
|
|
|
|
def find_sage_root():
|
|
for c in [os.path.expanduser("~/sage"), os.path.expanduser("~/repos/sage")]:
|
|
if os.path.isdir(os.path.join(c, "py3")): return c
|
|
return None
|
|
|
|
SAGE = find_sage_root()
|
|
if not SAGE: sys.exit("Sage root not found")
|
|
PY = os.path.join(SAGE, "py3", "bin", "python")
|
|
SET = os.path.join(SAGE, "set_role_perm.py")
|
|
|
|
JSON_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "json")
|
|
API_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "wwwroot", "api")
|
|
|
|
def load_cruds():
|
|
defs = []
|
|
for f in sorted(glob.glob(os.path.join(JSON_DIR, "*.json"))):
|
|
try:
|
|
d = json.load(open(f))
|
|
defs.append({"tblname": d["tblname"], "alias": d.get("alias", d["tblname"]), "subtables": d.get("params", {}).get("subtables", [])})
|
|
except: pass
|
|
return defs
|
|
|
|
def get_apis():
|
|
if not os.path.isdir(API_DIR): return []
|
|
return [f"/pcc/api/{f}" for f in sorted(os.listdir(API_DIR)) if f.endswith(".dspy")]
|
|
|
|
cruds = load_cruds()
|
|
apis = get_apis()
|
|
|
|
PATHS_ANY = [
|
|
f"/pcc/menu.ui",
|
|
]
|
|
PATHS_LOGINED = [
|
|
f"/pcc",
|
|
f"/pcc/index.ui",
|
|
]
|
|
for d in cruds:
|
|
PATHS_ANY.append(f"/pcc/{d['alias']}")
|
|
PATHS_LOGINED.append(f"/pcc/{d['alias']}/index.ui")
|
|
for act in ["get", "add", "update", "delete"]:
|
|
PATHS_LOGINED.append(f"/pcc/{d['alias']}/{act}_{d['tblname']}.dspy")
|
|
for api in apis:
|
|
PATHS_LOGINED.append(api)
|
|
|
|
PATHS_OPERATOR = list(PATHS_LOGINED)
|
|
|
|
PATHS_ANY = list(dict.fromkeys(PATHS_ANY))
|
|
PATHS_LOGINED = list(dict.fromkeys(PATHS_LOGINED))
|
|
PATHS_OPERATOR = list(dict.fromkeys(PATHS_OPERATOR))
|
|
|
|
def reg(role, paths):
|
|
ok = 0
|
|
for p in paths:
|
|
r = subprocess.run([PY, SET, role, p], capture_output=True, text=True)
|
|
if r.returncode == 0: ok += 1
|
|
print(f" {role}: {ok}/{len(paths)}")
|
|
return ok
|
|
|
|
total = 0
|
|
print(f"{mod_name}: any={len(PATHS_ANY)} logined={len(PATHS_LOGINED)} operator={len(PATHS_OPERATOR)}")
|
|
total += reg("any", PATHS_ANY)
|
|
total += reg("logined", PATHS_LOGINED)
|
|
total += reg("reseller.operator", PATHS_OPERATOR)
|
|
print(f"Done. {total} entries.")
|