‪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 
46  protected ‪$context;
47 
52  public function ‪__construct(‪Context ‪$context = null)
53  {
54  $this->context = ‪$context ?? GeneralUtility::makeInstance(Context::class);
55  foreach ($this->defaultRestrictionTypes as $restrictionType) {
56  $this->‪add($this->‪createRestriction($restrictionType));
57  }
58  }
59 
68  public function ‪buildExpression(array $queriedTables, ‪ExpressionBuilder $expressionBuilder): ‪CompositeExpression
69  {
70  $constraints = [];
71  foreach ($this->restrictions as $restriction) {
72  foreach ($queriedTables as $tableAlias => $tableName) {
73  $disableRestriction = false;
74  if ($restriction instanceof ‪HiddenRestriction || $restriction instanceof ‪StartTimeRestriction || $restriction instanceof ‪EndTimeRestriction) {
75  $visibilityAspect = $this->context->getAspect('visibility');
76  if ($restriction instanceof ‪HiddenRestriction) {
77  // If display of hidden records is requested, we must disable the hidden restriction.
78  if ($tableName === 'pages') {
79  $disableRestriction = $visibilityAspect->includeHiddenPages();
80  } else {
81  $disableRestriction = $visibilityAspect->includeHiddenContent();
82  }
83  }
84  if ($restriction instanceof StartTimeRestriction || $restriction instanceof EndTimeRestriction) {
85  $disableRestriction = $visibilityAspect->includeScheduledRecords();
86  }
87  }
88  if (!$disableRestriction) {
89  $constraints[] = $restriction->buildExpression([$tableAlias => $tableName], $expressionBuilder);
90  }
91  }
92  }
93  return $expressionBuilder->‪and(...$constraints);
94  }
95 
96  protected function ‪createRestriction($restrictionClass): ‪QueryRestrictionInterface
97  {
98  if ($restrictionClass === WorkspaceRestriction::class) {
99  return GeneralUtility::makeInstance($restrictionClass, (int)$this->context->getPropertyFromAspect('workspace', 'id', 0));
100  }
101  if ($restrictionClass === FrontendGroupRestriction::class) {
102  return GeneralUtility::makeInstance($restrictionClass, $this->context->getPropertyFromAspect('frontend.user', 'groupIds', []));
103  }
104 
105  return parent::createRestriction($restrictionClass);
106  }
107 }
‪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\Database\Query\Expression\ExpressionBuilder\and
‪and(CompositeExpression|string|null ... $expressions)
Definition: ExpressionBuilder.php:69
‪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:45
‪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:67
‪TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer\__construct
‪__construct(Context $context=null)
Definition: FrontendRestrictionContainer.php:51
‪TYPO3\CMS\Core\Database\Query\Restriction\AbstractRestrictionContainer
Definition: AbstractRestrictionContainer.php:28
‪TYPO3\CMS\Core\Database\Query\Restriction\AbstractRestrictionContainer\add
‪add(QueryRestrictionInterface $restriction)
Definition: AbstractRestrictionContainer.php:77
‪TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer\createRestriction
‪createRestriction($restrictionClass)
Definition: FrontendRestrictionContainer.php:95
‪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