Refactor session management and navigation in index and header files; add auth utilities for user role verification

This commit is contained in:
Noah 2026-06-05 09:51:02 +02:00
parent f6d994a55b
commit ec31b1df61
3 changed files with 39 additions and 24 deletions

View file

@ -0,0 +1,19 @@
<?php
// Vérifie si l'utilisateur est connecté
function is_logged() {
return isset($_SESSION['login']);
}
// Vérifie si l'utilisateur a un rôle spécifique
function has_role(string $role) {
return isset($_SESSION['role']) && $_SESSION['role'] == $role;
}
// Redirige vers l'authentification si l'utilisateur n'a pas les droits
function verify_grants(string $route, string $role = '') {
if (!has_role($role) && !($role == '' && is_logged())) {
header('Location: index.php?route=auth&ask=' . $route);
exit;
}
}

View file

@ -8,7 +8,7 @@
//Loads some functions for session managment and starts the session //Loads some functions for session managment and starts the session
require('controllers/auth_utilities.php'); require('controllers/auth_utilities.php');
session_start(); session_start();
var_dump($_SESSION); //var_dump($_SESSION);
//Erreurs à afficher SEULEMENT en phase de développement ! //Erreurs à afficher SEULEMENT en phase de développement !
error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED); error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED);

View file

@ -1,34 +1,30 @@
<?php <?php
//session_start(); //session_start();
// Liens de navigation (conditionnels selon session et rôle) // --- NAVIGATION STATIQUE (auth commentée en attendant le système de connexion) ---
$nav = '<li><a href="index.php">Accueil</a></li>'; $nav = '<li><a href="index.php">Accueil</a></li>';
if (is_logged()) { // À DÉCOMMENTER quand auth_utilities.php sera chargé :
// $nav .= '<li><a href="index.php?route=planning">Planning</a></li>';
// $nav .= '<li><a href="index.php?route=sauveteurs">Sauveteurs</a></li>';
// $nav .= '<li><a href="index.php?route=gestion">Gestion</a></li>';
// $nav .= '<li><a href="index.php?route=admin">Admin</a></li>';
$nav .= '<li><a href="index.php?route=planning">Planning</a></li>'; $nav .= '<li><a href="index.php?route=planning">Planning</a></li>';
$nav .= '<li><a href="index.php?route=sauveteurs">Sauveteurs</a></li>'; $nav .= '<li><a href="index.php?route=sauveteurs">Sauveteurs</a></li>';
}
if (has_role('gestionnaire') || has_role('administration')) {
$nav .= '<li><a href="index.php?route=gestion">Gestion</a></li>'; $nav .= '<li><a href="index.php?route=gestion">Gestion</a></li>';
}
if (has_role('administration')) {
$nav .= '<li><a href="index.php?route=admin">Admin</a></li>'; $nav .= '<li><a href="index.php?route=admin">Admin</a></li>';
}
if (is_logged()) {
$nav .= '<li><a href="index.php?route=logout" class="nav-right">Déconnexion</a></li>';
$session = 'Connecté : ' . htmlentities($_SESSION['login']) . ' (' . ($_SESSION['role'] ?: 'lecture') . ')';
} else {
$nav .= '<li><a href="index.php?route=auth" class="nav-right">Connexion</a></li>'; $nav .= '<li><a href="index.php?route=auth" class="nav-right">Connexion</a></li>';
$session = 'Non connecté';
}
// Notification flash // $session = 'Connecté : ' . htmlentities($_SESSION['login']) . ' (' . ($_SESSION['role'] ?: 'lecture') . ')';
$session = 'Non connecté';
$notif = ''; $notif = '';
if (!empty($_SESSION['notification'])) { // if (!empty($_SESSION['notification'])) {
$notif = '<div id="notification">' . htmlentities($_SESSION['notification']) . '</div>'; // $notif = '<div id="notification">' . htmlentities($_SESSION['notification']) . '</div>';
unset($_SESSION['notification']); // unset($_SESSION['notification']);
} // }
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
<html lang="fr"> <html lang="fr">