Skip to content

Make SessionConfig an http listener instead of just legacy provider #18588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion dependency_injection/legacyConfigProviders.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
*/

$services->set(LegacyConfigurators\CleanPHPSelfParam::class)->tag($tagName, ['priority' => 150]);
$services->set(LegacyConfigurators\SessionConfig::class)->tag($tagName, ['priority' => 130]);

// FIXME: This class MUST stay at the end until the entire config is revamped.
$services->set(LegacyConfigurators\ConfigRest::class)->tag($tagName, ['priority' => 10]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,41 +32,48 @@
* ---------------------------------------------------------------------
*/

namespace Glpi\Config\LegacyConfigurators;
namespace Glpi\Http\Listener;

use Glpi\Config\ConfigProviderHasRequestTrait;
use Glpi\Config\ConfigProviderWithRequestInterface;
use Glpi\Config\LegacyConfigProviderInterface;
use Glpi\Kernel\ListenersPriority;
use Glpi\Toolbox\URL;
use Session;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;

final class SessionConfig implements LegacyConfigProviderInterface, ConfigProviderWithRequestInterface
final class SessionVariables implements EventSubscriberInterface
{
use ConfigProviderHasRequestTrait;
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => ['onKernelRequest', ListenersPriority::REQUEST_LISTENERS_PRIORITIES[self::class]],
];
}

public function execute(): void
public function onKernelRequest(RequestEvent $event): void
{
if (!isset($_SESSION["MESSAGE_AFTER_REDIRECT"])) {
$_SESSION["MESSAGE_AFTER_REDIRECT"] = [];
if (!$event->isMainRequest()) {
return;
}

$request = $this->getRequest();
$request = $event->getRequest();

// Manage force tab
if ($request->query->has('forcetab')) {
$itemtype = URL::extractItemtypeFromUrlPath($request->getPathInfo());
if ($itemtype !== null) {
Session::setActiveTab($itemtype, $_REQUEST['forcetab']);
Session::setActiveTab($itemtype, $request->get('forcetab'));
}
}

// Manage tabs
if (isset($_REQUEST['glpi_tab']) && isset($_REQUEST['itemtype'])) {
Session::setActiveTab($_REQUEST['itemtype'], $_REQUEST['glpi_tab']);
if ($request->get('glpi_tab') && $request->get('itemtype')) {
Session::setActiveTab($request->get('itemtype'), $request->get('glpi_tab'));
}

// Override list-limit if choosen
if (isset($_REQUEST['glpilist_limit'])) {
$_SESSION['glpilist_limit'] = $_REQUEST['glpilist_limit'];
if ($request->get('glpilist_limit')) {
$_SESSION['glpilist_limit'] = $request->get('glpilist_limit');
}
}
}
12 changes: 10 additions & 2 deletions src/Glpi/Kernel/ListenersPriority.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,23 @@ final class ListenersPriority
HttpListener\LegacyItemtypeRouteListener::class => 375,

// Legacy URLs redirections.
HttpListener\RedirectLegacyRouteListener::class => 350,
// Must be executed before the Symfony router, to prevent `NotFoundHttpException` to be thrown.
//
// Symfony's Router priority is 32.
// @see \Symfony\Component\HttpKernel\EventListener\RouterListener::getSubscribedEvents()
HttpListener\RedirectLegacyRouteListener::class => 33,

// This listener allows matching plugins routes at runtime,
// that's why it's executed right after Symfony's Router,
// and also after GLPI's config is set.
//
// Symfony's Router priority is 32.
// @see \Symfony\Component\HttpKernel\EventListener\RouterListener::getSubscribedEvents()
HttpListener\PluginsRouterListener::class => 31,
HttpListener\PluginsRouterListener::class => 31,

// Update session variables according to request parameters.
// Must be called as late as possible, just before controllers execution.
HttpListener\SessionVariables::class => 0,
];

private function __construct()
Expand Down
5 changes: 5 additions & 0 deletions src/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ public static function start()
if (!isset($_SESSION['glpilanguage'])) {
$_SESSION['glpilanguage'] = Session::getPreferredLanguage();
}

// Init messages array
if (!isset($_SESSION["MESSAGE_AFTER_REDIRECT"])) {
$_SESSION["MESSAGE_AFTER_REDIRECT"] = [];
}
}


Expand Down
4 changes: 2 additions & 2 deletions tests/functional/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ public function testAddMessageAfterRedirect()
$warn_msg = 'There was a warning. Be carefull.';
$info_msg = 'All goes well. Or not... Who knows ;)';

$this->array($_SESSION)->notHasKey('MESSAGE_AFTER_REDIRECT');
$this->array($_SESSION['MESSAGE_AFTER_REDIRECT'])->isEmpty();

//test add message in cron mode
$_SESSION['glpicronuserrunning'] = 'cron_phpunit';
\Session::addMessageAfterRedirect($err_msg, false, ERROR);
//adding a message in "cron mode" does not add anything in the session
$this->array($_SESSION)->notHasKey('MESSAGE_AFTER_REDIRECT');
$this->array($_SESSION['MESSAGE_AFTER_REDIRECT'])->isEmpty();

//set not running from cron
unset($_SESSION['glpicronuserrunning']);
Expand Down
Loading