mirror of
https://github.com/tips-of-mine/gestion-certificats2.git
synced 2025-06-28 13:58:42 +02:00
![google-labs-jules[bot]](/assets/img/avatar_default.png)
Ce commit introduit la possibilité pour vous de télécharger les fichiers de certificats (racine, intermédiaire, final) et la clé privée du certificat racine (administrateurs uniquement) directement depuis la page du tableau de bord. Changements inclus : - Ajout d'une méthode `download()` dans `CertificateController` pour gérer la logique de téléchargement sécurisé des fichiers. - Ajout d'une nouvelle route `GET /certificates/download`. - Correction et standardisation des liens de téléchargement dans la vue du dashboard pour assurer la transmission correcte des paramètres (type de certificat, nom de fichier, périmètre). - La méthode de téléchargement inclut la journalisation des tentatives et des erreurs, ainsi que la gestion des permissions pour la clé privée racine.
126 lines
4.7 KiB
PHP
126 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Services\AuthService;
|
|
use App\Services\LanguageService;
|
|
use App\Utils\DarkMode;
|
|
// Ensure App\Core\Database is imported
|
|
use App\Core\Database;
|
|
|
|
/**
|
|
* Contrôleur pour la page du tableau de bord.
|
|
*/
|
|
class DashboardController
|
|
{
|
|
private $authService;
|
|
private $langService;
|
|
private $db; // Property to hold the database instance
|
|
|
|
/**
|
|
* Constructeur du DashboardController.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
// Initialize database connection here if it's meant to be a class property
|
|
// For now, authService initializes its own, and index() gets a new instance.
|
|
// If $this->db was intended, it should be $this->db = Database::getInstance();
|
|
$this->authService = new AuthService(Database::getInstance());
|
|
$this->langService = new LanguageService(APP_ROOT_DIR . '/src/Lang/');
|
|
// $this->db = Database::getInstance(); // Uncomment if $db should be a class property accessible in index() via $this->db
|
|
}
|
|
|
|
/**
|
|
* Affiche le tableau de bord.
|
|
* Redirige vers la page de connexion si l'utilisateur n'est pas connecté.
|
|
*/
|
|
public function index()
|
|
{
|
|
if (!$this->authService->isLoggedIn()) {
|
|
header('Location: /login');
|
|
exit();
|
|
}
|
|
|
|
// Récupère les traductions et les informations pour la vue
|
|
global $translations;
|
|
$currentLang = $this->langService->getLanguage();
|
|
$username = $this->authService->getUsername();
|
|
$darkModeClass = DarkMode::getBodyClass();
|
|
$userRole = $this->authService->getUserRole(); // Pour afficher/masquer certains éléments
|
|
|
|
// Initialize database connection
|
|
// If $this->db was initialized in constructor, use $db = $this->db;
|
|
$db = Database::getInstance(); // Using a local instance as per original structure
|
|
|
|
// Initialize structured certificates array
|
|
$structuredCertificates = [
|
|
'root' => null,
|
|
'intermediates' => [],
|
|
];
|
|
|
|
// Fetch Root Certificate
|
|
$stmt = $db->prepare("SELECT name FROM certificates WHERE type = 'root' LIMIT 1");
|
|
$stmt->execute();
|
|
$rootCert = $stmt->fetch();
|
|
|
|
if ($rootCert) {
|
|
$structuredCertificates['root'] = [
|
|
'name' => $rootCert['name'],
|
|
'cert_path' => ROOT_CA_PATH . '/certs/ca.cert.pem',
|
|
'key_path' => ROOT_CA_PATH . '/private/ca.key.pem', // Corrected path as per instructions
|
|
];
|
|
} else {
|
|
// Handle case where root certificate is not found, though unlikely
|
|
// You might want to log this or set a default structure
|
|
$structuredCertificates['root'] = [
|
|
'name' => 'N/A',
|
|
'cert_path' => null,
|
|
'key_path' => null,
|
|
];
|
|
}
|
|
|
|
// Fetch Intermediate Certificates
|
|
$stmt = $db->prepare("
|
|
SELECT c.id, c.name, c.functional_perimeter_id, fp.name as perimeter_name
|
|
FROM certificates c
|
|
JOIN functional_perimeters fp ON c.functional_perimeter_id = fp.id
|
|
WHERE c.type = 'intermediate'
|
|
ORDER BY fp.name ASC, c.name ASC
|
|
");
|
|
$stmt->execute();
|
|
$intermediateCerts = $stmt->fetchAll();
|
|
|
|
foreach ($intermediateCerts as $interCert) {
|
|
$intermediateData = [
|
|
'id' => $interCert['id'],
|
|
'name' => $interCert['name'],
|
|
'perimeter_name' => $interCert['perimeter_name'],
|
|
'functional_perimeter_id' => $interCert['functional_perimeter_id'], // Pass perimeter_id for linking
|
|
'final_certificates' => [],
|
|
];
|
|
|
|
// Fetch Final Certificates for this Intermediate
|
|
// Ensure functional_perimeter_id is used in the query for 'simple' certificates
|
|
$stmtFinal = $db->prepare("
|
|
SELECT name, type, expiration_date, is_revoked
|
|
FROM certificates
|
|
WHERE type = 'simple' AND functional_perimeter_id = ?
|
|
ORDER BY name ASC
|
|
");
|
|
// Use $interCert['functional_perimeter_id'] which is the ID of the perimeter for this intermediate cert
|
|
$stmtFinal->execute([$interCert['functional_perimeter_id']]);
|
|
$finalCerts = $stmtFinal->fetchAll();
|
|
|
|
if ($finalCerts) {
|
|
foreach ($finalCerts as $finalCert) {
|
|
$intermediateData['final_certificates'][] = $finalCert;
|
|
}
|
|
}
|
|
$structuredCertificates['intermediates'][] = $intermediateData;
|
|
}
|
|
|
|
// Pass data to the view
|
|
require_once APP_ROOT_DIR . '/src/Views/dashboard/index.php';
|
|
}
|
|
}
|