148 lines
4.8 KiB
Python
148 lines
4.8 KiB
Python
|
|
"""
|
|||
|
|
添加用户管理和角色管理菜单到数据库
|
|||
|
|
"""
|
|||
|
|
import sys
|
|||
|
|
import asyncio
|
|||
|
|
from pathlib import Path
|
|||
|
|
|
|||
|
|
# 添加项目根目录到 Python 路径
|
|||
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|||
|
|
|
|||
|
|
from sqlalchemy import text
|
|||
|
|
from app.core.database import async_session
|
|||
|
|
|
|||
|
|
|
|||
|
|
async def add_user_role_menus():
|
|||
|
|
"""添加用户管理和角色管理菜单"""
|
|||
|
|
print("正在添加用户管理和角色管理菜单...")
|
|||
|
|
|
|||
|
|
async with async_session() as session:
|
|||
|
|
# 1. 检查菜单是否已存在
|
|||
|
|
result = await session.execute(
|
|||
|
|
text("SELECT COUNT(*) FROM system_menus WHERE id IN (15, 16)")
|
|||
|
|
)
|
|||
|
|
count = result.scalar()
|
|||
|
|
|
|||
|
|
if count > 0:
|
|||
|
|
print(f" 菜单已存在({count}个),跳过添加")
|
|||
|
|
return
|
|||
|
|
|
|||
|
|
# 2. 插入用户管理菜单(ID: 15)
|
|||
|
|
await session.execute(
|
|||
|
|
text("""
|
|||
|
|
INSERT INTO system_menus (
|
|||
|
|
id, parent_id, menu_name, menu_code, menu_type,
|
|||
|
|
path, component, icon, sort_order, visible, status,
|
|||
|
|
created_at, updated_at
|
|||
|
|
) VALUES (
|
|||
|
|
15, 4, '用户管理', 'system:users', 1,
|
|||
|
|
'/users', 'UserManagement', 'UserOutlined',
|
|||
|
|
1, 1, 1, NOW(), NOW()
|
|||
|
|
)
|
|||
|
|
""")
|
|||
|
|
)
|
|||
|
|
print("✓ 用户管理菜单添加成功(ID: 15)")
|
|||
|
|
|
|||
|
|
# 3. 插入角色管理菜单(ID: 16)
|
|||
|
|
await session.execute(
|
|||
|
|
text("""
|
|||
|
|
INSERT INTO system_menus (
|
|||
|
|
id, parent_id, menu_name, menu_code, menu_type,
|
|||
|
|
path, component, icon, sort_order, visible, status,
|
|||
|
|
created_at, updated_at
|
|||
|
|
) VALUES (
|
|||
|
|
16, 4, '角色管理', 'system:roles', 1,
|
|||
|
|
'/roles', 'RoleManagement', 'TeamOutlined',
|
|||
|
|
2, 1, 1, NOW(), NOW()
|
|||
|
|
)
|
|||
|
|
""")
|
|||
|
|
)
|
|||
|
|
print("✓ 角色管理菜单添加成功(ID: 16)")
|
|||
|
|
|
|||
|
|
# 4. 更新已有菜单的sort_order,避免冲突
|
|||
|
|
await session.execute(
|
|||
|
|
text("""
|
|||
|
|
UPDATE system_menus
|
|||
|
|
SET sort_order = sort_order + 2
|
|||
|
|
WHERE parent_id = 4 AND id NOT IN (15, 16) AND sort_order >= 1
|
|||
|
|
""")
|
|||
|
|
)
|
|||
|
|
print("✓ 已调整其他子菜单的排序")
|
|||
|
|
|
|||
|
|
# 5. 为超级管理员角色分配这两个菜单权限
|
|||
|
|
result = await session.execute(
|
|||
|
|
text("SELECT id FROM roles WHERE role_code = 'super_admin'")
|
|||
|
|
)
|
|||
|
|
super_admin_role_id = result.scalar()
|
|||
|
|
|
|||
|
|
if super_admin_role_id:
|
|||
|
|
# 用户管理菜单
|
|||
|
|
await session.execute(
|
|||
|
|
text("""
|
|||
|
|
INSERT INTO role_menus (role_id, menu_id, created_at)
|
|||
|
|
VALUES (:role_id, 15, NOW())
|
|||
|
|
"""),
|
|||
|
|
{"role_id": super_admin_role_id}
|
|||
|
|
)
|
|||
|
|
# 角色管理菜单
|
|||
|
|
await session.execute(
|
|||
|
|
text("""
|
|||
|
|
INSERT INTO role_menus (role_id, menu_id, created_at)
|
|||
|
|
VALUES (:role_id, 16, NOW())
|
|||
|
|
"""),
|
|||
|
|
{"role_id": super_admin_role_id}
|
|||
|
|
)
|
|||
|
|
print(f"✓ 已为超级管理员角色分配权限")
|
|||
|
|
|
|||
|
|
# 6. 为管理员角色分配这两个菜单权限(如果存在)
|
|||
|
|
result = await session.execute(
|
|||
|
|
text("SELECT id FROM roles WHERE role_code = 'admin'")
|
|||
|
|
)
|
|||
|
|
admin_role_id = result.scalar()
|
|||
|
|
|
|||
|
|
if admin_role_id:
|
|||
|
|
# 用户管理菜单
|
|||
|
|
await session.execute(
|
|||
|
|
text("""
|
|||
|
|
INSERT INTO role_menus (role_id, menu_id, created_at)
|
|||
|
|
VALUES (:role_id, 15, NOW())
|
|||
|
|
"""),
|
|||
|
|
{"role_id": admin_role_id}
|
|||
|
|
)
|
|||
|
|
# 角色管理菜单
|
|||
|
|
await session.execute(
|
|||
|
|
text("""
|
|||
|
|
INSERT INTO role_menus (role_id, menu_id, created_at)
|
|||
|
|
VALUES (:role_id, 16, NOW())
|
|||
|
|
"""),
|
|||
|
|
{"role_id": admin_role_id}
|
|||
|
|
)
|
|||
|
|
print(f"✓ 已为管理员角色分配权限")
|
|||
|
|
|
|||
|
|
await session.commit()
|
|||
|
|
print("\n✓ 用户管理和角色管理菜单添加完成!")
|
|||
|
|
|
|||
|
|
|
|||
|
|
async def main():
|
|||
|
|
"""主函数"""
|
|||
|
|
print("=" * 80)
|
|||
|
|
print("添加用户管理和角色管理菜单")
|
|||
|
|
print("=" * 80)
|
|||
|
|
print()
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
await add_user_role_menus()
|
|||
|
|
print()
|
|||
|
|
print("=" * 80)
|
|||
|
|
print("✓ 操作完成!现在可以在系统管理菜单中访问用户管理和角色管理了")
|
|||
|
|
print("=" * 80)
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"\n✗ 操作失败: {str(e)}")
|
|||
|
|
import traceback
|
|||
|
|
traceback.print_exc()
|
|||
|
|
sys.exit(1)
|
|||
|
|
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
asyncio.run(main())
|