world/scripts/load_path.py

75 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""world 模块 RBAC 权限注册脚本(显式路径,禁通配符)。
Usage: cd ~/repos/sage && ./py3/bin/python ~/repos/world/scripts/load_path.py
"""
import subprocess
import os
import sys
def find_sage_root():
for c in [os.path.expanduser('~/repos/sage'), os.path.expanduser('~/sage')]:
if os.path.isdir(os.path.join(c, 'py3')) and os.path.isdir(os.path.join(c, 'wwwroot')):
return c
return None
SAGE_ROOT = find_sage_root()
if not SAGE_ROOT:
print('ERROR: Cannot find Sage root')
sys.exit(1)
PYTHON = os.path.join(SAGE_ROOT, 'py3', 'bin', 'python')
SET_PERM_SCRIPT = os.path.join(SAGE_ROOT, 'set_role_perm.py')
MOD = 'world'
# 公开路径(无需登录)
PATHS_ANY = [
'/world/menu.ui',
]
# 登录路径(页面 + API + CRUD 自动生成目录,全部显式注册)
PATHS_LOGINED = [
'/world',
'/world/index.ui',
'/world/api/create_world.dspy',
'/world/api/world_update.dspy',
'/world/api/world_delete.dspy',
'/world/api/get_world.dspy',
'/world/api/list_worlds.dspy',
'/world/api/set_world_mode.dspy',
'/world/api/get_search_status.dspy',
'/world/api/get_search_world_type.dspy',
# CRUD 自动生成目录 world/xls2ui 依据 json/world.json 生成)
'/world/world',
'/world/world/index.ui',
'/world/world/get_world.dspy',
'/world/world/add_world.dspy',
'/world/world/update_world.dspy',
'/world/world/delete_world.dspy',
]
def run_set_perm(role, path):
try:
r = subprocess.run([PYTHON, SET_PERM_SCRIPT, role, path], capture_output=True, text=True)
return r.returncode == 0
except Exception as e:
print(' set_role_perm error for %s: %s' % (path, e))
return False
def register_role_paths(role, paths):
count = sum(1 for p in paths if run_set_perm(role, p))
print(' %s: %d/%d paths registered' % (role, count, len(paths)))
return count
def main():
total = register_role_paths('any', PATHS_ANY) + register_role_paths('logined', PATHS_LOGINED)
print('Done. Total %d permission entries registered.' % total)
if __name__ == '__main__':
main()