Skip to content

Commit

Permalink
Move db update check out of StandardIncludes to its own listener+cont…
Browse files Browse the repository at this point in the history
…roller
  • Loading branch information
Pierstoval authored Dec 20, 2024
1 parent 1160f4a commit 122be4b
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 45 deletions.
1 change: 0 additions & 1 deletion dependency_injection/legacyConfigProviders.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(<<<TWIG
Html::nullHeader(__('Update needed'));
// language=Twig
echo TemplateRenderer::getInstance()->renderFromStringTemplate(<<<TWIG
<div class="container-fluid mb-4">
<div class="row justify-content-evenly">
<div class="col-12 col-xxl-6">
Expand Down Expand Up @@ -127,9 +123,7 @@ public function execute(): void
</div>
</div>
TWIG, $twig_params);
Html::nullFooter();
$_SESSION['glpi_use_mode'] = $debug_mode;
exit();
}
Html::nullFooter();
$_SESSION['glpi_use_mode'] = $debug_mode;
}
}
74 changes: 74 additions & 0 deletions src/Glpi/Http/Listener/CheckIfUpdateNeededListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?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\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);
}
}
4 changes: 4 additions & 0 deletions src/Glpi/Kernel/ListenersPriority.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 122be4b

Please sign in to comment.