Skip to content

Commit

Permalink
Merge branch '4.x' of github.com:contao/contao into feature/language-…
Browse files Browse the repository at this point in the history
…dependent-module-configuration
  • Loading branch information
bytehead committed Dec 30, 2021
2 parents 83aab2e + 69129f9 commit 127497d
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 14 deletions.
11 changes: 11 additions & 0 deletions DEPRECATED.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ The `$GLOBALS['TL_AUTO_ITEM']` variable has been deprecated and will be removed
together with the `useAutoItem` setting in Contao 5.0. Using auto items can no
longer be disabled then.

## BE_USER_LOGGED_IN

The constant `BE_USER_LOGGED_IN` has been deprecated and will be removed in
Contao 5.0. It was historically used to preview unpublished elements in the
front end. Use the token checker service to check the separate cases instead:

```php
$hasBackendUser = System::getContainer()->get('contao.security.token_checker')->hasBackendUser();
$showUnpublished = System::getContainer()->get('contao.security.token_checker')->isPreviewMode();
```

## FE_USER_LOGGED_IN

The constant `FE_USER_LOGGED_IN` has been deprecated and will be removed in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ protected function compile()
$time = Date::floorToMinute();

// Find the boundaries
$objMinMax = $this->Database->query("SELECT MIN(startTime) AS dateFrom, MAX(endTime) AS dateTo, MAX(repeatEnd) AS repeatUntil FROM tl_calendar_events WHERE pid IN(" . implode(',', array_map('\intval', $this->cal_calendar)) . ")" . (!BE_USER_LOGGED_IN ? " AND published='1' AND (start='' OR start<='$time') AND (stop='' OR stop>'$time')" : ""));
$blnShowUnpublished = System::getContainer()->get('contao.security.token_checker')->isPreviewMode();
$objMinMax = $this->Database->query("SELECT MIN(startTime) AS dateFrom, MAX(endTime) AS dateTo, MAX(repeatEnd) AS repeatUntil FROM tl_calendar_events WHERE pid IN(" . implode(',', array_map('\intval', $this->cal_calendar)) . ")" . (!$blnShowUnpublished ? " AND published='1' AND (start='' OR start<='$time') AND (stop='' OR stop>'$time')" : ""));
$dateFrom = $objMinMax->dateFrom;
$dateTo = $objMinMax->dateTo;
$repeatUntil = $objMinMax->repeatUntil;
Expand Down
7 changes: 5 additions & 2 deletions core-bundle/src/DataCollector/ContaoDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use Contao\CoreBundle\Framework\FrameworkAwareInterface;
use Contao\CoreBundle\Framework\FrameworkAwareTrait;
use Contao\CoreBundle\Security\Authentication\Token\TokenChecker;
use Contao\CoreBundle\Util\PackageUtil;
use Contao\LayoutModel;
use Contao\Model\Registry;
Expand All @@ -32,13 +33,15 @@ class ContaoDataCollector extends DataCollector implements FrameworkAwareInterfa
{
use FrameworkAwareTrait;

private TokenChecker $tokenChecker;
private bool $legacyRouting;
private string $projectDir;
private bool $prependLocale;
private string $urlSuffix;

public function __construct(bool $legacyRouting, string $projectDir, bool $prependLocale, string $urlSuffix)
public function __construct(TokenChecker $tokenChecker, bool $legacyRouting, string $projectDir, bool $prependLocale, string $urlSuffix)
{
$this->tokenChecker = $tokenChecker;
$this->legacyRouting = $legacyRouting;
$this->projectDir = $projectDir;
$this->prependLocale = $prependLocale;
Expand Down Expand Up @@ -165,7 +168,7 @@ private function addSummaryData(): void
'framework' => $framework,
'models' => $modelCount,
'frontend' => isset($GLOBALS['objPage']),
'preview' => \defined('BE_USER_LOGGED_IN') && true === BE_USER_LOGGED_IN,
'preview' => $this->tokenChecker->isPreviewMode(),
'layout' => $this->getLayoutName(),
'template' => $this->getTemplateName(),
'legacy_routing' => $this->legacyRouting,
Expand Down
1 change: 1 addition & 0 deletions core-bundle/src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ services:
contao.data_collector:
class: Contao\CoreBundle\DataCollector\ContaoDataCollector
arguments:
- '@contao.security.token_checker'
- '%contao.legacy_routing%'
- '%kernel.project_dir%'
- '%contao.prepend_locale%'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,10 @@ public function renderPage($pageModel)

// Inherit the settings from the parent pages
$objPage->loadDetails();
$blnShowUnpublished = System::getContainer()->get('contao.security.token_checker')->isPreviewMode();

// Trigger the 404 page if the page is not published and the front end preview is not active (see #374)
if (!BE_USER_LOGGED_IN && !$objPage->isPublic)
if (!$blnShowUnpublished && !$objPage->isPublic)
{
throw new PageNotFoundException('Page not found: ' . Environment::get('uri'));
}
Expand All @@ -242,7 +243,7 @@ public function renderPage($pageModel)

// Exit if the root page has not been published (see #2425)
// Do not try to load the 404 page, it can cause an infinite loop!
if (!BE_USER_LOGGED_IN && !$objPage->rootIsPublic)
if (!$blnShowUnpublished && !$objPage->rootIsPublic)
{
throw new PageNotFoundException('Page not found: ' . Environment::get('uri'));
}
Expand Down
2 changes: 1 addition & 1 deletion core-bundle/src/Resources/contao/library/Contao/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1303,7 +1303,7 @@ protected static function isPreviewMode(array $arrOptions)
return false;
}

return \defined('BE_USER_LOGGED_IN') && BE_USER_LOGGED_IN === true;
return System::getContainer()->get('contao.security.token_checker')->isPreviewMode();
}
}

Expand Down
3 changes: 2 additions & 1 deletion core-bundle/src/Resources/contao/modules/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ protected function renderNavigation($pid, $level=1, $host=null, $language=null)
$items = array();
$security = System::getContainer()->get('security.helper');
$isMember = $security->isGranted('ROLE_MEMBER');
$blnShowUnpublished = System::getContainer()->get('contao.security.token_checker')->isPreviewMode();

$objTemplate = new FrontendTemplate($this->navigationTpl ?: 'nav_default');
$objTemplate->pid = $pid;
Expand Down Expand Up @@ -362,7 +363,7 @@ protected function renderNavigation($pid, $level=1, $host=null, $language=null)
}

// Hide the link if the target page is invisible
if (!$objNext instanceof PageModel || (!$objNext->loadDetails()->isPublic && !BE_USER_LOGGED_IN))
if (!$objNext instanceof PageModel || (!$objNext->loadDetails()->isPublic && !$blnShowUnpublished))
{
continue 2;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ protected function compile()
$pages = array($objPage);
$items = array();

$blnShowUnpublished = System::getContainer()->get('contao.security.token_checker')->isPreviewMode();

// Get all pages up to the root page
$objPages = PageModel::findParentsById($objPage->pid);

Expand Down Expand Up @@ -97,7 +99,7 @@ protected function compile()

for ($i=(\count($pages)-1); $i>0; $i--)
{
if (($pages[$i]->hide && !$this->showHidden) || (!$pages[$i]->published && !BE_USER_LOGGED_IN))
if (($pages[$i]->hide && !$this->showHidden) || (!$pages[$i]->published && !$blnShowUnpublished))
{
continue;
}
Expand Down
3 changes: 2 additions & 1 deletion core-bundle/src/Resources/contao/pages/PageRegular.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ protected function prepare($objPage)
$request->setLocale($locale);

$this->responseContext = $container->get('contao.routing.response_context_factory')->createContaoWebpageResponseContext($objPage);
$blnShowUnpublished = $container->get('contao.security.token_checker')->isPreviewMode();

System::loadLanguageFile('default');

Expand Down Expand Up @@ -147,7 +148,7 @@ protected function prepare($objPage)
foreach ($arrModules as $arrModule)
{
// Disabled module
if (!BE_USER_LOGGED_IN && !($arrModule['enable'] ?? null))
if (!$blnShowUnpublished && !($arrModule['enable'] ?? null))
{
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
$GoogleAnalyticsId = 'UA-XXXXX-X';

// DO NOT EDIT ANYTHING BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING!
if ('UA-XXXXX-X' != $GoogleAnalyticsId && !BE_USER_LOGGED_IN && !$this->hasAuthenticatedBackendUser()): ?>
if ('UA-XXXXX-X' != $GoogleAnalyticsId && !$this->hasAuthenticatedBackendUser()): ?>

<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ $MatomoSite = 0;
$MatomoPath = '//www.example.com/matomo/';

// DO NOT EDIT ANYTHING BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING!
if ($MatomoSite > 0 && '//www.example.com/matomo/' != $MatomoPath && !BE_USER_LOGGED_IN && !$this->hasAuthenticatedBackendUser()): ?>
if ($MatomoSite > 0 && '//www.example.com/matomo/' != $MatomoPath && !$this->hasAuthenticatedBackendUser()): ?>

<script>
var _paq = window._paq = window._paq || [];
Expand Down
54 changes: 52 additions & 2 deletions core-bundle/tests/DataCollector/ContaoDataCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Contao\ContentImage;
use Contao\ContentText;
use Contao\CoreBundle\DataCollector\ContaoDataCollector;
use Contao\CoreBundle\Security\Authentication\Token\TokenChecker;
use Contao\CoreBundle\Tests\Fixtures\DataCollector\TestClass;
use Contao\CoreBundle\Tests\Fixtures\DataCollector\vendor\foo\bar\BundleTestClass;
use Contao\CoreBundle\Tests\TestCase;
Expand Down Expand Up @@ -106,6 +107,49 @@ public function testCollectsDataInFrontEnd(): void
unset($GLOBALS['objPage']);
}

public function testSetsTheFrontendPreviewFromTokenChecker(): void
{
$layout = $this->mockClassWithProperties(LayoutModel::class);
$layout->name = 'Default';
$layout->id = 2;
$layout->template = 'fe_page';

$adapter = $this->mockConfiguredAdapter(['findByPk' => $layout]);
$framework = $this->mockContaoFramework([LayoutModel::class => $adapter]);

$page = $this->mockClassWithProperties(PageModel::class);
$page->id = 2;

$GLOBALS['objPage'] = $page;

$tokenChecker = $this->createMock(TokenChecker::class);
$tokenChecker
->expects($this->once())
->method('isPreviewMode')
->willReturn(true)
;

$collector = $this->getDataCollector(false, false, '.html', $tokenChecker);
$collector->setFramework($framework);
$collector->collect(new Request(), new Response());

$this->assertSame(
[
'version' => PackageUtil::getContaoVersion(),
'framework' => false,
'models' => 0,
'frontend' => true,
'preview' => true,
'layout' => 'Default (ID 2)',
'template' => 'fe_page',
'legacy_routing' => false,
],
$collector->getSummary()
);

unset($GLOBALS['objPage']);
}

public function testStoresTheLegacyRoutingData(bool $legacyRouting = false, bool $prependLocale = false, string $urlSuffix = '.html'): void
{
$collector = $this->getDataCollector($legacyRouting, $prependLocale, $urlSuffix);
Expand Down Expand Up @@ -179,8 +223,14 @@ public function testReturnsAnEmptyArrayIfTheKeyIsUnknown(): void
$this->assertSame([], $method->invokeArgs($collector, ['foo']));
}

private function getDataCollector(bool $legacyRouting = false, bool $prependLocale = false, string $urlSuffix = '.html'): ContaoDataCollector
private function getDataCollector(bool $legacyRouting = false, bool $prependLocale = false, string $urlSuffix = '.html', TokenChecker $tokenChecker = null): ContaoDataCollector
{
return new ContaoDataCollector($legacyRouting, \dirname(__DIR__).'/Fixtures/DataCollector', $prependLocale, $urlSuffix);
return new ContaoDataCollector(
$tokenChecker ?? $this->createMock(TokenChecker::class),
$legacyRouting,
\dirname(__DIR__).'/Fixtures/DataCollector',
$prependLocale,
$urlSuffix
);
}
}
2 changes: 1 addition & 1 deletion news-bundle/src/Resources/contao/models/NewsModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public static function findPublishedByPids($arrPids, $blnFeatured=null, $intLimi
}

// Never return unpublished elements in the back end, so they don't end up in the RSS feed
if (!BE_USER_LOGGED_IN || TL_MODE == 'BE')
if (!static::isPreviewMode($arrOptions) || TL_MODE == 'BE')
{
$time = Date::floorToMinute();
$arrColumns[] = "$t.published='1' AND ($t.start='' OR $t.start<='$time') AND ($t.stop='' OR $t.stop>'$time')";
Expand Down

0 comments on commit 127497d

Please sign in to comment.