feat: implement multi-tenancy governance and enhance club member management
- Bumped application version to 0.8.17, reflecting updates in both backend and frontend. - Introduced governance visibility for training plan templates and framework programs, allowing access based on visibility settings (private, club, official). - Added API endpoints for managing club members, including listing, adding, updating, and removing members. - Updated changelog to document the new features and changes made in this release.
This commit is contained in:
1 parent
c7bf7dcd9d
commit
7d476268b8
9 files changed
+501
-45
No files matched your search
@@ -2,7 +2,7 @@
|
||||
Training Planning – Trainingseinheiten, strukturierter Ablauf (Sektionen + Übungen/Notizen)
|
||||
und wiederverwendbare Trainingsvorlagen (Sektions-Gliederung).
|
||||
|
||||
Governance (Vorlagen-rechte über Vereine/„offiziell“) kann später nachgezogen werden.
|
||||
Governance: Sichtbarkeit wie Übungen (private / club / official); Schreiben nur Ersteller oder Plattform-Admin.
|
||||
"""
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
@@ -10,6 +10,11 @@ from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
|
||||
from db import get_db, get_cursor, r2d
|
||||
from auth import require_auth
|
||||
from club_tenancy import (
|
||||
assert_valid_governance_visibility,
|
||||
exercise_visible_to_profile,
|
||||
is_platform_admin,
|
||||
)
|
||||
|
||||
router = APIRouter(prefix="/api", tags=["training_planning"])
|
||||
|
||||
@@ -515,23 +520,39 @@ def _instantiate_from_template(cur, unit_id: int, template_id: int):
|
||||
)
|
||||
|
||||
|
||||
def _template_access(cur, tid: int, profile_id: int, role: str) -> Dict[str, Any]:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT *
|
||||
FROM training_plan_templates
|
||||
WHERE id = %s
|
||||
""",
|
||||
(tid,),
|
||||
)
|
||||
def _fetch_training_plan_template_row(cur, tid: int) -> Dict[str, Any]:
|
||||
cur.execute("SELECT * FROM training_plan_templates WHERE id = %s", (tid,))
|
||||
r = cur.fetchone()
|
||||
if not r:
|
||||
raise HTTPException(status_code=404, detail="Trainingsvorlage nicht gefunden")
|
||||
row = r2d(r)
|
||||
if role in ["admin", "superadmin"]:
|
||||
return row
|
||||
if row["created_by"] != profile_id:
|
||||
return r2d(r)
|
||||
|
||||
|
||||
def _template_assert_readable(cur, row: Dict[str, Any], profile_id: int, role: Optional[str]) -> None:
|
||||
if is_platform_admin(role):
|
||||
return
|
||||
if not exercise_visible_to_profile(
|
||||
cur,
|
||||
profile_id,
|
||||
row.get("visibility") or "club",
|
||||
row.get("club_id"),
|
||||
row.get("created_by"),
|
||||
role,
|
||||
):
|
||||
raise HTTPException(status_code=403, detail="Keine Berechtigung für diese Vorlage")
|
||||
|
||||
|
||||
def _template_assert_writable(cur, row: Dict[str, Any], profile_id: int, role: Optional[str]) -> None:
|
||||
if is_platform_admin(role):
|
||||
return
|
||||
if row.get("created_by") != profile_id:
|
||||
raise HTTPException(status_code=403, detail="Nur der Ersteller darf diese Vorlage ändern")
|
||||
|
||||
|
||||
def _template_access(cur, tid: int, profile_id: int, role: str) -> Dict[str, Any]:
|
||||
"""Lesender Zugriff (Liste der Vorlage für Einheit); Schreiben: _template_assert_writable."""
|
||||
row = _fetch_training_plan_template_row(cur, tid)
|
||||
_template_assert_readable(cur, row, profile_id, role)
|
||||
return row
|
||||
|
||||
|
||||
@@ -544,7 +565,7 @@ def list_training_plan_templates(session=Depends(require_auth)):
|
||||
role = session.get("role")
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
if role in ["admin", "superadmin"]:
|
||||
if is_platform_admin(role):
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT t.*,
|
||||
@@ -561,10 +582,21 @@ def list_training_plan_templates(session=Depends(require_auth)):
|
||||
(SELECT COUNT(*) FROM training_plan_template_sections s WHERE s.template_id = t.id)
|
||||
AS sections_count
|
||||
FROM training_plan_templates t
|
||||
WHERE t.created_by = %s
|
||||
WHERE (
|
||||
t.visibility = 'official'
|
||||
OR (t.visibility = 'private' AND t.created_by = %s)
|
||||
OR (
|
||||
t.visibility = 'club'
|
||||
AND t.club_id IS NOT NULL
|
||||
AND EXISTS (
|
||||
SELECT 1 FROM club_members cm
|
||||
WHERE cm.profile_id = %s AND cm.club_id = t.club_id AND cm.status = 'active'
|
||||
)
|
||||
)
|
||||
)
|
||||
ORDER BY t.updated_at DESC NULLS LAST, t.name
|
||||
""",
|
||||
(profile_id,),
|
||||
(profile_id, profile_id),
|
||||
)
|
||||
return [r2d(r) for r in cur.fetchall()]
|
||||
|
||||
@@ -598,18 +630,23 @@ def create_training_plan_template(data: dict, session=Depends(require_auth)):
|
||||
name = (data.get("name") or "").strip()
|
||||
if not name:
|
||||
raise HTTPException(status_code=400, detail="name ist Pflicht")
|
||||
vis_raw = data.get("visibility")
|
||||
visibility = (vis_raw if isinstance(vis_raw, str) else "club").strip() or "club"
|
||||
club_id = data.get("club_id")
|
||||
if club_id in ("", []):
|
||||
club_id = None
|
||||
sections_in = data.get("sections") or []
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
assert_valid_governance_visibility(cur, profile_id, role, visibility, club_id)
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO training_plan_templates (club_id, created_by, name, description)
|
||||
VALUES (%s, %s, %s, %s)
|
||||
INSERT INTO training_plan_templates (club_id, created_by, name, description, visibility)
|
||||
VALUES (%s, %s, %s, %s, %s)
|
||||
RETURNING id
|
||||
""",
|
||||
(club_id, profile_id, name, data.get("description")),
|
||||
(club_id, profile_id, name, data.get("description"), visibility),
|
||||
)
|
||||
tid = cur.fetchone()["id"]
|
||||
for si, sec in enumerate(sections_in):
|
||||
@@ -634,7 +671,21 @@ def update_training_plan_template(template_id: int, data: dict, session=Depends(
|
||||
role = session.get("role")
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
_template_access(cur, template_id, profile_id, role)
|
||||
row_prev = _fetch_training_plan_template_row(cur, template_id)
|
||||
_template_assert_writable(cur, row_prev, profile_id, role)
|
||||
merged_vis = row_prev.get("visibility") or "club"
|
||||
merged_club = row_prev.get("club_id")
|
||||
if "visibility" in data:
|
||||
v_in = data.get("visibility")
|
||||
if not isinstance(v_in, str) or v_in not in ("private", "club", "official"):
|
||||
raise HTTPException(status_code=400, detail="visibility ungültig")
|
||||
merged_vis = v_in
|
||||
if "club_id" in data:
|
||||
merged_club = data.get("club_id")
|
||||
if merged_club in ("", []):
|
||||
merged_club = None
|
||||
if "visibility" in data or "club_id" in data:
|
||||
assert_valid_governance_visibility(cur, profile_id, role, merged_vis, merged_club)
|
||||
fields = []
|
||||
params: List[Any] = []
|
||||
if "name" in data:
|
||||
@@ -649,7 +700,10 @@ def update_training_plan_template(template_id: int, data: dict, session=Depends(
|
||||
params.append(data.get("description"))
|
||||
if "club_id" in data:
|
||||
fields.append("club_id = %s")
|
||||
params.append(data.get("club_id"))
|
||||
params.append(merged_club)
|
||||
if "visibility" in data:
|
||||
fields.append("visibility = %s")
|
||||
params.append(merged_vis)
|
||||
fields.append("updated_at = NOW()")
|
||||
params.append(template_id)
|
||||
cur.execute(
|
||||
@@ -686,7 +740,8 @@ def delete_training_plan_template(template_id: int, session=Depends(require_auth
|
||||
role = session.get("role")
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
_template_access(cur, template_id, profile_id, role)
|
||||
row_del = _fetch_training_plan_template_row(cur, template_id)
|
||||
_template_assert_writable(cur, row_del, profile_id, role)
|
||||
cur.execute("DELETE FROM training_plan_templates WHERE id = %s", (template_id,))
|
||||
conn.commit()
|
||||
return {"ok": True}
|
||||
|
||||
Reference in new issue
Block a user