#!/usr/bin/env python3 """ discount 模块 RBAC 权限管理脚本 使用方法: cd ~/repos/sage ./py3/bin/python ~/repos/discount/scripts/load_path.py 每次代码变更如有新 path 出现,需同步更新此脚本。 路径分类: - any: 静态资源/菜单/CRUD别名目录 - logined: 需要认证的页面和 API - reseller.operator: 运营角色 — 折扣方案、营销方案、客户归属管理 - reseller.sale: 销售角色 — 促销码、客户归属查看 """ import subprocess import os import sys def find_sage_root(): candidates = [ os.path.expanduser("~/repos/sage"), os.path.expanduser("~/sage"), os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))), ] for c in candidates: 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 directory") sys.exit(1) PYTHON = os.path.join(SAGE_ROOT, "py3", "bin", "python") SET_PERM_SCRIPT = os.path.join(SAGE_ROOT, "set_role_perm.py") if not os.path.exists(SET_PERM_SCRIPT): print(f"ERROR: set_role_perm.py not found at {SET_PERM_SCRIPT}") sys.exit(1) MOD = "discount" # ============================================================ # 权限路径定义 — 每次新增页面或API时同步更新 # ============================================================ # any — 无需登录(菜单、CRUD别名目录) PATHS_ANY = [ f"/{MOD}/menu.ui", # CRUD alias directories f"/{MOD}/discount_list", f"/{MOD}/discount_detail_list", f"/{MOD}/discount_marketing_list", f"/{MOD}/discount_promo_code_list", f"/{MOD}/discount_customer_bind_list", ] # logined — 需要认证的页面和 API PATHS_LOGINED = [ # 模块入口 f"/{MOD}", f"/{MOD}/index.ui", # 功能页面 f"/{MOD}/generate_qr.dspy", f"/{MOD}/promote", f"/{MOD}/promote.ui", f"/{MOD}/promote/index.dspy", # CRUD 列表页 f"/{MOD}/discount_list/index.ui", f"/{MOD}/discount_detail_list/index.ui", f"/{MOD}/discount_marketing_list/index.ui", f"/{MOD}/discount_promo_code_list/index.ui", f"/{MOD}/discount_customer_bind_list/index.ui", # CRUD auto-generated .dspy — discount f"/{MOD}/discount_list/get_discount.dspy", f"/{MOD}/discount_list/add_discount.dspy", f"/{MOD}/discount_list/update_discount.dspy", f"/{MOD}/discount_list/delete_discount.dspy", # CRUD auto-generated .dspy — discount_detail f"/{MOD}/discount_detail_list/get_discount_detail.dspy", f"/{MOD}/discount_detail_list/add_discount_detail.dspy", f"/{MOD}/discount_detail_list/update_discount_detail.dspy", f"/{MOD}/discount_detail_list/delete_discount_detail.dspy", # CRUD auto-generated .dspy — discount_marketing f"/{MOD}/discount_marketing_list/get_discount_marketing.dspy", f"/{MOD}/discount_marketing_list/add_discount_marketing.dspy", f"/{MOD}/discount_marketing_list/update_discount_marketing.dspy", f"/{MOD}/discount_marketing_list/delete_discount_marketing.dspy", # CRUD auto-generated .dspy — discount_promo_code f"/{MOD}/discount_promo_code_list/get_discount_promo_code.dspy", f"/{MOD}/discount_promo_code_list/add_discount_promo_code.dspy", f"/{MOD}/discount_promo_code_list/update_discount_promo_code.dspy", f"/{MOD}/discount_promo_code_list/delete_discount_promo_code.dspy", # CRUD auto-generated .dspy — discount_customer_bind f"/{MOD}/discount_customer_bind_list/get_discount_customer_bind.dspy", f"/{MOD}/discount_customer_bind_list/add_discount_customer_bind.dspy", f"/{MOD}/discount_customer_bind_list/update_discount_customer_bind.dspy", f"/{MOD}/discount_customer_bind_list/delete_discount_customer_bind.dspy", # CRUD API (json/ defined) f"/{MOD}/api/get_customer_list.dspy", f"/{MOD}/api/discount_create.dspy", f"/{MOD}/api/discount_update.dspy", f"/{MOD}/api/discount_delete.dspy", f"/{MOD}/api/discount_detail_create.dspy", f"/{MOD}/api/discount_detail_update.dspy", f"/{MOD}/api/discount_detail_delete.dspy", f"/{MOD}/api/marketing_create.dspy", f"/{MOD}/api/marketing_update.dspy", f"/{MOD}/api/marketing_delete.dspy", f"/{MOD}/api/promo_code_create.dspy", f"/{MOD}/api/promo_code_update.dspy", f"/{MOD}/api/promo_code_delete.dspy", f"/{MOD}/api/customer_bind_create.dspy", f"/{MOD}/api/customer_bind_update.dspy", f"/{MOD}/api/customer_bind_delete.dspy", ] # 角色专属权限 — operator 可管理折扣方案、营销方案、客户归属 PATHS_OPERATOR = [ # 折扣方案管理 f"/{MOD}/discount_list", f"/{MOD}/discount_list/index.ui", f"/{MOD}/discount_list/get_discount.dspy", f"/{MOD}/discount_list/add_discount.dspy", f"/{MOD}/discount_list/update_discount.dspy", f"/{MOD}/discount_list/delete_discount.dspy", # 折扣产品明细 f"/{MOD}/discount_detail_list", f"/{MOD}/discount_detail_list/index.ui", f"/{MOD}/discount_detail_list/get_discount_detail.dspy", f"/{MOD}/discount_detail_list/add_discount_detail.dspy", f"/{MOD}/discount_detail_list/update_discount_detail.dspy", f"/{MOD}/discount_detail_list/delete_discount_detail.dspy", # 营销方案管理 f"/{MOD}/discount_marketing_list", f"/{MOD}/discount_marketing_list/index.ui", f"/{MOD}/discount_marketing_list/get_discount_marketing.dspy", f"/{MOD}/discount_marketing_list/add_discount_marketing.dspy", f"/{MOD}/discount_marketing_list/update_discount_marketing.dspy", f"/{MOD}/discount_marketing_list/delete_discount_marketing.dspy", f"/{MOD}/api/marketing_create.dspy", f"/{MOD}/api/marketing_update.dspy", f"/{MOD}/api/marketing_delete.dspy", # 促销码管理 f"/{MOD}/discount_promo_code_list", f"/{MOD}/discount_promo_code_list/index.ui", f"/{MOD}/discount_promo_code_list/get_discount_promo_code.dspy", f"/{MOD}/discount_promo_code_list/add_discount_promo_code.dspy", f"/{MOD}/discount_promo_code_list/update_discount_promo_code.dspy", f"/{MOD}/discount_promo_code_list/delete_discount_promo_code.dspy", f"/{MOD}/api/promo_code_create.dspy", f"/{MOD}/api/promo_code_update.dspy", f"/{MOD}/api/promo_code_delete.dspy", # 客户归属管理 f"/{MOD}/discount_customer_bind_list", f"/{MOD}/discount_customer_bind_list/index.ui", f"/{MOD}/discount_customer_bind_list/get_discount_customer_bind.dspy", f"/{MOD}/discount_customer_bind_list/add_discount_customer_bind.dspy", f"/{MOD}/discount_customer_bind_list/update_discount_customer_bind.dspy", f"/{MOD}/discount_customer_bind_list/delete_discount_customer_bind.dspy", f"/{MOD}/api/customer_bind_create.dspy", f"/{MOD}/api/customer_bind_update.dspy", f"/{MOD}/api/customer_bind_delete.dspy", # 折扣方案和明细 API f"/{MOD}/api/get_customer_list.dspy", f"/{MOD}/api/discount_create.dspy", f"/{MOD}/api/discount_update.dspy", f"/{MOD}/api/discount_delete.dspy", f"/{MOD}/api/discount_detail_create.dspy", f"/{MOD}/api/discount_detail_update.dspy", f"/{MOD}/api/discount_detail_delete.dspy", ] # 角色专属权限 — sale 可查看促销码和客户归属 PATHS_SALE = [ # 促销码(生成+查看) f"/{MOD}/discount_promo_code_list", f"/{MOD}/discount_promo_code_list/index.ui", f"/{MOD}/discount_promo_code_list/get_discount_promo_code.dspy", f"/{MOD}/discount_promo_code_list/add_discount_promo_code.dspy", f"/{MOD}/discount_promo_code_list/update_discount_promo_code.dspy", f"/{MOD}/discount_promo_code_list/delete_discount_promo_code.dspy", f"/{MOD}/api/promo_code_create.dspy", f"/{MOD}/api/promo_code_update.dspy", f"/{MOD}/api/promo_code_delete.dspy", # 客户归属(查看+手动分配) f"/{MOD}/discount_customer_bind_list", f"/{MOD}/discount_customer_bind_list/index.ui", f"/{MOD}/discount_customer_bind_list/get_discount_customer_bind.dspy", f"/{MOD}/discount_customer_bind_list/add_discount_customer_bind.dspy", f"/{MOD}/discount_customer_bind_list/update_discount_customer_bind.dspy", f"/{MOD}/discount_customer_bind_list/delete_discount_customer_bind.dspy", f"/{MOD}/api/customer_bind_create.dspy", f"/{MOD}/api/customer_bind_update.dspy", f"/{MOD}/api/customer_bind_delete.dspy", ] # ============================================================ # 执行注册 # ============================================================ def run_set_perm(role, path, verbose=True): """Register a single permission path.""" cmd = [PYTHON, SET_PERM_SCRIPT, role, path] result = subprocess.run(cmd, capture_output=True, text=True) if verbose: output = result.stdout.strip() if output: print(f" {role}: {path} -> {output}") return result.returncode == 0 def register_role_paths(role, paths): """Register all paths for a role.""" count = 0 for path in paths: if run_set_perm(role, path, verbose=False): count += 1 print(f" {role}: {count}/{len(paths)} paths registered") return count def main(): print(f"Sage root: {SAGE_ROOT}") print(f"set_role_perm.py: {SET_PERM_SCRIPT}") print() total = 0 print("[1/4] Registering 'any' role paths...") total += register_role_paths("any", PATHS_ANY) print("[2/4] Registering 'logined' role paths...") total += register_role_paths("logined", PATHS_LOGINED) print("[3/4] Registering 'reseller.operator' role paths...") total += register_role_paths("reseller.operator", PATHS_OPERATOR) print("[4/4] Registering 'reseller.sale' role paths...") total += register_role_paths("reseller.sale", PATHS_SALE) print() print(f"Done. Total {total} permission entries registered.") print() print("NOTE: Restart Sage after permission changes to reload RBAC cache.") if __name__ == "__main__": main()