Auth Phase 2: Session-Zugriffe kanalisieren (verhaltensneutral)

Vorbereitung für den JWT+Redis-Umschalter: alle verstreuten
$_SESSION['teacher_id'|admin_id']-Zugriffe laufen jetzt durch Session::-Helper,
damit der Backend-Tausch später EINE Datei (Session.php) ist statt 18 Stellen.

- Session.php: neue Admin-Helper adminId()/loginAdmin()/logoutAdmin()/
  requireAdmin() (mirror der Teacher-Helper). teacherId()/adminId() starten
  die Session jetzt lazy (Aufrufer müssen Session::start() nicht mehr).
- 18 Fundstellen kanalisiert (admin.php, admin-accounts, admin-modules,
  licenses, levels, waypoints, foto-upload, admin_gate, save_map).
- Country.php + app.php cockpit_href: über Session:: mit class_exists-Fallback
  (Bootstrap/Lib-Kontext, Session evtl. nicht geladen).

Verhaltensneutral: 14/14 Lint OK, Helper-Round-Trip 6/6 PASS, Live-Smoke
(admin status/modules/accounts/licenses) weist unauth. Requests wie bisher
mit 401/403 ab. AUTH_BACKEND bleibt 'session' — Live unverändert.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-08-18 16:33:14 +02:00
parent 5d94572569
commit 37f6238d05
12 changed files with 55 additions and 27 deletions
+35 -1
View File
@@ -67,8 +67,9 @@ class Session {
return $id;
}
/** Lehrer eingeloggt? */
/** Lehrer eingeloggt? (startet die Session lazy — Aufrufer müssen nicht) */
public static function teacherId(): ?int {
self::start();
return $_SESSION['teacher_id'] ?? null;
}
@@ -89,6 +90,39 @@ class Session {
return $id;
}
// ── Admin (Super-Admin nach 2FA) ────────────────────────────────────
// Einheitlicher Zugang wie beim Lehrer, damit der spätere JWT+Redis-
// Umschalter (AUTH_BACKEND) an EINER Stelle greift statt an jedem Endpoint.
/** Admin eingeloggt (2FA abgeschlossen)? */
public static function adminId(): ?int {
self::start();
return $_SESSION['admin_id'] ?? null;
}
/** Admin-Login abschließen (nach erfolgreicher 2FA). */
public static function loginAdmin(int $adminId): void {
self::start();
$_SESSION['admin_id'] = $adminId;
}
/** Admin-Logout — inkl. offener 2FA-Phase (admin_pending). */
public static function logoutAdmin(): void {
self::start();
unset($_SESSION['admin_id'], $_SESSION['admin_pending']);
}
/** Admin-Guard: 403 wenn nicht eingeloggt. */
public static function requireAdmin(): int {
$id = self::adminId();
if (!$id) {
http_response_code(403);
echo json_encode(['error' => 'Nur Admin']);
exit;
}
return $id;
}
public static function logout(): void {
self::start();
session_destroy();