Skip to content

Commit

Permalink
simplify implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzmg committed Jul 17, 2022
1 parent 7abe0f2 commit 7d72d58
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 23 deletions.
Expand Up @@ -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());
}
}
21 changes: 9 additions & 12 deletions core-bundle/src/Framework/ContaoFramework.php
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
}

Expand Down
9 changes: 6 additions & 3 deletions core-bundle/tests/Framework/ContaoFrameworkTest.php
Expand Up @@ -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'));
Expand Down Expand Up @@ -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'));
Expand Down

0 comments on commit 7d72d58

Please sign in to comment.