From 122be4b0b08225ff4436d24ee358a78a89b45660 Mon Sep 17 00:00:00 2001 From: Alex Rock Date: Fri, 20 Dec 2024 15:34:08 +0100 Subject: [PATCH] Move db update check out of StandardIncludes to its own listener+controller --- .../legacyConfigProviders.php | 1 - .../NeedsUpdateController.php} | 82 +++++++++---------- .../Listener/CheckIfUpdateNeededListener.php | 74 +++++++++++++++++ src/Glpi/Kernel/ListenersPriority.php | 4 + 4 files changed, 116 insertions(+), 45 deletions(-) rename src/Glpi/{Config/LegacyConfigurators/StandardIncludes.php => Controller/NeedsUpdateController.php} (60%) create mode 100644 src/Glpi/Http/Listener/CheckIfUpdateNeededListener.php diff --git a/dependency_injection/legacyConfigProviders.php b/dependency_injection/legacyConfigProviders.php index f3156877b45..8d31e1764f9 100644 --- a/dependency_injection/legacyConfigProviders.php +++ b/dependency_injection/legacyConfigProviders.php @@ -55,7 +55,6 @@ * ⚠ Here, ORDER of definition matters! */ - $services->set(LegacyConfigurators\StandardIncludes::class)->tag($tagName, ['priority' => 160]); $services->set(LegacyConfigurators\CleanPHPSelfParam::class)->tag($tagName, ['priority' => 150]); $services->set(LegacyConfigurators\SessionConfig::class)->tag($tagName, ['priority' => 130]); diff --git a/src/Glpi/Config/LegacyConfigurators/StandardIncludes.php b/src/Glpi/Controller/NeedsUpdateController.php similarity index 60% rename from src/Glpi/Config/LegacyConfigurators/StandardIncludes.php rename to src/Glpi/Controller/NeedsUpdateController.php index 2f4a8812e41..8cf4a01653f 100644 --- a/src/Glpi/Config/LegacyConfigurators/StandardIncludes.php +++ b/src/Glpi/Controller/NeedsUpdateController.php @@ -32,64 +32,60 @@ * --------------------------------------------------------------------- */ -namespace Glpi\Config\LegacyConfigurators; +namespace Glpi\Controller; use Config; +use DBmysql; use Glpi\Application\View\TemplateRenderer; -use Glpi\Config\ConfigProviderHasRequestTrait; -use Glpi\Config\ConfigProviderWithRequestInterface; -use Glpi\Config\LegacyConfigProviderInterface; -use Glpi\Http\RequestPoliciesTrait; use Glpi\System\Requirement\DatabaseTablesEngine; use Glpi\System\RequirementsManager; use Glpi\Toolbox\VersionParser; use Html; use Session; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpFoundation\StreamedResponse; use Toolbox; -use Update; -final class StandardIncludes implements LegacyConfigProviderInterface, ConfigProviderWithRequestInterface +class NeedsUpdateController extends AbstractController { - use ConfigProviderHasRequestTrait; - use RequestPoliciesTrait; + public function __invoke(string $key): Response + { + return new StreamedResponse($this->display(...)); + } - public function execute(): void + public function display(): void { - /** - * @var array $CFG_GLPI - */ - global $CFG_GLPI; + // Prevent debug bar to be displayed when an admin user was connected with debug mode when codebase was updated. + $debug_mode = $_SESSION['glpi_use_mode']; + Toolbox::setDebugMode(Session::NORMAL_MODE); - // 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. - $debug_mode = $_SESSION['glpi_use_mode']; - Toolbox::setDebugMode(Session::NORMAL_MODE); + /** @var DBmysql $DB */ + global $DB; - /** @var \DBmysql $DB */ - global $DB; + /** @var array $CFG_GLPI */ + global $CFG_GLPI; - $requirements = (new RequirementsManager())->getCoreRequirementList($DB); - $requirements->add(new DatabaseTablesEngine($DB)); + $requirements = (new RequirementsManager())->getCoreRequirementList($DB); + $requirements->add(new DatabaseTablesEngine($DB)); - $twig_params = [ - 'core_requirements' => $requirements, - 'try_again' => __('Try again'), - 'update_needed' => __('The GLPI codebase has been updated. The update of the GLPI database is necessary.'), - 'upgrade' => _sx('button', 'Upgrade'), - 'outdated_files' => __('You are trying to use GLPI with outdated files compared to the version of the database. Please install the correct GLPI files corresponding to the version of your database.'), - 'stable_release' => VersionParser::isStableRelease(GLPI_VERSION), - 'agree_unstable' => Config::agreeUnstableMessage(VersionParser::isDevVersion(GLPI_VERSION)), - 'outdated' => version_compare( - VersionParser::getNormalizedVersion($CFG_GLPI['version'] ?? '0.0.0-dev'), - VersionParser::getNormalizedVersion(GLPI_VERSION), - '>' - ) - ]; + $twig_params = [ + 'core_requirements' => $requirements, + 'try_again' => __('Try again'), + 'update_needed' => __('The GLPI codebase has been updated. The update of the GLPI database is necessary.'), + 'upgrade' => _sx('button', 'Upgrade'), + 'outdated_files' => __('You are trying to use GLPI with outdated files compared to the version of the database. Please install the correct GLPI files corresponding to the version of your database.'), + 'stable_release' => VersionParser::isStableRelease(GLPI_VERSION), + 'agree_unstable' => Config::agreeUnstableMessage(VersionParser::isDevVersion(GLPI_VERSION)), + 'outdated' => version_compare( + VersionParser::getNormalizedVersion($CFG_GLPI['version'] ?? '0.0.0-dev'), + VersionParser::getNormalizedVersion(GLPI_VERSION), + '>' + ) + ]; - Html::nullHeader(__('Update needed'), $CFG_GLPI["root_doc"]); - // language=Twig - echo TemplateRenderer::getInstance()->renderFromStringTemplate(<<renderFromStringTemplate(<<
@@ -127,9 +123,7 @@ public function execute(): void
TWIG, $twig_params); - Html::nullFooter(); - $_SESSION['glpi_use_mode'] = $debug_mode; - exit(); - } + Html::nullFooter(); + $_SESSION['glpi_use_mode'] = $debug_mode; } } diff --git a/src/Glpi/Http/Listener/CheckIfUpdateNeededListener.php b/src/Glpi/Http/Listener/CheckIfUpdateNeededListener.php new file mode 100644 index 00000000000..2ab6de2a781 --- /dev/null +++ b/src/Glpi/Http/Listener/CheckIfUpdateNeededListener.php @@ -0,0 +1,74 @@ +. + * + * --------------------------------------------------------------------- + */ + +namespace Glpi\Http\Listener; + +use Glpi\Controller\NeedsUpdateController; +use Glpi\Http\RequestPoliciesTrait; +use Glpi\Kernel\ListenersPriority; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\HttpKernel\Event\RequestEvent; +use Symfony\Component\HttpKernel\KernelEvents; +use Update; + +final readonly class CheckIfUpdateNeededListener 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 ( + \defined('SKIP_UPDATES') + || Update::isDbUpToDate() + || !$this->shouldCheckDbStatus($event->getRequest()) + ) { + return; + } + + $event->getRequest()->attributes->set('_controller', NeedsUpdateController::class); + } +} diff --git a/src/Glpi/Kernel/ListenersPriority.php b/src/Glpi/Kernel/ListenersPriority.php index 58c41304d36..489df393991 100644 --- a/src/Glpi/Kernel/ListenersPriority.php +++ b/src/Glpi/Kernel/ListenersPriority.php @@ -66,6 +66,10 @@ final class ListenersPriority // It must be executed before executing any controller (except controllers related to front-end assets). HttpListener\CheckDatabaseStatusListener::class => 450, + // This listener will ensure that GLPI is not being updated, or does not need a database update. + // Must also be executed before other controllers, since it defines its own controller. + HttpListener\CheckIfUpdateNeededListener::class => 440, + HttpListener\CheckMaintenanceListener::class => 425, // Legacy config providers.