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
This commit is contained in:
yumoqing 2026-05-05 14:08:49 +08:00
parent 6255feaf5b
commit 1fc2be73c0
2 changed files with 20 additions and 34 deletions

View File

@ -35,16 +35,6 @@ import importlib.util
APP_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) APP_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, APP_ROOT) 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.py 路径
PERM_CONFIG_PATH = os.path.join(APP_ROOT, "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}) existing = await sor.R('rolepermission', {'roleid': role_id, 'permid': perm_id})
if not existing: if not existing:
from appPublic.uniqueID import getID
await sor.C('rolepermission', { await sor.C('rolepermission', {
'id': getID(),
'roleid': role_id, 'roleid': role_id,
'permid': perm_id, 'permid': perm_id,
}) })
@ -253,16 +245,17 @@ async def main():
path_role_map = build_path_role_map(pc, all_files) path_role_map = build_path_role_map(pc, all_files)
print(f"\n[3/6] 路径-角色映射: {len(path_role_map)} 个路径需要授权") 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 from sqlor.dbpools import DBPools
config = getConfig('.')
DBPools(config.databases)
db = DBPools() db = DBPools()
db.addDatabase('crm_db', { # 获取数据库名config.json databases 中定义的第一个/默认数据库名)
'driver': 'aiomysql', dbname = list(config.databases.keys())[0]
'kwargs': DB_CONFIG, print(f"\n[4/6] 连接数据库 {dbname}...")
})
print(f"\n[4/6] 连接数据库 {DB_CONFIG['db']}...")
async with db.sqlorContext('crm_db') as sor: async with db.sqlorContext(dbname) as sor:
# ---- 4a: 创建/验证所有角色 ---- # ---- 4a: 创建/验证所有角色 ----
# 关键:所有角色使用 orgtypeid='*' 创建,配合 RBAC 通配机制 # 关键:所有角色使用 orgtypeid='*' 创建,配合 RBAC 通配机制

View File

@ -35,16 +35,6 @@ import importlib.util
APP_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) APP_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, APP_ROOT) 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.py 路径
PERM_CONFIG_PATH = os.path.join(APP_ROOT, "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}) existing = await sor.R('rolepermission', {'roleid': role_id, 'permid': perm_id})
if not existing: if not existing:
from appPublic.uniqueID import getID
await sor.C('rolepermission', { await sor.C('rolepermission', {
'id': getID(),
'roleid': role_id, 'roleid': role_id,
'permid': perm_id, 'permid': perm_id,
}) })
@ -253,16 +245,17 @@ async def main():
path_role_map = build_path_role_map(pc, all_files) path_role_map = build_path_role_map(pc, all_files)
print(f"\n[3/6] 路径-角色映射: {len(path_role_map)} 个路径需要授权") 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 from sqlor.dbpools import DBPools
config = getConfig('.')
DBPools(config.databases)
db = DBPools() db = DBPools()
db.addDatabase('crm_db', { # 获取数据库名config.json databases 中定义的第一个/默认数据库名)
'driver': 'aiomysql', dbname = list(config.databases.keys())[0]
'kwargs': DB_CONFIG, print(f"\n[4/6] 连接数据库 {dbname}...")
})
print(f"\n[4/6] 连接数据库 {DB_CONFIG['db']}...")
async with db.sqlorContext('crm_db') as sor: async with db.sqlorContext(dbname) as sor:
# ---- 4a: 创建/验证所有角色 ---- # ---- 4a: 创建/验证所有角色 ----
# 关键:所有角色使用 orgtypeid='*' 创建,配合 RBAC 通配机制 # 关键:所有角色使用 orgtypeid='*' 创建,配合 RBAC 通配机制