‪TYPO3CMS  ‪main
FrontendRestrictionContainer.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
24 
30 {
34  protected array ‪$defaultRestrictionTypes = [
35  DeletedRestriction::class,
36  WorkspaceRestriction::class,
37  HiddenRestriction::class,
38  StartTimeRestriction::class,
39  EndTimeRestriction::class,
40  FrontendGroupRestriction::class,
41  ];
42 
44 
49  public function ‪__construct(‪Context ‪$context = null)
50  {
51  $this->context = ‪$context ?? GeneralUtility::makeInstance(Context::class);
52  foreach ($this->defaultRestrictionTypes as $restrictionType) {
53  $this->‪add($this->‪createRestriction($restrictionType));
54  }
55  }
56 
65  public function ‪buildExpression(array $queriedTables, ‪ExpressionBuilder $expressionBuilder): ‪CompositeExpression
66  {
67  $constraints = [];
68  foreach ($this->restrictions as $restriction) {
69  foreach ($queriedTables as $tableAlias => $tableName) {
70  $disableRestriction = false;
71  if ($restriction instanceof ‪HiddenRestriction || $restriction instanceof ‪StartTimeRestriction || $restriction instanceof ‪EndTimeRestriction) {
72  $visibilityAspect = $this->context->getAspect('visibility');
73  if ($restriction instanceof ‪HiddenRestriction) {
74  // If display of hidden records is requested, we must disable the hidden restriction.
75  if ($tableName === 'pages') {
76  $disableRestriction = $visibilityAspect->includeHiddenPages();
77  } else {
78  $disableRestriction = $visibilityAspect->includeHiddenContent();
79  }
80  }
81  if ($restriction instanceof ‪StartTimeRestriction || $restriction instanceof ‪EndTimeRestriction) {
82  $disableRestriction = $visibilityAspect->includeScheduledRecords();
83  }
84  }
85  if (!$disableRestriction) {
86  $constraints[] = $restriction->buildExpression([$tableAlias => $tableName], $expressionBuilder);
87  }
88  }
89  }
90  return $expressionBuilder->‪and(...$constraints);
91  }
92 
93  protected function ‪createRestriction($restrictionClass): ‪QueryRestrictionInterface
94  {
95  if ($restrictionClass === WorkspaceRestriction::class) {
96  return GeneralUtility::makeInstance($restrictionClass, (int)$this->context->getPropertyFromAspect('workspace', 'id', 0));
97  }
98  if ($restrictionClass === FrontendGroupRestriction::class) {
99  return GeneralUtility::makeInstance($restrictionClass, $this->context->getPropertyFromAspect('frontend.user', 'groupIds', []));
100  }
101 
102  return parent::createRestriction($restrictionClass);
103  }
104 }
‪TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction
Definition: HiddenRestriction.php:27
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder
Definition: ExpressionBuilder.php:40
‪TYPO3\CMS\Core\Database\Query\Restriction\EndTimeRestriction
Definition: EndTimeRestriction.php:27
‪TYPO3\CMS\Core\Database\Query\Restriction\QueryRestrictionInterface
Definition: QueryRestrictionInterface.php:27
‪TYPO3\CMS\Core\Database\Query\Restriction\StartTimeRestriction
Definition: StartTimeRestriction.php:27
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:54
‪TYPO3\CMS\Core\Database\Query\Expression\CompositeExpression
Definition: CompositeExpression.php:27
‪TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer\$context
‪Context $context
Definition: FrontendRestrictionContainer.php:43
‪TYPO3\CMS\Core\Database\Query\Restriction
Definition: AbstractRestrictionContainer.php:18
‪TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer\buildExpression
‪CompositeExpression buildExpression(array $queriedTables, ExpressionBuilder $expressionBuilder)
Definition: FrontendRestrictionContainer.php:65
‪TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer\__construct
‪__construct(Context $context=null)
Definition: FrontendRestrictionContainer.php:49
‪TYPO3\CMS\Core\Database\Query\Restriction\AbstractRestrictionContainer
Definition: AbstractRestrictionContainer.php:28
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder\and
‪and(CompositeExpression|\Doctrine\DBAL\Query\Expression\CompositeExpression|string|null ... $expressions,)
Definition: ExpressionBuilder.php:50
‪TYPO3\CMS\Core\Database\Query\Restriction\AbstractRestrictionContainer\add
‪add(QueryRestrictionInterface $restriction)
Definition: AbstractRestrictionContainer.php:90
‪TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer\createRestriction
‪createRestriction($restrictionClass)
Definition: FrontendRestrictionContainer.php:93
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer
Definition: FrontendRestrictionContainer.php:30
‪TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer\$defaultRestrictionTypes
‪array $defaultRestrictionTypes
Definition: FrontendRestrictionContainer.php:34