From 1fc2be73c08fa31a9f03d459ab2a5002015be4f0 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Tue, 5 May 2026 14:08:49 +0800 Subject: [PATCH] fix: init_permissions.py - use config.json databases for DBPools, add id for rolepermission insert 1. Replace manual DB_CONFIG with getConfig('.').databases for DBPools init 2. Add id=getID() when inserting into rolepermission table (id has no default value) 3. Sync app/ and root copies --- app/init_permissions.py | 27 ++++++++++----------------- init_permissions.py | 27 ++++++++++----------------- 2 files changed, 20 insertions(+), 34 deletions(-) diff --git a/app/init_permissions.py b/app/init_permissions.py index 95130c5..fad8f31 100644 --- a/app/init_permissions.py +++ b/app/init_permissions.py @@ -35,16 +35,6 @@ import importlib.util APP_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, APP_ROOT) -# 数据库配置(与 conf/config.json 一致) -DB_CONFIG = { - "host": "localhost", - "port": 3306, - "user": "hermes", - "password": "hermes123", - "db": "crm_db", - "charset": "utf8mb4", -} - # perm_config.py 路径 PERM_CONFIG_PATH = os.path.join(APP_ROOT, "perm_config.py") @@ -173,7 +163,9 @@ async def grant_perm(sor, role_id, perm_id): """授予角色权限(幂等)。""" existing = await sor.R('rolepermission', {'roleid': role_id, 'permid': perm_id}) if not existing: + from appPublic.uniqueID import getID await sor.C('rolepermission', { + 'id': getID(), 'roleid': role_id, 'permid': perm_id, }) @@ -253,16 +245,17 @@ async def main(): path_role_map = build_path_role_map(pc, all_files) print(f"\n[3/6] 路径-角色映射: {len(path_role_map)} 个路径需要授权") - # [4] 连接数据库 + 注册所有角色 + # [4] 连接数据库:用 config.json 中的 databases 初始化 DBPools + from appPublic.jsonConfig import getConfig from sqlor.dbpools import DBPools + config = getConfig('.') + DBPools(config.databases) db = DBPools() - db.addDatabase('crm_db', { - 'driver': 'aiomysql', - 'kwargs': DB_CONFIG, - }) - print(f"\n[4/6] 连接数据库 {DB_CONFIG['db']}...") + # 获取数据库名(config.json databases 中定义的第一个/默认数据库名) + dbname = list(config.databases.keys())[0] + print(f"\n[4/6] 连接数据库 {dbname}...") - async with db.sqlorContext('crm_db') as sor: + async with db.sqlorContext(dbname) as sor: # ---- 4a: 创建/验证所有角色 ---- # 关键:所有角色使用 orgtypeid='*' 创建,配合 RBAC 通配机制 diff --git a/init_permissions.py b/init_permissions.py index 95130c5..fad8f31 100644 --- a/init_permissions.py +++ b/init_permissions.py @@ -35,16 +35,6 @@ import importlib.util APP_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, APP_ROOT) -# 数据库配置(与 conf/config.json 一致) -DB_CONFIG = { - "host": "localhost", - "port": 3306, - "user": "hermes", - "password": "hermes123", - "db": "crm_db", - "charset": "utf8mb4", -} - # perm_config.py 路径 PERM_CONFIG_PATH = os.path.join(APP_ROOT, "perm_config.py") @@ -173,7 +163,9 @@ async def grant_perm(sor, role_id, perm_id): """授予角色权限(幂等)。""" existing = await sor.R('rolepermission', {'roleid': role_id, 'permid': perm_id}) if not existing: + from appPublic.uniqueID import getID await sor.C('rolepermission', { + 'id': getID(), 'roleid': role_id, 'permid': perm_id, }) @@ -253,16 +245,17 @@ async def main(): path_role_map = build_path_role_map(pc, all_files) print(f"\n[3/6] 路径-角色映射: {len(path_role_map)} 个路径需要授权") - # [4] 连接数据库 + 注册所有角色 + # [4] 连接数据库:用 config.json 中的 databases 初始化 DBPools + from appPublic.jsonConfig import getConfig from sqlor.dbpools import DBPools + config = getConfig('.') + DBPools(config.databases) db = DBPools() - db.addDatabase('crm_db', { - 'driver': 'aiomysql', - 'kwargs': DB_CONFIG, - }) - print(f"\n[4/6] 连接数据库 {DB_CONFIG['db']}...") + # 获取数据库名(config.json databases 中定义的第一个/默认数据库名) + dbname = list(config.databases.keys())[0] + print(f"\n[4/6] 连接数据库 {dbname}...") - async with db.sqlorContext('crm_db') as sor: + async with db.sqlorContext(dbname) as sor: # ---- 4a: 创建/验证所有角色 ---- # 关键:所有角色使用 orgtypeid='*' 创建,配合 RBAC 通配机制