diff --git a/core-bundle/src/EventListener/LegacyLoginConstantsListener.php b/core-bundle/src/EventListener/LegacyLoginConstantsListener.php index 2ccb27a9fe5..6b8d2301ea3 100644 --- a/core-bundle/src/EventListener/LegacyLoginConstantsListener.php +++ b/core-bundle/src/EventListener/LegacyLoginConstantsListener.php @@ -34,14 +34,6 @@ public function __construct(ContaoFramework $framework) public function __invoke(RequestEvent $event): void { - // Set the legacy login constants if the legacy framework was initialized before. - // Otherwise allow the framework to set them itself during initialize. - if (!$this->framework->isInitialized()) { - $this->framework->setLoginConstantsOnInit(true); - - return; - } - $this->framework->setLoginConstants($event->getRequest()); } } diff --git a/core-bundle/src/Framework/ContaoFramework.php b/core-bundle/src/Framework/ContaoFramework.php index df8a7c40369..d5087668222 100644 --- a/core-bundle/src/Framework/ContaoFramework.php +++ b/core-bundle/src/Framework/ContaoFramework.php @@ -99,7 +99,7 @@ class ContaoFramework implements ContaoFrameworkInterface, ContainerAwareInterfa /** * @var bool */ - private $setLoginConstants = false; + private $setLoginConstantsOnInit = false; public function __construct(RequestStack $requestStack, ScopeMatcher $scopeMatcher, TokenChecker $tokenChecker, Filesystem $filesystem, string $projectDir, int $errorLevel) { @@ -181,21 +181,18 @@ public function getAdapter($class): Adapter return $this->adapterCache[$class]; } - /** - * Allows the login constants to be set during initialize. - * - * @deprecated Deprecated since Contao 4.9, to be removed in Contao 5.0 - */ - public function setLoginConstantsOnInit(bool $setLoginConstants = true): void - { - $this->setLoginConstants = $setLoginConstants; - } - /** * @deprecated Deprecated since Contao 4.9, to be removed in Contao 5.0 */ public function setLoginConstants(Request $request = null): void { + // If the framework has not been initialized yet, set the login constants on init (#4968) + if (!$this->isInitialized()) { + $this->setLoginConstantsOnInit = true; + + return; + } + if (null !== $request && $this->scopeMatcher->isFrontendRequest($request)) { \define('BE_USER_LOGGED_IN', $this->tokenChecker->hasBackendUser() && $this->tokenChecker->isPreviewMode()); \define('FE_USER_LOGGED_IN', $this->tokenChecker->hasFrontendUser()); @@ -225,7 +222,7 @@ private function setConstants(): void $request = $this->requestStack->getCurrentRequest(); // Define the login status constants (see #4099, #5279) - if ($this->setLoginConstants || null === $request) { + if ($this->setLoginConstantsOnInit || null === $request) { $this->setLoginConstants($request); } diff --git a/core-bundle/tests/Framework/ContaoFrameworkTest.php b/core-bundle/tests/Framework/ContaoFrameworkTest.php index 7d11f31ecda..66eb2b3e7c1 100644 --- a/core-bundle/tests/Framework/ContaoFrameworkTest.php +++ b/core-bundle/tests/Framework/ContaoFrameworkTest.php @@ -264,9 +264,12 @@ public function testSetsTheLoginConstantsOnInitIfEnabled(): void $request->attributes->set('_route', 'dummy'); $request->attributes->set('_scope', 'frontend'); - $framework = $this->mockFramework(); + $framework = $this->mockFramework($request); $framework->setContainer($this->getContainerWithContaoConfiguration()); - $framework->setLoginConstantsOnInit(true); + + // Call setLoginConstants before initialize + $framework->setLoginConstants($request); + $framework->initialize(); $this->assertTrue(\defined('BE_USER_LOGGED_IN')); @@ -337,7 +340,7 @@ public function testInitializesTheFrameworkInPreviewMode(): void $framework = $this->mockFramework($request, null, $tokenChecker); $framework->setContainer($this->getContainerWithContaoConfiguration()); - $framework->setLoginConstantsOnInit(true); + $framework->setLoginConstants($request); $framework->initialize(); $this->assertTrue(\defined('TL_MODE'));