sage/scripts/list_path_roles.py

93 lines
3.0 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 python
"""
列出指定路径拥有权限的所有角色。
用法:
./py3/bin/python list_path_roles.py <path>
示例:
./py3/bin/python list_path_roles.py /harnessed_agent/index.ui
./py3/bin/python list_path_roles.py /bricks/css/bricks.css
说明:
- 精确匹配路径
- 如果路径未注册,会提示
- 显示角色 id、orgtypeid、name
"""
import os
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
async def main():
if len(sys.argv) < 2:
print(f"用法: {sys.argv[0]} <path>")
print(f"示例: {sys.argv[0]} /harnessed_agent/index.ui")
sys.exit(1)
target_path = sys.argv[1]
if not target_path.startswith('/'):
target_path = '/' + target_path
config = getConfig('.')
db = DBPools(config.databases)
async with db.sqlorContext('sage') as sor:
# 1. 查找 permission 记录
perm_recs = await sor.R('permission', {'path': target_path})
if not perm_recs:
print(f"[未找到] 路径 '{target_path}' 未在 permission 表中注册。")
# 尝试 LIKE 模糊匹配
like_path = target_path.rstrip('/') + '/%'
like_recs = await sor.sqlExe("SELECT path FROM permission WHERE path LIKE %s", (like_path,))
if like_recs:
print(f"\n模糊匹配到 {len(like_recs)} 个子路径LIKE '{like_path}'")
for r in like_recs[:20]:
print(f" {r.path}")
if len(like_recs) > 20:
print(f" ... 共 {len(like_recs)}")
sys.exit(0)
perm = perm_recs[0]
print(f"路径: {perm.path}")
print(f"perm_id: {perm.id}")
# 2. 查找 rolepermission 关联
rp_recs = await sor.R('rolepermission', {'permid': perm.id})
if not rp_recs:
print(" [无角色] 该路径没有任何角色权限记录。")
sys.exit(0)
print(f"\n{len(rp_recs)} 个角色有权限:\n")
print(f"{'角色ID':<30} {'orgtypeid':<20} {'名称':<30}")
print("-" * 80)
for rp in rp_recs:
roleid = rp.roleid
# 特殊角色直接显示
if roleid in ('any', 'anonymous', 'logined'):
print(f"{roleid:<30} {'*':<20} {roleid:<30}")
continue
# 普通角色查 role 表
role_recs = await sor.R('role', {'id': roleid})
if role_recs:
r = role_recs[0]
orgtypeid = getattr(r, 'orgtypeid', '*')
name = getattr(r, 'name', roleid)
print(f"{roleid:<30} {orgtypeid:<20} {name:<30}")
else:
print(f"{roleid:<30} {'(未知)':<20} {'(role表中不存在)':<30}")
if __name__ == '__main__':
asyncio.get_event_loop().run_until_complete(main())