feat: integrate tenant context across club-related APIs
- Refactored club join requests, memberships, and clubs routers to utilize TenantContext for authentication and authorization, enhancing security and consistency. - Updated session handling to replace direct session dictionary access with TenantContext, improving code clarity and maintainability. - Ensured proper role and profile ID retrieval from TenantContext in various endpoints, streamlining access control for club management functionalities.
This commit is contained in:
1 parent
870a7611dc
commit
5aee9c52fc
9 files changed
+294
-182
No files matched your search
+50
-50
@@ -7,7 +7,7 @@ from typing import Any, List, Optional
|
||||
from fastapi import APIRouter, HTTPException, Depends, Query
|
||||
|
||||
from db import get_db, get_cursor, r2d
|
||||
from auth import require_auth
|
||||
from tenant_context import TenantContext, get_tenant_context
|
||||
from club_tenancy import (
|
||||
assert_club_member,
|
||||
can_manage_club_org,
|
||||
@@ -23,13 +23,13 @@ router = APIRouter(prefix="/api", tags=["clubs"])
|
||||
@router.get("/clubs")
|
||||
def list_clubs(
|
||||
status: Optional[str] = Query(default=None),
|
||||
session=Depends(require_auth),
|
||||
tenant: TenantContext = Depends(get_tenant_context),
|
||||
):
|
||||
"""
|
||||
Vereine: für normale Nutzer nur Mitgliedschaft-Vereine; Plattform-Admins sehen alle.
|
||||
"""
|
||||
role = session.get("role")
|
||||
profile_id = session["profile_id"]
|
||||
role = tenant.global_role
|
||||
profile_id = tenant.profile_id
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
query = "SELECT * FROM clubs"
|
||||
@@ -76,9 +76,9 @@ def public_club_directory():
|
||||
|
||||
# ── Aktive Mitglieder für Trainer-/Co-Trainer-Auswahl (jeder Vereinsmitglied) ──
|
||||
@router.get("/clubs/{club_id}/members/directory")
|
||||
def club_members_directory(club_id: int, session=Depends(require_auth)):
|
||||
profile_id = session["profile_id"]
|
||||
role = session.get("role")
|
||||
def club_members_directory(club_id: int, tenant: TenantContext = Depends(get_tenant_context)):
|
||||
profile_id = tenant.profile_id
|
||||
role = tenant.global_role
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
if not is_platform_admin(role):
|
||||
@@ -101,10 +101,10 @@ def club_members_directory(club_id: int, session=Depends(require_auth)):
|
||||
|
||||
# ── Get Club ──────────────────────────────────────────────────────────
|
||||
@router.get("/clubs/{club_id}")
|
||||
def get_club(club_id: int, session=Depends(require_auth)):
|
||||
def get_club(club_id: int, tenant: TenantContext = Depends(get_tenant_context)):
|
||||
"""Get club by ID with divisions and groups – nur Mitglied oder Plattform-Admin."""
|
||||
profile_id = session["profile_id"]
|
||||
role = session.get("role")
|
||||
profile_id = tenant.profile_id
|
||||
role = tenant.global_role
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
if not is_platform_admin(role):
|
||||
@@ -149,9 +149,9 @@ def get_club(club_id: int, session=Depends(require_auth)):
|
||||
|
||||
# ── Create Club ───────────────────────────────────────────────────────
|
||||
@router.post("/clubs")
|
||||
def create_club(data: dict, session=Depends(require_auth)):
|
||||
def create_club(data: dict, tenant: TenantContext = Depends(get_tenant_context)):
|
||||
"""Neuen Verein anlegen – nur Plattform-Admin; Pflicht: primary_admin_profile_id (Hauptverwalter:in)."""
|
||||
role = session.get("role")
|
||||
role = tenant.global_role
|
||||
if not is_platform_admin(role):
|
||||
raise HTTPException(403, "Nur Plattform-Administratoren dürfen neue Vereine anlegen")
|
||||
|
||||
@@ -213,15 +213,15 @@ def create_club(data: dict, session=Depends(require_auth)):
|
||||
|
||||
conn.commit()
|
||||
|
||||
return get_club(club_id, session)
|
||||
return get_club(club_id, tenant)
|
||||
|
||||
|
||||
# ── Update Club ───────────────────────────────────────────────────────
|
||||
@router.put("/clubs/{club_id}")
|
||||
def update_club(club_id: int, data: dict, session=Depends(require_auth)):
|
||||
def update_club(club_id: int, data: dict, tenant: TenantContext = Depends(get_tenant_context)):
|
||||
"""Verein bearbeiten – Plattform-Admin oder Vereinsadmin."""
|
||||
profile_id = session["profile_id"]
|
||||
role = session.get("role")
|
||||
profile_id = tenant.profile_id
|
||||
role = tenant.global_role
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
@@ -263,14 +263,14 @@ def update_club(club_id: int, data: dict, session=Depends(require_auth)):
|
||||
|
||||
conn.commit()
|
||||
|
||||
return get_club(club_id, session)
|
||||
return get_club(club_id, tenant)
|
||||
|
||||
|
||||
# ── Delete Club ───────────────────────────────────────────────────────
|
||||
@router.delete("/clubs/{club_id}")
|
||||
def delete_club(club_id: int, session=Depends(require_auth)):
|
||||
def delete_club(club_id: int, tenant: TenantContext = Depends(get_tenant_context)):
|
||||
"""Delete club (superadmin only)."""
|
||||
role = session.get('role')
|
||||
role = tenant.global_role
|
||||
if role != 'superadmin':
|
||||
raise HTTPException(403, "Nur Superadmins dürfen Vereine löschen")
|
||||
|
||||
@@ -293,11 +293,11 @@ def delete_club(club_id: int, session=Depends(require_auth)):
|
||||
@router.get("/divisions")
|
||||
def list_divisions(
|
||||
club_id: Optional[int] = Query(default=None),
|
||||
session=Depends(require_auth),
|
||||
tenant: TenantContext = Depends(get_tenant_context),
|
||||
):
|
||||
"""Sparten – ohne Admin-Rechte nur in eigenen Vereinen sichtbar."""
|
||||
profile_id = session["profile_id"]
|
||||
role = session.get("role")
|
||||
profile_id = tenant.profile_id
|
||||
role = tenant.global_role
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
@@ -338,10 +338,10 @@ def list_divisions(
|
||||
|
||||
# ── Create Division ───────────────────────────────────────────────────
|
||||
@router.post("/divisions")
|
||||
def create_division(data: dict, session=Depends(require_auth)):
|
||||
def create_division(data: dict, tenant: TenantContext = Depends(get_tenant_context)):
|
||||
"""Create new division – Vereinsadmin / Plattform-Admin."""
|
||||
profile_id = session["profile_id"]
|
||||
role = session.get("role")
|
||||
profile_id = tenant.profile_id
|
||||
role = tenant.global_role
|
||||
|
||||
club_id = data.get("club_id")
|
||||
name = data.get("name")
|
||||
@@ -392,10 +392,10 @@ def create_division(data: dict, session=Depends(require_auth)):
|
||||
|
||||
# ── Update Division ───────────────────────────────────────────────────
|
||||
@router.put("/divisions/{division_id}")
|
||||
def update_division(division_id: int, data: dict, session=Depends(require_auth)):
|
||||
def update_division(division_id: int, data: dict, tenant: TenantContext = Depends(get_tenant_context)):
|
||||
"""Update division – Vereinsadmin / Plattform-Admin."""
|
||||
profile_id = session["profile_id"]
|
||||
role = session.get("role")
|
||||
profile_id = tenant.profile_id
|
||||
role = tenant.global_role
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
@@ -439,10 +439,10 @@ def update_division(division_id: int, data: dict, session=Depends(require_auth))
|
||||
|
||||
# ── Delete Division ───────────────────────────────────────────────────
|
||||
@router.delete("/divisions/{division_id}")
|
||||
def delete_division(division_id: int, session=Depends(require_auth)):
|
||||
def delete_division(division_id: int, tenant: TenantContext = Depends(get_tenant_context)):
|
||||
"""Delete division – Vereinsadmin / Plattform-Admin."""
|
||||
profile_id = session["profile_id"]
|
||||
role = session.get("role")
|
||||
profile_id = tenant.profile_id
|
||||
role = tenant.global_role
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
@@ -467,13 +467,13 @@ def list_training_groups(
|
||||
club_id: Optional[int] = Query(default=None),
|
||||
division_id: Optional[int] = Query(default=None),
|
||||
status: Optional[str] = Query(default=None),
|
||||
session=Depends(require_auth),
|
||||
tenant: TenantContext = Depends(get_tenant_context),
|
||||
):
|
||||
"""
|
||||
Trainingsgruppen – ohne Plattform-Admin nur in eigenen Vereinen.
|
||||
"""
|
||||
profile_id = session["profile_id"]
|
||||
role = session.get("role")
|
||||
profile_id = tenant.profile_id
|
||||
role = tenant.global_role
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
@@ -526,10 +526,10 @@ def list_training_groups(
|
||||
|
||||
# ── Get Training Group ────────────────────────────────────────────────
|
||||
@router.get("/groups/{group_id}")
|
||||
def get_training_group(group_id: int, session=Depends(require_auth)):
|
||||
def get_training_group(group_id: int, tenant: TenantContext = Depends(get_tenant_context)):
|
||||
"""Trainingsgruppe – nur mit Vereinszugriff."""
|
||||
profile_id = session["profile_id"]
|
||||
role = session.get("role")
|
||||
profile_id = tenant.profile_id
|
||||
role = tenant.global_role
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
|
||||
@@ -562,11 +562,11 @@ def get_training_group(group_id: int, session=Depends(require_auth)):
|
||||
|
||||
# ── Create Training Group ─────────────────────────────────────────────
|
||||
@router.post("/groups")
|
||||
def create_training_group(data: dict, session=Depends(require_auth)):
|
||||
def create_training_group(data: dict, tenant: TenantContext = Depends(get_tenant_context)):
|
||||
"""Trainingsgruppe anlegen – Mitglied mit Planungs-/Admin-Rolle im Verein."""
|
||||
profile_id = session["profile_id"]
|
||||
role = session.get("role")
|
||||
if role not in ["admin", "superadmin", "trainer", "user"]:
|
||||
profile_id = tenant.profile_id
|
||||
role = tenant.global_role
|
||||
if role not in ("admin", "superadmin", "trainer", "user"):
|
||||
raise HTTPException(403, "Keine Berechtigung, Trainingsgruppen anzulegen")
|
||||
|
||||
club_id = data.get("club_id")
|
||||
@@ -623,15 +623,15 @@ def create_training_group(data: dict, session=Depends(require_auth)):
|
||||
group_id = cur.fetchone()["id"]
|
||||
conn.commit()
|
||||
|
||||
return get_training_group(group_id, session)
|
||||
return get_training_group(group_id, tenant)
|
||||
|
||||
|
||||
# ── Update Training Group ─────────────────────────────────────────────
|
||||
@router.put("/groups/{group_id}")
|
||||
def update_training_group(group_id: int, data: dict, session=Depends(require_auth)):
|
||||
def update_training_group(group_id: int, data: dict, tenant: TenantContext = Depends(get_tenant_context)):
|
||||
"""Update training group – Vereinsadmin, Plattform-Admin oder zugewiesene Trainer."""
|
||||
profile_id = session["profile_id"]
|
||||
role = session.get("role")
|
||||
profile_id = tenant.profile_id
|
||||
role = tenant.global_role
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
@@ -647,7 +647,7 @@ def update_training_group(group_id: int, data: dict, session=Depends(require_aut
|
||||
co_trainers = row["co_trainer_ids"] or []
|
||||
club_id = row["club_id"]
|
||||
|
||||
allowed = role in ["admin", "superadmin"]
|
||||
allowed = role in ("admin", "superadmin")
|
||||
if not allowed:
|
||||
allowed = can_manage_club_org(cur, profile_id, club_id, role)
|
||||
if not allowed:
|
||||
@@ -693,15 +693,15 @@ def update_training_group(group_id: int, data: dict, session=Depends(require_aut
|
||||
|
||||
conn.commit()
|
||||
|
||||
return get_training_group(group_id, session)
|
||||
return get_training_group(group_id, tenant)
|
||||
|
||||
|
||||
# ── Delete Training Group ─────────────────────────────────────────────
|
||||
@router.delete("/groups/{group_id}")
|
||||
def delete_training_group(group_id: int, session=Depends(require_auth)):
|
||||
def delete_training_group(group_id: int, tenant: TenantContext = Depends(get_tenant_context)):
|
||||
"""Delete training group – Vereinsadmin oder Plattform-Admin."""
|
||||
profile_id = session["profile_id"]
|
||||
role = session.get("role")
|
||||
profile_id = tenant.profile_id
|
||||
role = tenant.global_role
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
|
||||
Reference in new issue
Block a user