Skip to content

Commit

Permalink
Move maintenance check to a controller and console event
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierstoval authored and trasher committed Dec 20, 2024
1 parent 2da6cb9 commit 1160f4a
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 20 deletions.
20 changes: 0 additions & 20 deletions src/Glpi/Config/LegacyConfigurators/StandardIncludes.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,6 @@ public function execute(): void
*/
global $CFG_GLPI;

// Check maintenance mode
if (
!$this->isFrontEndAssetEndpoint($this->getRequest())
&& !$this->isSymfonyProfilerEndpoint($this->getRequest())
&& isset($CFG_GLPI["maintenance_mode"])
&& $CFG_GLPI["maintenance_mode"]
) {
if (isset($_GET['skipMaintenance']) && $_GET['skipMaintenance']) {
$_SESSION["glpiskipMaintenance"] = 1;
}

if (!isset($_SESSION["glpiskipMaintenance"]) || !$_SESSION["glpiskipMaintenance"]) {
TemplateRenderer::getInstance()->display('maintenance.html.twig', [
'title' => "MAINTENANCE MODE",
'maintenance_text' => $CFG_GLPI["maintenance_text"] ?? "",
]);
exit();
}
}

// Check version
if ($this->shouldCheckDbStatus($this->getRequest()) && !defined('SKIP_UPDATES') && !Update::isDbUpToDate()) {
// Prevent debug bar to be displayed when an admin user was connected with debug mode when codebase was updated.
Expand Down
1 change: 1 addition & 0 deletions src/Glpi/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public function __construct(private Kernel $kernel)
parent::__construct('GLPI CLI', GLPI_VERSION);

$this->kernel->boot();
$this->setDispatcher($this->kernel->getContainer()->get('event_dispatcher'));

$this->db = $DB;
$this->config = &$CFG_GLPI;
Expand Down
53 changes: 53 additions & 0 deletions src/Glpi/Controller/MaintenanceController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2024 Teclib' and contributors.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

declare(strict_types=1);

namespace Glpi\Controller;

use Symfony\Component\HttpFoundation\Response;

class MaintenanceController extends AbstractController
{
public function __invoke(): Response
{
/** @var array $CFG_GLPI */
global $CFG_GLPI;

return $this->render('maintenance.html.twig', [
'title' => "MAINTENANCE MODE",
'maintenance_text' => $CFG_GLPI["maintenance_text"] ?? "",
]);
}
}
87 changes: 87 additions & 0 deletions src/Glpi/Http/Listener/CheckMaintenanceListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2024 Teclib' and contributors.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

namespace Glpi\Http\Listener;

use Glpi\Controller\MaintenanceController;
use Glpi\Http\RequestPoliciesTrait;
use Glpi\Kernel\ListenersPriority;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;

final readonly class CheckMaintenanceListener implements EventSubscriberInterface
{
use RequestPoliciesTrait;

public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => ['onKernelRequest', ListenersPriority::REQUEST_LISTENERS_PRIORITIES[self::class]],
];
}

public function onKernelRequest(RequestEvent $event): void
{
if (!$event->isMainRequest()) {
return;
}

if (
$this->isFrontEndAssetEndpoint($event->getRequest())
|| $this->isSymfonyProfilerEndpoint($event->getRequest())
) {
return;
}

/** @var array $CFG_GLPI */
global $CFG_GLPI;

// Check maintenance mode
if (!isset($CFG_GLPI["maintenance_mode"]) || !$CFG_GLPI["maintenance_mode"]) {
return;
}

if ($event->getRequest()->query->get('skipMaintenance')) {
$_SESSION["glpiskipMaintenance"] = 1;
return;
}

if (isset($_SESSION["glpiskipMaintenance"]) && $_SESSION["glpiskipMaintenance"]) {
return;
}

$event->getRequest()->attributes->set('_controller', MaintenanceController::class);
}
}
2 changes: 2 additions & 0 deletions src/Glpi/Kernel/ListenersPriority.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ final class ListenersPriority
// It must be executed before executing any controller (except controllers related to front-end assets).
HttpListener\CheckDatabaseStatusListener::class => 450,

HttpListener\CheckMaintenanceListener::class => 425,

// Legacy config providers.
// FIXME: Reorganize them and transform them into HTTP request listeners to register them here directly.
LegacyConfigProviderListener::class => 425,
Expand Down

0 comments on commit 1160f4a

Please sign in to comment.