fix: update OPC routers to use request.state.user instead of Depends(_inject_user)

This commit is contained in:
Gan, Jimmy
2026-04-02 00:45:26 +08:00
parent 5cfb92cfc6
commit 6cffc1f9ff
3 changed files with 50 additions and 28 deletions
+16 -9
View File
@@ -1,11 +1,11 @@
"""
OPC Agents Router
"""
from fastapi import APIRouter, Depends, HTTPException
from fastapi import APIRouter, Depends, HTTPException, Request
from typing import Optional, List
from pydantic import BaseModel
from db import opc_db
from rbac import require_page, User, _inject_user, require_admin
from rbac import require_page, User, require_admin
router = APIRouter()
@@ -23,10 +23,11 @@ class ExecutionApproval(BaseModel):
@router.get("/agents")
async def list_agents(
request: Request,
enabled_only: bool = True,
user: User = Depends(_inject_user)
):
"""List all agents"""
user: User = request.state.user
agents = await opc_db.get_agents(enabled_only=enabled_only)
return {"items": agents}
@@ -34,9 +35,10 @@ async def list_agents(
@router.get("/agents/{agent_id}")
async def get_agent(
agent_id: str,
user: User = Depends(_inject_user)
request: Request,
):
"""Get agent details"""
user: User = request.state.user
agent = await opc_db.get_agent(agent_id)
if not agent:
raise HTTPException(status_code=404, detail="Agent not found")
@@ -47,9 +49,10 @@ async def get_agent(
async def update_agent(
agent_id: str,
updates: AgentUpdate,
user: User = Depends(_inject_user)
request: Request,
):
"""Update agent configuration (admin only)"""
user: User = request.state.user
# This would need an update_agent function in opc_db
raise HTTPException(status_code=501, detail="Not implemented yet")
@@ -58,9 +61,10 @@ async def update_agent(
async def trigger_agent(
agent_id: str,
task_id: int,
user: User = Depends(_inject_user)
request: Request,
):
"""Manually trigger agent execution"""
user: User = request.state.user
execution = await opc_db.create_agent_execution(
task_id=task_id,
agent_id=agent_id,
@@ -71,13 +75,14 @@ async def trigger_agent(
@router.get("/executions")
async def list_executions(
request: Request,
task_id: Optional[int] = None,
agent_id: Optional[str] = None,
status: Optional[str] = None,
limit: int = 50,
user: User = Depends(_inject_user)
):
"""List agent executions"""
user: User = request.state.user
executions = await opc_db.get_agent_executions(
task_id=task_id,
agent_id=agent_id,
@@ -90,9 +95,10 @@ async def list_executions(
@router.get("/executions/{execution_id}")
async def get_execution(
execution_id: int,
user: User = Depends(_inject_user)
request: Request,
):
"""Get execution details"""
user: User = request.state.user
executions = await opc_db.get_agent_executions(limit=1000)
execution = next((e for e in executions if e["id"] == execution_id), None)
if not execution:
@@ -104,9 +110,10 @@ async def get_execution(
async def approve_execution(
execution_id: int,
approval: ExecutionApproval,
user: User = Depends(_inject_user)
request: Request,
):
"""Approve or reject agent execution (for CxO level actions)"""
user: User = request.state.user
# This would need an approve_execution function in opc_db
# For now, return placeholder
return {