SchoolSettings + adminpanel filtrato per scuola
This commit is contained in:
+117
-36
@@ -1,17 +1,18 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* SchoolContext v2
|
||||
* Determina la scuola corrente dell'utente loggato, il suo ruolo nella scuola
|
||||
* e le scuole tra cui può passare (owner multi-scuola, superadmin).
|
||||
* SchoolContext v3
|
||||
* - Scuola corrente dell'utente loggato e ruolo nella scuola
|
||||
* - Scuole tra cui può passare (owner multi-scuola, superadmin)
|
||||
* - Vista attiva (superadmin | school | teacher | student): permette a chi ha
|
||||
* permessi alti di vedere l'app come un livello inferiore. Mai superiore.
|
||||
*
|
||||
* Uso (nella headscript):
|
||||
* Uso (headscript):
|
||||
* $schoolData = SchoolContext::resolve((int)$iduserlogin);
|
||||
* $idschool = SchoolContext::id();
|
||||
*
|
||||
* Ruoli scuola: owner | admin | teacher | student
|
||||
* Il superadmin di piattaforma (auth_users.is_superadmin = 'Y') può entrare
|
||||
* in qualsiasi scuola: se non ne è membro, il suo ruolo risulta 'superadmin'.
|
||||
* I controlli isSuperadmin()/isSchoolAdmin()/isStaff() tengono conto della
|
||||
* vista attiva. viewModes() e available() usano i permessi REALI.
|
||||
*/
|
||||
|
||||
if (!class_exists('DBHandlerSelect')) {
|
||||
@@ -29,13 +30,15 @@ class SchoolContext
|
||||
/** @var int */
|
||||
private static $userId = 0;
|
||||
|
||||
/** @var bool */
|
||||
/** @var bool permesso REALE di superadmin */
|
||||
private static $superadmin = false;
|
||||
|
||||
/** @var array scuole di cui l'utente è membro */
|
||||
private static $memberships = [];
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* ================================================================== */
|
||||
/* RISOLUZIONE SCUOLA */
|
||||
/* ================================================================== */
|
||||
|
||||
public static function resolve(int $userId): array
|
||||
{
|
||||
@@ -77,9 +80,7 @@ class SchoolContext
|
||||
return self::$school;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cambia scuola corrente. Ritorna false se l'utente non può accedervi.
|
||||
*/
|
||||
/** Cambia scuola corrente. False se l'utente non può accedervi. */
|
||||
public static function switchTo(int $idschool): bool
|
||||
{
|
||||
if ($idschool <= 0) {
|
||||
@@ -96,10 +97,7 @@ class SchoolContext
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Scuole tra cui l'utente può scegliere (per il selettore in topbar).
|
||||
* Superadmin: tutte le scuole. Altri: solo quelle di cui è membro.
|
||||
*/
|
||||
/** Scuole selezionabili (permessi reali). Superadmin: tutte. */
|
||||
public static function available(): array
|
||||
{
|
||||
if (!self::$superadmin) {
|
||||
@@ -119,7 +117,70 @@ class SchoolContext
|
||||
return $all;
|
||||
}
|
||||
|
||||
/* --------------------------- getter -------------------------------- */
|
||||
/* ================================================================== */
|
||||
/* VISTA ATTIVA */
|
||||
/* ================================================================== */
|
||||
|
||||
/**
|
||||
* Viste consentite in base ai permessi REALI, dalla più alta alla più bassa.
|
||||
* @return array chiave => etichetta
|
||||
*/
|
||||
public static function viewModes(): array
|
||||
{
|
||||
$real = self::realRole();
|
||||
$modes = [];
|
||||
|
||||
if (self::$superadmin) {
|
||||
$modes['superadmin'] = 'Superadmin';
|
||||
}
|
||||
if (self::$superadmin || in_array($real, ['owner', 'admin'], true)) {
|
||||
$modes['school'] = 'Scuola';
|
||||
}
|
||||
if ($real === 'teacher') {
|
||||
$modes['teacher'] = 'Insegnante';
|
||||
}
|
||||
$modes['student'] = 'Allievo';
|
||||
|
||||
return $modes;
|
||||
}
|
||||
|
||||
/** Vista attiva (se quella in sessione non è consentita: la più alta) */
|
||||
public static function viewMode(): string
|
||||
{
|
||||
$modes = self::viewModes();
|
||||
$req = $_SESSION['view_mode'] ?? '';
|
||||
if (isset($modes[$req])) {
|
||||
return $req;
|
||||
}
|
||||
return (string) array_key_first($modes);
|
||||
}
|
||||
|
||||
public static function setViewMode(string $mode): bool
|
||||
{
|
||||
if (!isset(self::viewModes()[$mode])) {
|
||||
return false;
|
||||
}
|
||||
$_SESSION['view_mode'] = $mode;
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Pagina iniziale per la vista attiva */
|
||||
public static function landingPage(): string
|
||||
{
|
||||
switch (self::viewMode()) {
|
||||
case 'superadmin':
|
||||
return 'sa-schools.php';
|
||||
case 'school':
|
||||
case 'teacher':
|
||||
return 'adminpanel.php';
|
||||
default:
|
||||
return 'userpanel.php';
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================================== */
|
||||
/* GETTER E CONTROLLI (tengono conto della vista) */
|
||||
/* ================================================================== */
|
||||
|
||||
public static function id(): int
|
||||
{
|
||||
@@ -134,64 +195,85 @@ class SchoolContext
|
||||
return self::$school;
|
||||
}
|
||||
|
||||
/** owner | admin | teacher | student | superadmin */
|
||||
public static function role(): string
|
||||
/** Ruolo REALE nella scuola: owner|admin|teacher|student|superadmin */
|
||||
public static function realRole(): string
|
||||
{
|
||||
return self::get()['school_role'];
|
||||
}
|
||||
|
||||
public static function isSuperadmin(): bool
|
||||
/** Alias storico di realRole() */
|
||||
public static function role(): string
|
||||
{
|
||||
return self::realRole();
|
||||
}
|
||||
|
||||
public static function isRealSuperadmin(): bool
|
||||
{
|
||||
return self::$superadmin;
|
||||
}
|
||||
|
||||
public static function isSuperadmin(): bool
|
||||
{
|
||||
return self::viewMode() === 'superadmin';
|
||||
}
|
||||
|
||||
public static function isOwner(): bool
|
||||
{
|
||||
return self::role() === 'owner';
|
||||
return self::realRole() === 'owner' && self::isSchoolAdmin();
|
||||
}
|
||||
|
||||
/** Può gestire la scuola: owner, admin o superadmin */
|
||||
/** Gestisce la scuola (vista superadmin o scuola) */
|
||||
public static function isSchoolAdmin(): bool
|
||||
{
|
||||
return self::$superadmin || in_array(self::role(), ['owner', 'admin'], true);
|
||||
return in_array(self::viewMode(), ['superadmin', 'school'], true);
|
||||
}
|
||||
|
||||
/** Staff della scuola: amministratori + insegnanti */
|
||||
/** Staff: amministratori + insegnanti */
|
||||
public static function isStaff(): bool
|
||||
{
|
||||
return self::isSchoolAdmin() || self::role() === 'teacher';
|
||||
return self::isSchoolAdmin() || self::viewMode() === 'teacher';
|
||||
}
|
||||
|
||||
/** Blocca la pagina se l'utente non è amministratore della scuola */
|
||||
public static function requireSchoolAdmin(): void
|
||||
{
|
||||
if (!self::isSchoolAdmin()) {
|
||||
http_response_code(403);
|
||||
die('Accesso non autorizzato.');
|
||||
self::deny();
|
||||
}
|
||||
}
|
||||
|
||||
public static function requireStaff(): void
|
||||
{
|
||||
if (!self::isStaff()) {
|
||||
self::deny();
|
||||
}
|
||||
}
|
||||
|
||||
/** Blocca la pagina se l'utente non è superadmin */
|
||||
public static function requireSuperadmin(): void
|
||||
{
|
||||
if (!self::$superadmin) {
|
||||
http_response_code(403);
|
||||
die('Accesso non autorizzato.');
|
||||
if (!self::isSuperadmin()) {
|
||||
self::deny();
|
||||
}
|
||||
}
|
||||
|
||||
/* --------------------------- interni ------------------------------- */
|
||||
/* ================================================================== */
|
||||
/* INTERNI */
|
||||
/* ================================================================== */
|
||||
|
||||
private static function deny(): void
|
||||
{
|
||||
http_response_code(403);
|
||||
$home = self::$school ? self::landingPage() : 'userpanel.php';
|
||||
die('Accesso non autorizzato con la vista attuale. <a href="' . $home . '">Torna alla pagina iniziale</a>');
|
||||
}
|
||||
|
||||
private static function pick(int $requested): ?array
|
||||
{
|
||||
// 1) scuola richiesta e l'utente ne è membro
|
||||
foreach (self::$memberships as $m) {
|
||||
if ((int) $m['idschool'] === $requested) {
|
||||
return $m;
|
||||
}
|
||||
}
|
||||
|
||||
// 2) superadmin: può entrare in qualunque scuola esistente
|
||||
if (self::$superadmin) {
|
||||
$pdo = self::pdo();
|
||||
if ($requested > 0) {
|
||||
@@ -214,7 +296,6 @@ class SchoolContext
|
||||
return null;
|
||||
}
|
||||
|
||||
// 3) prima scuola di cui è membro
|
||||
return self::$memberships[0] ?? null;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user