Files
beige/backend/app/api/v1/workspaces.py
yangqianqian 6a2632da70 baseline: Clover 独立仓库首次基线提交
将 Clover 从上层产品包旧仓库中独立出来,建立专属版本控制。
当前状态=纵切片端到端已打通(登录→选品→出文出图→审核→下载包),
M1文案质量去套路化已验收。此提交作为后续按核销清单逐条修复的基线。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-16 11:30:22 +08:00

44 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
app/api/v1/workspaces.py — workspace 切换路由
POST /workspaces/switch → /api/v1/workspaces/switch契约路径
从 auth.py 独立出来,避免 /auth 前缀错误。
"""
from typing import Annotated
from fastapi import APIRouter, Depends
from pydantic import BaseModel
from sqlalchemy.orm import Session
from app.core.security import create_access_token
from app.core.database import get_db
from app.core.response import ok, raise_forbidden
from app.middleware.workspace_guard import CurrentUser, get_current_user
from app.models.workspace import WorkspaceMember
router = APIRouter(tags=["workspaces"])
class SwitchWorkspaceRequest(BaseModel):
workspace_id: int
@router.post("/workspaces/switch")
def switch_workspace(
body: SwitchWorkspaceRequest,
current_user: Annotated[CurrentUser, Depends(get_current_user)],
db: Session = Depends(get_db),
):
"""切换当前 workspace必须查 membership 校验)。"""
member = db.query(WorkspaceMember).filter(
WorkspaceMember.user_id == current_user.user_id,
WorkspaceMember.workspace_id == body.workspace_id,
).first()
if not member:
raise_forbidden("无权访问目标 workspace")
token = create_access_token(current_user.user_id, body.workspace_id, member.role)
return ok({
"current_workspace_id": body.workspace_id,
"token": token,
})