门户估价对话、会员与智能体账号,以及生产 Docker 部署一直停在工作区,远端主分支还没有这些改动。 Co-authored-by: Cursor <cursoragent@cursor.com>
100 lines
3.2 KiB
Python
100 lines
3.2 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
|
from pydantic import BaseModel
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.core.deps import require_staff
|
|
from app.db import get_db
|
|
from app.models.agent_user import (
|
|
create_agent_user,
|
|
delete_agent_record,
|
|
get_agent_record,
|
|
query_agent_users,
|
|
update_agent_record,
|
|
)
|
|
from app.services.password import hash_password, validate_password
|
|
|
|
router = APIRouter(dependencies=[Depends(require_staff)])
|
|
|
|
|
|
class AgentUserCreate(BaseModel):
|
|
username: str
|
|
displayName: str
|
|
password: str
|
|
status: str = "active"
|
|
|
|
|
|
class AgentUserUpdate(BaseModel):
|
|
displayName: str
|
|
status: str
|
|
password: str | None = None
|
|
|
|
|
|
def _password_hash(password: str | None, *, required: bool) -> str | None:
|
|
text_value = (password or "").strip()
|
|
if not text_value:
|
|
if required:
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="请填写密码")
|
|
return None
|
|
validate_password(text_value)
|
|
return hash_password(text_value)
|
|
|
|
|
|
@router.get("/agent-users")
|
|
def list_agent_users(
|
|
current: int = Query(1),
|
|
size: int = Query(20),
|
|
username: str | None = None,
|
|
db: Session = Depends(get_db),
|
|
) -> dict:
|
|
return query_agent_users(db, current=current, size=size, username=username)
|
|
|
|
|
|
@router.post("/agent-users", status_code=status.HTTP_201_CREATED)
|
|
def add_agent_user(body: AgentUserCreate, db: Session = Depends(get_db)) -> dict:
|
|
password_hash = _password_hash(body.password, required=True)
|
|
try:
|
|
return create_agent_user(
|
|
db,
|
|
username=body.username,
|
|
password_hash=password_hash or "",
|
|
display_name=body.displayName,
|
|
status=body.status,
|
|
)
|
|
except ValueError as exc:
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc)) from exc
|
|
|
|
|
|
@router.get("/agent-users/{user_key}")
|
|
def read_agent_user(user_key: str, db: Session = Depends(get_db)) -> dict:
|
|
try:
|
|
return get_agent_record(db, user_key)
|
|
except LookupError as exc:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(exc)) from exc
|
|
|
|
|
|
@router.put("/agent-users/{user_key}")
|
|
def write_agent_user(user_key: str, body: AgentUserUpdate, db: Session = Depends(get_db)) -> dict:
|
|
try:
|
|
return update_agent_record(
|
|
db,
|
|
user_key,
|
|
display_name=body.displayName,
|
|
status=body.status,
|
|
password_hash=_password_hash(body.password, required=False),
|
|
)
|
|
except LookupError as exc:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(exc)) from exc
|
|
except ValueError as exc:
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc)) from exc
|
|
|
|
|
|
@router.delete("/agent-users/{user_key}")
|
|
def remove_agent_user(user_key: str, db: Session = Depends(get_db)) -> dict:
|
|
try:
|
|
delete_agent_record(db, user_key)
|
|
except LookupError as exc:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(exc)) from exc
|
|
except ValueError as exc:
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc)) from exc
|
|
return {"ok": True}
|