66 lines
2.5 KiB
Python
66 lines
2.5 KiB
Python
#!/usr/bin/env python3
|
|
"""RAG Server RBAC 权限初始化 — 自动扫描 wwwroot, 幂等"""
|
|
import sys, os, asyncio, hashlib
|
|
|
|
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), '..'))
|
|
|
|
from sqlor.dbpools import DBPools
|
|
from appPublic.jsonConfig import getConfig
|
|
|
|
config = getConfig('.')
|
|
DBPools(config.databases)
|
|
|
|
BASE = os.path.dirname(os.path.abspath(__file__))
|
|
WWWROOT = os.path.join(BASE, '..', 'pkgs', 'rag', 'wwwroot')
|
|
|
|
PUBLIC = [
|
|
'/', '/index.ui', '/top.ui', '/user_menu.ui', '/shell_theme.css',
|
|
'/i18n_getmsgs', '/bricks/**', '/i18n/**', '/uapi/**',
|
|
'/rbac/user/login.ui', '/rbac/user/register.ui',
|
|
'/rbac/user/login.dspy', '/rbac/user/register.dspy', '/rbac/user/logout.dspy',
|
|
'/rbac/**',
|
|
'/api/status', '/api/engines', '/api/kb/list',
|
|
]
|
|
LOGINED = [
|
|
'/api/search', '/api/doc/upload', '/api/doc/delete',
|
|
'/api/dir/create', '/api/dir/delete', '/api/dir/list',
|
|
'/api/tag/create', '/api/tag/list', '/api/tag/delete',
|
|
'/api/tag/assign', '/api/tag/unassign', '/api/tag/media_tags', '/api/tag/search',
|
|
]
|
|
|
|
def pid(path):
|
|
return 'p_' + hashlib.md5(path.encode()).hexdigest()[:12]
|
|
|
|
def discover_paths(root, prefix='/rag'):
|
|
paths = set()
|
|
for dirpath, _, filenames in os.walk(root):
|
|
rel = dirpath[len(root):] + '/'
|
|
paths.add(prefix + rel)
|
|
for f in filenames:
|
|
if f.endswith(('.dspy', '.ui')):
|
|
paths.add(prefix + rel + f)
|
|
return sorted(paths)
|
|
|
|
async def main():
|
|
db = DBPools()
|
|
async with db.sqlorContext('rag') as sor:
|
|
for rid, rname in [('any', 'any'), ('logined', 'logined')]:
|
|
await sor.sqlExe(
|
|
f"INSERT IGNORE INTO role (id, orgtypeid, name) VALUES ('{rid}', '', '{rname}')", {})
|
|
|
|
for path in PUBLIC:
|
|
p = pid(path)
|
|
await sor.sqlExe(f"INSERT IGNORE INTO permission (id, path, name) VALUES ('{p}', '{path}', 'RAG')", {})
|
|
await sor.sqlExe(f"INSERT IGNORE INTO rolepermission (id, roleid, permid) VALUES ('rp_{p}', 'any', '{p}')", {})
|
|
|
|
all_pages = discover_paths(WWWROOT)
|
|
for path in LOGINED + all_pages:
|
|
p = pid(path)
|
|
await sor.sqlExe(f"INSERT IGNORE INTO permission (id, path, name) VALUES ('{p}', '{path}', 'RAG')", {})
|
|
await sor.sqlExe(f"INSERT IGNORE INTO rolepermission (id, roleid, permid) VALUES ('rp_{p}', 'logined', '{p}')", {})
|
|
|
|
print(f"OK: {len(PUBLIC)} public + {len(LOGINED) + len(all_pages)} logined")
|
|
|
|
if __name__ == '__main__':
|
|
asyncio.run(main())
|