diff --git a/app/api/endpoints/history.py b/app/api/endpoints/history.py index 64d9a0756..9541135ce 100644 --- a/app/api/endpoints/history.py +++ b/app/api/endpoints/history.py @@ -261,6 +261,42 @@ def _submit_batch_ai_redo( ) +def _resolve_download_history_username(token: _SchemaTokenPayload) -> Optional[str]: + """ + 解析下载历史查询应限定的用户名;管理员和下载管理权限用户返回 None 以查看全局记录。 + + :param token: 已校验的访问令牌载荷 + :return: 限定查询的用户名;允许全局查询时返回 None + """ + if token.agent_authorization_id: + from app.application.security.authorization import get_channel_authorization + + authorization = get_channel_authorization() + if authorization is None: + raise HTTPException(status_code=503, detail="渠道授权服务不可用") + user = authorization.authorize_operation(token.agent_authorization_id, token.agent_operation or "") + return None if user.is_superuser else user.name + if token.super_user: + return None + if token.sub is not None: + from app.application.security.user import get_configured_user_id_lookup + from app.domain.authorization import has_web_permission + + web_user = get_configured_user_id_lookup()(int(token.sub)) + if web_user is None or not web_user.is_active: + raise HTTPException(status_code=401, detail="用户未激活") + if web_user.is_superuser or has_web_permission(False, web_user.permissions, "manage", "manage.downloading"): + return None + if has_web_permission(False, web_user.permissions, "search", "search.resource") or has_web_permission( + False, web_user.permissions, "subscribe" + ): + return web_user.name + raise HTTPException(status_code=403, detail="用户权限不足") + if token.username: + return token.username + raise HTTPException(status_code=403, detail="用户权限不足") + + @router.get( "/download", summary="查询下载历史记录", @@ -279,46 +315,14 @@ async def download_history( """ page = page if page is not None else 1 count = count if count is not None else 30 - if isinstance(_, _SchemaTokenPayload) and _.agent_authorization_id: - from app.application.security.authorization import get_channel_authorization + if isinstance(_, _SchemaTokenPayload) and (_.agent_authorization_id or not _.super_user): from app.runtime.execution import run_in_threadpool - authorization = get_channel_authorization() - if authorization is None: - raise HTTPException(status_code=503, detail="渠道授权服务不可用") - user = await run_in_threadpool(authorization.authorize_operation, _.agent_authorization_id, _.agent_operation or "") - username = None if user.is_superuser else user.name + username = await run_in_threadpool(_resolve_download_history_username, _) results = await query.list_download(page=page, count=count, username=username) if response is not None: response.headers[COLLECTION_TOTAL_HEADER] = str(await query.count_download(username=username)) return results - if isinstance(_, _SchemaTokenPayload) and not _.super_user: - from app.application.security.user import get_configured_user_id_lookup - from app.domain.authorization import has_web_permission - from app.runtime.execution import run_in_threadpool - - web_username: Optional[str] = None - if _.sub is not None: - lookup = get_configured_user_id_lookup() - web_user = await run_in_threadpool(lookup, int(_.sub)) - if web_user is None or not web_user.is_active: - raise HTTPException(status_code=401, detail="用户未激活") - if web_user.is_superuser or has_web_permission(False, web_user.permissions, "manage", "manage.downloading"): - web_username = None - elif has_web_permission(False, web_user.permissions, "search", "search.resource") or has_web_permission( - False, web_user.permissions, "subscribe" - ): - web_username = web_user.name - else: - raise HTTPException(status_code=403, detail="用户权限不足") - elif _.username: - web_username = _.username - else: - raise HTTPException(status_code=403, detail="用户权限不足") - results = await query.list_download(page=page, count=count, username=web_username) - if response is not None: - response.headers[COLLECTION_TOTAL_HEADER] = str(await query.count_download(username=web_username)) - return results results = await query.list_download(page=page, count=count) if response is not None: response.headers[COLLECTION_TOTAL_HEADER] = str(await query.count_download()) diff --git a/docs/architecture-overview.md b/docs/architecture-overview.md index 114633e64..415d6ef04 100644 --- a/docs/architecture-overview.md +++ b/docs/architecture-overview.md @@ -851,7 +851,7 @@ flowchart LR | 指标 | 当前值 | |---|---:| | Python 模块 | 1194 | -| 内部导入边 | 10,510 | +| 内部导入边 | 10,524 | | 非平凡 SCC | 1(29 个模块的 TMDB vendor 精确 containment) | | Application / Chain 具体 Adapter 直连 | 0 / 0 | | Direct egress | 52(精确 policy 准入,Application / Chain 均为 0;本知识 Adapter 仍复用宿主 HTTP 层) | diff --git a/tests/test_web_agent_permissions.py b/tests/test_web_agent_permissions.py index 8df887615..2a2f6520a 100644 --- a/tests/test_web_agent_permissions.py +++ b/tests/test_web_agent_permissions.py @@ -15,7 +15,6 @@ from starlette.responses import Response import app.agent.orchestrator as orchestrator_module from app.agent.middleware.jobs import JobsMiddleware from app.agent.middleware.policy import AgentPolicyMiddleware -from app.agent.orchestrator import MoviePilotAgent from app.agent.policy.contracts import ( AuthSource, PrincipalRole,