Compare commits

..

No commits in common. "895573aaad1c31dda4e123c5809c9aaf0e1a5cb4" and "1fedd68a52dc0742d17f103968cdb9754430599b" have entirely different histories.

View File

@ -1,38 +1,84 @@
#!/usr/bin/env python3 # -*- coding:utf-8 -*-
"""RAG Server RBAC 权限初始化 — 直接 SQL""" """RAG Server RBAC 权限初始化"""
import sys, os, asyncio import sys, os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'pkgs', 'rag-pipeline'))
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), '..')) from ahserver.serverenv import ServerEnv
from sqlor.dbpools import DBPools
from appPublic.jsonConfig import getConfig from appPublic.jsonConfig import getConfig
from sqlor.dbpools import DBPools
import asyncio
config = getConfig('.') config = getConfig('.')
DBPools(config.databases) DBPools(config.databases)
PUBLIC = ['/api/status', '/api/engines', '/api/kb/list', '/', '/index.ui', '/bricks/**'] from rbac.userperm import UserPerm
LOGINED = ['/api/search', '/api/ingest',
'/knowledge_bases_list/', '/documents_list/', PUBLIC_PATHS = [
'/engine_configs_list/', '/subscriptions_list/'] '/api/status',
'/api/engines',
'/api/kb/list',
'/',
'/index.ui',
]
LOGINED_PATHS = [
'/api/search',
'/api/ingest',
'/knowledge_bases_list/',
'/documents_list/',
'/engine_configs_list/',
'/subscriptions_list/',
]
async def main(): async def init_perms():
db = DBPools() sor = await DBPools().get_sor_context('rag')
async with db.sqlorContext('rag') as sor: up = UserPerm()
for rid, rname in [('any', 'any'), ('logined', 'logined')]: await up.init(sor)
await sor.sqlExe(
f"INSERT IGNORE INTO role (id, orgtypeid, name) VALUES ('{rid}', '', '{rname}')", {})
for paths, role in [(PUBLIC, 'any'), (LOGINED, 'logined')]: # Create 'any' role for public access
for path in paths: recs = await sor.sqlExe("SELECT id FROM role WHERE id='any'", {})
pid = f"perm_{path.replace('/', '_')}" if not recs:
await sor.sqlExe( await sor.sqlExe(
f"INSERT IGNORE INTO permission (id, path, name) VALUES ('{pid}', '{path}', 'RAG')", {}) "INSERT INTO role (id, rolename, orgtypeid, roletype, del_flg) "
await sor.sqlExe( "VALUES ('any', 'any', '', 'any', '0')", {}
f"INSERT IGNORE INTO rolepermission (id, roleid, permid) VALUES ('rp_{pid}', '{role}', '{pid}')", {}) )
print(f"OK: {len(PUBLIC)} public + {len(LOGINED)} logined paths") # Create 'logined' role
recs = await sor.sqlExe("SELECT id FROM role WHERE id='logined'", {})
if not recs:
await sor.sqlExe(
"INSERT INTO role (id, rolename, orgtypeid, roletype, del_flg) "
"VALUES ('logined', 'logined', '', 'logined', '0')", {}
)
# Register public paths
for path in PUBLIC_PATHS:
perm_id = f"perm_{path.replace('/', '_')}"
await sor.sqlExe(
f"INSERT IGNORE INTO permission (id, path, name) VALUES ('{perm_id}', '{path}', 'RAG {path}')",
{}
)
await sor.sqlExe(
f"INSERT IGNORE INTO rolepermission (role_id, permission_id) VALUES ('any', '{perm_id}')",
{}
)
# Register logined paths
for path in LOGINED_PATHS:
perm_id = f"perm_{path.replace('/', '_')}"
await sor.sqlExe(
f"INSERT IGNORE INTO permission (id, path, name) VALUES ('{perm_id}', '{path}', 'RAG {path}')",
{}
)
await sor.sqlExe(
f"INSERT IGNORE INTO rolepermission (role_id, permission_id) VALUES ('logined', '{perm_id}')",
{}
)
print(f"RBAC initialized: {len(PUBLIC_PATHS)} public + {len(LOGINED_PATHS)} logined paths")
if __name__ == '__main__': if __name__ == '__main__':
asyncio.run(main()) asyncio.run(init_perms())