96 lines
3.0 KiB
Python
96 lines
3.0 KiB
Python
"""
|
|
检查和修复角色的 is_system 字段
|
|
"""
|
|
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 check_and_fix_roles():
|
|
"""检查和修复角色的 is_system 字段"""
|
|
print("正在检查角色数据...")
|
|
|
|
async with async_session() as session:
|
|
# 查询所有角色
|
|
result = await session.execute(
|
|
text("SELECT id, role_name, role_code, is_system FROM roles ORDER BY id")
|
|
)
|
|
roles = result.fetchall()
|
|
|
|
print("\n当前角色列表:")
|
|
print("-" * 80)
|
|
print(f"{'ID':<5} {'角色名称':<20} {'角色编码':<20} {'是否系统角色':<15}")
|
|
print("-" * 80)
|
|
for role in roles:
|
|
is_system_text = "是" if role[3] == 1 else "否"
|
|
print(f"{role[0]:<5} {role[1]:<20} {role[2]:<20} {is_system_text:<15}")
|
|
print("-" * 80)
|
|
|
|
# 修复建议:只有 super_admin 应该是系统角色
|
|
print("\n开始修复角色 is_system 字段...")
|
|
|
|
# 将 super_admin 设置为系统角色
|
|
await session.execute(
|
|
text("UPDATE roles SET is_system = 1 WHERE role_code = 'super_admin'")
|
|
)
|
|
print("✓ 已将 super_admin 设置为系统角色")
|
|
|
|
# 将其他角色设置为非系统角色
|
|
await session.execute(
|
|
text("UPDATE roles SET is_system = 0 WHERE role_code != 'super_admin'")
|
|
)
|
|
print("✓ 已将其他角色设置为非系统角色")
|
|
|
|
await session.commit()
|
|
|
|
# 再次查询验证
|
|
result = await session.execute(
|
|
text("SELECT id, role_name, role_code, is_system FROM roles ORDER BY id")
|
|
)
|
|
roles = result.fetchall()
|
|
|
|
print("\n修复后的角色列表:")
|
|
print("-" * 80)
|
|
print(f"{'ID':<5} {'角色名称':<20} {'角色编码':<20} {'是否系统角色':<15}")
|
|
print("-" * 80)
|
|
for role in roles:
|
|
is_system_text = "是" if role[3] == 1 else "否"
|
|
print(f"{role[0]:<5} {role[1]:<20} {role[2]:<20} {is_system_text:<15}")
|
|
print("-" * 80)
|
|
|
|
print("\n✓ 角色数据修复完成!")
|
|
print("\n说明:")
|
|
print(" - super_admin (超级管理员): 系统角色,不允许修改权限")
|
|
print(" - admin (管理员): 非系统角色,可以修改权限")
|
|
print(" - user (普通用户): 非系统角色,可以修改权限")
|
|
|
|
|
|
async def main():
|
|
"""主函数"""
|
|
print("=" * 80)
|
|
print("检查和修复角色 is_system 字段")
|
|
print("=" * 80)
|
|
print()
|
|
|
|
try:
|
|
await check_and_fix_roles()
|
|
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())
|