2026-06-29 00:10:10 +02:00
|
|
|
"""Tenant management routes."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import uuid
|
|
|
|
|
|
2026-06-29 17:43:56 +02:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
2026-06-29 00:10:10 +02:00
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
|
|
|
|
from app.core.db import get_db
|
2026-07-15 22:35:50 +02:00
|
|
|
from app.deps import require_permission
|
2026-06-29 00:10:10 +02:00
|
|
|
from app.schemas.tenant import TenantCreate, TenantUserAssign
|
|
|
|
|
from app.services.tenant_service import tenant_service
|
|
|
|
|
|
|
|
|
|
router = APIRouter(prefix="/api/v1/tenants", tags=["tenants"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("")
|
|
|
|
|
async def list_tenants(
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
2026-07-15 22:35:50 +02:00
|
|
|
current_user: dict = Depends(require_permission("tenants:read")),
|
2026-06-29 00:10:10 +02:00
|
|
|
):
|
|
|
|
|
"""List tenants for the current user."""
|
|
|
|
|
user_id = uuid.UUID(current_user["user_id"])
|
|
|
|
|
tenants = await tenant_service.list_tenants_for_user(db, user_id)
|
|
|
|
|
return {"items": tenants}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("")
|
|
|
|
|
async def create_tenant(
|
|
|
|
|
body: TenantCreate,
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
2026-07-15 22:35:50 +02:00
|
|
|
current_user: dict = Depends(require_permission("tenants:write")),
|
2026-06-29 00:10:10 +02:00
|
|
|
):
|
|
|
|
|
"""Create a new tenant (admin only)."""
|
|
|
|
|
tenant = await tenant_service.create_tenant(db, body.name, body.slug)
|
|
|
|
|
return {
|
|
|
|
|
"id": str(tenant.id),
|
|
|
|
|
"name": tenant.name,
|
|
|
|
|
"slug": tenant.slug,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/{tenant_id}/users")
|
|
|
|
|
async def list_tenant_users(
|
|
|
|
|
tenant_id: str,
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
2026-07-15 22:35:50 +02:00
|
|
|
current_user: dict = Depends(require_permission("tenants:write")),
|
2026-06-29 00:10:10 +02:00
|
|
|
):
|
|
|
|
|
"""List users in a tenant (admin only)."""
|
|
|
|
|
try:
|
|
|
|
|
tid = uuid.UUID(tenant_id)
|
|
|
|
|
except ValueError:
|
2026-06-29 17:43:56 +02:00
|
|
|
raise HTTPException(
|
|
|
|
|
400, detail={"detail": "Invalid tenant_id", "code": "invalid_id"}
|
|
|
|
|
) from None
|
2026-06-29 00:10:10 +02:00
|
|
|
|
|
|
|
|
users = await tenant_service.list_tenant_users(db, tid)
|
|
|
|
|
return {"items": users}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/{tenant_id}/users")
|
|
|
|
|
async def assign_user_to_tenant(
|
|
|
|
|
tenant_id: str,
|
|
|
|
|
body: TenantUserAssign,
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
2026-07-15 22:35:50 +02:00
|
|
|
current_user: dict = Depends(require_permission("tenants:write")),
|
2026-06-29 00:10:10 +02:00
|
|
|
):
|
|
|
|
|
"""Assign a user to a tenant (admin only)."""
|
|
|
|
|
try:
|
|
|
|
|
tid = uuid.UUID(tenant_id)
|
|
|
|
|
uid = uuid.UUID(body.user_id)
|
|
|
|
|
except ValueError:
|
2026-06-29 17:43:56 +02:00
|
|
|
raise HTTPException(400, detail={"detail": "Invalid ID", "code": "invalid_id"}) from None
|
2026-06-29 00:10:10 +02:00
|
|
|
|
2026-06-29 17:43:56 +02:00
|
|
|
await tenant_service.assign_user_to_tenant(db, tid, uid)
|
2026-06-29 00:10:10 +02:00
|
|
|
return {"message": "User assigned to tenant"}
|