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
@@ -6,9 +6,9 @@ from typing import Any, Dict, List, Optional
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from auth import require_auth
|
||||
from club_tenancy import can_manage_club_org
|
||||
from db import get_db, get_cursor, r2d
|
||||
from tenant_context import TenantContext, get_tenant_context
|
||||
|
||||
router = APIRouter(prefix="/api", tags=["club_join_requests"])
|
||||
|
||||
@@ -35,9 +35,9 @@ def _club_active(cur, club_id: int) -> bool:
|
||||
return cur.fetchone() is not None
|
||||
|
||||
|
||||
def _assert_manage_club(cur, session: dict, club_id: int) -> None:
|
||||
pid = session["profile_id"]
|
||||
role = session.get("role")
|
||||
def _assert_manage_club(cur, tenant: TenantContext, club_id: int) -> None:
|
||||
pid = tenant.profile_id
|
||||
role = tenant.global_role
|
||||
if not can_manage_club_org(cur, pid, club_id, role):
|
||||
raise HTTPException(status_code=403, detail="Keine Berechtigung für Mitglieder-Verwaltung in diesem Verein")
|
||||
|
||||
@@ -107,8 +107,8 @@ def _response_one(cur, req_id: int, viewer_profile_id: int) -> Dict[str, Any]:
|
||||
|
||||
|
||||
@router.get("/me/club-join-requests")
|
||||
def get_my_join_requests(session: dict = Depends(require_auth)):
|
||||
pid = session["profile_id"]
|
||||
def get_my_join_requests(tenant: TenantContext = Depends(get_tenant_context)):
|
||||
pid = tenant.profile_id
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cur.execute(
|
||||
@@ -126,9 +126,9 @@ def get_my_join_requests(session: dict = Depends(require_auth)):
|
||||
|
||||
|
||||
@router.post("/me/club-join-requests", status_code=201)
|
||||
def create_my_join_request(body: JoinRequestCreate, session: dict = Depends(require_auth)):
|
||||
def create_my_join_request(body: JoinRequestCreate, tenant: TenantContext = Depends(get_tenant_context)):
|
||||
"""Antrag stellen (nicht möglich wenn bereits aktives Mitglied)."""
|
||||
pid = session["profile_id"]
|
||||
pid = tenant.profile_id
|
||||
msg = (body.message or "").strip() or None
|
||||
cid = body.club_id
|
||||
|
||||
@@ -168,8 +168,8 @@ def create_my_join_request(body: JoinRequestCreate, session: dict = Depends(requ
|
||||
|
||||
|
||||
@router.delete("/me/club-join-requests/{request_id}")
|
||||
def withdraw_my_join_request(request_id: int, session: dict = Depends(require_auth)):
|
||||
pid = session["profile_id"]
|
||||
def withdraw_my_join_request(request_id: int, tenant: TenantContext = Depends(get_tenant_context)):
|
||||
pid = tenant.profile_id
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cur.execute(
|
||||
@@ -188,11 +188,11 @@ def withdraw_my_join_request(request_id: int, session: dict = Depends(require_au
|
||||
|
||||
|
||||
@router.get("/clubs/{club_id}/join-requests")
|
||||
def list_club_join_requests(club_id: int, session: dict = Depends(require_auth)):
|
||||
def list_club_join_requests(club_id: int, tenant: TenantContext = Depends(get_tenant_context)):
|
||||
"""Offene Anträge für einen Verein (Vereins-/Plattform-Admin)."""
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
_assert_manage_club(cur, session, club_id)
|
||||
_assert_manage_club(cur, tenant, club_id)
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT r.*, p.email AS applicant_email, p.name AS applicant_name
|
||||
@@ -211,14 +211,14 @@ def accept_club_join_request(
|
||||
club_id: int,
|
||||
request_id: int,
|
||||
body: JoinRequestAccept,
|
||||
session: dict = Depends(require_auth),
|
||||
tenant: TenantContext = Depends(get_tenant_context),
|
||||
):
|
||||
admin_pid = session["profile_id"]
|
||||
admin_pid = tenant.profile_id
|
||||
roles = _normalize_roles(body.roles)
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
_assert_manage_club(cur, session, club_id)
|
||||
_assert_manage_club(cur, tenant, club_id)
|
||||
|
||||
cur.execute(
|
||||
"""
|
||||
@@ -257,11 +257,11 @@ def accept_club_join_request(
|
||||
|
||||
|
||||
@router.post("/clubs/{club_id}/join-requests/{request_id}/reject")
|
||||
def reject_club_join_request(club_id: int, request_id: int, session: dict = Depends(require_auth)):
|
||||
admin_pid = session["profile_id"]
|
||||
def reject_club_join_request(club_id: int, request_id: int, tenant: TenantContext = Depends(get_tenant_context)):
|
||||
admin_pid = tenant.profile_id
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
_assert_manage_club(cur, session, club_id)
|
||||
_assert_manage_club(cur, tenant, club_id)
|
||||
|
||||
cur.execute(
|
||||
"""
|
||||
|
||||
Reference in new issue
Block a user