fix: v0.5.6 require_role 装饰器 signature + 3 个 schema 同步 migration
🛠️ Bug 修复: - backend/app/dependencies.py: 修 require_role 装饰器 问题:@wraps 让 FastAPI 看到 __wrapped__ 签名,Depends 默认值未被解析, current_user 实际是 Depends 对象 → 'Depends' object has no attribute 'roles' 修法:用 inspect 合并签名 + 手动设 wrapper.__signature__, 把 current_user 加进 FastAPI 看到的参数列表 影响:所有用 @require_role 的 endpoint 在生产都受影响,修后正常 📦 Dependencies: - backend/requirements.txt: pydantic 2.7.4 原因:2.7.5 被 PyPI yank,清华源不缓存,build 失败 (本次不进生产,但合并时一起跟) 🗃️ Alembic migrations(3 个,生产必跑): - 010_add_agent_otp: agents.otp_secret + agents.otp_enabled 背景:Agent 模型加了 OTP 字段但没建 migration,坐席登录报 'column agents.otp_secret does not exist' 字段:otp_secret VARCHAR(64) NULL, otp_enabled BOOLEAN DEFAULT false 安全:nullable + default,现有坐席不受影响 - 011_add_conversation_impact: conversations 3 个评估字段 背景:坐席发消息 500 报 'column conversations.impact_scope does not exist' 字段:impact_scope INT DEFAULT 0, is_blocking BOOL DEFAULT false, emotion_state VARCHAR(20) DEFAULT 'normal' 安全:都有 default,现有会话自动填默认值 - 012_sync_remaining_fields: 模型 vs DB 剩余漂移 背景:dev-check-schema-drift 找到 4 个 dev 模式下没暴露的字段 字段:conversations.dify_conversation_id VARCHAR(128) NULL, employees.it_level VARCHAR(20) DEFAULT 'silver', employees.it_level_source VARCHAR(20) DEFAULT 'system', employees.notes JSON DEFAULT '{}' 安全:都有 default,现有数据自动填默认值 部署: cd /app && python -m alembic upgrade head docker compose restart backend 验证:curl http://10.90.5.110:8000/health → 200
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
# 3. require_admin: 管理员权限验证
|
||||
# =============================================================================
|
||||
|
||||
import inspect
|
||||
import json
|
||||
import logging
|
||||
from dataclasses import dataclass
|
||||
@@ -225,12 +226,26 @@ def require_role(*required_roles: str):
|
||||
"""
|
||||
|
||||
def decorator(func):
|
||||
# 合并 func 签名 + current_user 参数,让 FastAPI 能正确解析 Depends
|
||||
# (v0.5.6 修复:之前用 @wraps,FastAPI 看到的是 __wrapped__ 的签名,
|
||||
# 没有 current_user,导致 Depends 默认值未被解析,current_user 实际是 Depends 对象)
|
||||
sig = inspect.signature(func)
|
||||
params = list(sig.parameters.values())
|
||||
params.append(
|
||||
inspect.Parameter(
|
||||
'current_user',
|
||||
inspect.Parameter.KEYWORD_ONLY,
|
||||
annotation=UserInfo,
|
||||
default=Depends(get_current_user),
|
||||
)
|
||||
)
|
||||
new_sig = sig.replace(parameters=params)
|
||||
|
||||
@wraps(func)
|
||||
async def wrapper(
|
||||
*args,
|
||||
current_user: UserInfo = Depends(get_current_user),
|
||||
**kwargs,
|
||||
):
|
||||
async def wrapper(*args, **kwargs):
|
||||
# FastAPI 已经把 current_user 注入了 kwargs
|
||||
current_user = kwargs.pop('current_user')
|
||||
|
||||
# 检查用户是否有任一所需角色
|
||||
user_roles = set(current_user.roles)
|
||||
required = set(required_roles)
|
||||
@@ -247,6 +262,8 @@ def require_role(*required_roles: str):
|
||||
|
||||
return await func(*args, current_user=current_user, **kwargs)
|
||||
|
||||
# 关键:让 FastAPI 用合并后的签名,这样它能看到 current_user 这个 Depends 参数
|
||||
wrapper.__signature__ = new_sig
|
||||
return wrapper
|
||||
|
||||
return decorator
|
||||
|
||||
Reference in New Issue
Block a user