Files
syops/backend/fix_agent_permission.py
T

130 lines
4.3 KiB
Python

#!/usr/bin/env python3
"""
fix_agent_permission.py
文件用途:修复 Agent 权限问题
关键逻辑:
1. 根据 username 或 user_id 获取用户
2. 给用户分配指定的 agent 权限
3. 支持批量分配和查看当前权限
语法实现:使用 SQLAlchemy relationship 操作多对多关联
"""
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from app import create_app, db
from app.models import User, Agent
def grant_agent_permission(username=None, user_id=None, agent_ids=None, grant_all=False):
"""
给用户分配 Agent 权限
参数:
username: 用户名
user_id: 用户 ID
agent_ids: 要分配的 Agent ID 列表,如 [1, 2, 3]
grant_all: 是否授予所有 Agent 的权限
"""
env = os.getenv('FLASK_ENV', 'production')
app = create_app(env)
with app.app_context():
user = None
if user_id:
user = User.query.get(int(user_id))
elif username:
user = User.query.filter_by(username=username).first()
if not user:
print(f"❌ 用户不存在: username={username}, user_id={user_id}")
return False
print(f"✅ 找到用户: {user.username} (ID: {user.id}, Role: {user.role})")
if grant_all:
agents = Agent.query.all()
agent_ids = [a.id for a in agents]
print(f"📋 所有可用 Agent: {agent_ids}")
if not agent_ids:
print("⚠️ 未指定 agent_ids,使用 --grant-all 授予所有权限")
agents = Agent.query.all()
user.agents = agents
else:
agents = Agent.query.filter(Agent.id.in_(agent_ids)).all()
if not agents:
print(f"❌ 未找到 ID 为 {agent_ids} 的 Agent")
return False
user.agents = agents
db.session.commit()
print(f"✅ 权限授予成功!用户 {user.username} 现在可以访问 Agent: {[a.id for a in user.agents]}")
return True
def list_user_permissions(username=None, user_id=None):
"""查看用户的 Agent 权限"""
env = os.getenv('FLASK_ENV', 'production')
app = create_app(env)
with app.app_context():
user = None
if user_id:
user = User.query.get(int(user_id))
elif username:
user = User.query.filter_by(username=username).first()
if not user:
print(f"❌ 用户不存在: username={username}, user_id={user_id}")
return
print(f"\n👤 用户: {user.username} (ID: {user.id}, Role: {user.role})")
print(f"📋 当前可访问的 Agent: {[a.id for a in user.agents]}")
print("\n📋 所有 Agent 列表:")
all_agents = Agent.query.all()
for a in all_agents:
has_access = a in user.agents
print(f" [{a.id}] {a.name} - {'✅ 有权限' if has_access else '❌ 无权限'}")
def list_all_users():
"""列出所有用户及其权限"""
env = os.getenv('FLASK_ENV', 'production')
app = create_app(env)
with app.app_context():
users = User.query.all()
print("\n👥 所有用户列表:")
for u in users:
print(f" [{u.id}] {u.username} (Role: {u.role}) - Agents: {[a.id for a in u.agents]}")
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='修复 Agent 权限')
parser.add_argument('--username', '-u', type=str, help='用户名')
parser.add_argument('--user-id', '-i', type=int, help='用户 ID')
parser.add_argument('--agent-ids', '-a', type=int, nargs='+', help='Agent ID 列表,如: 1 2 3')
parser.add_argument('--grant-all', '-g', action='store_true', help='授予所有 Agent 权限')
parser.add_argument('--list', '-l', action='store_true', help='查看用户权限')
parser.add_argument('--list-users', action='store_true', help='列出所有用户')
args = parser.parse_args()
if args.list_users:
list_all_users()
elif args.list:
list_user_permissions(username=args.username, user_id=args.user_id)
elif args.grant_all or args.agent_ids:
grant_agent_permission(
username=args.username,
user_id=args.user_id,
agent_ids=args.agent_ids,
grant_all=args.grant_all
)
else:
parser.print_help()