‪TYPO3CMS  10.4
PagePermissionRestriction.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 
23 
42 {
46  protected ‪$permissions;
47 
51  protected ‪$userAspect;
52 
54  {
55  $this->permissions = ‪$permissions;
56  $this->userAspect = ‪$userAspect;
57  }
58 
66  public function ‪buildExpression(array $queriedTables, ‪ExpressionBuilder $expressionBuilder): ‪CompositeExpression
67  {
68  $constraints = [];
69 
70  foreach ($queriedTables as $tableAlias => $tableName) {
71  if ($tableName !== 'pages') {
72  continue;
73  }
74 
75  $constraint = $this->‪buildUserConstraints($expressionBuilder, $tableAlias);
76  if ($constraint) {
77  $constraints[] = $expressionBuilder->‪andX($constraint);
78  }
79  }
80 
81  return $expressionBuilder->‪andX(...$constraints);
82  }
83 
90  protected function ‪buildUserConstraints(ExpressionBuilder $expressionBuilder, string $tableAlias)
91  {
92  if (!$this->userAspect->isLoggedIn()) {
93  return $expressionBuilder->comparison(1, ‪ExpressionBuilder::EQ, 0);
94  }
95  if ($this->userAspect->isAdmin()) {
96  return null;
97  }
98  // User permissions
99  $constraint = $expressionBuilder->orX(
100  $expressionBuilder->comparison(
101  $expressionBuilder->bitAnd($tableAlias . '.perms_everybody', $this->permissions),
103  $this->permissions
104  ),
105  $expressionBuilder->andX(
106  $expressionBuilder->eq($tableAlias . '.perms_userid', $this->userAspect->get('id')),
107  $expressionBuilder->comparison(
108  $expressionBuilder->bitAnd($tableAlias . '.perms_user', $this->permissions),
110  $this->permissions
111  )
112  )
113  );
114 
115  // User groups (if any are set)
116  $groupIds = array_map('intval', $this->userAspect->getGroupIds());
117  if (!empty($groupIds)) {
118  $constraint->add(
119  $expressionBuilder->andX(
120  $expressionBuilder->in(
121  $tableAlias . '.perms_groupid',
122  $groupIds
123  ),
124  $expressionBuilder->comparison(
125  $expressionBuilder->bitAnd($tableAlias . '.perms_group', $this->permissions),
127  $this->permissions
128  )
129  )
130  );
131  }
132  return $constraint;
133  }
134 }
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder
Definition: ExpressionBuilder.php:35
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder\EQ
‪const EQ
Definition: ExpressionBuilder.php:36
‪TYPO3\CMS\Core\Database\Query\Restriction\PagePermissionRestriction\buildExpression
‪CompositeExpression buildExpression(array $queriedTables, ExpressionBuilder $expressionBuilder)
Definition: PagePermissionRestriction.php:64
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder\bitAnd
‪string bitAnd(string $fieldName, int $value)
Definition: ExpressionBuilder.php:391
‪TYPO3\CMS\Core\Database\Query\Restriction\QueryRestrictionInterface
Definition: QueryRestrictionInterface.php:27
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder\eq
‪string eq(string $fieldName, $value)
Definition: ExpressionBuilder.php:109
‪TYPO3\CMS\Core\Database\Query\Restriction\PagePermissionRestriction\$userAspect
‪UserAspect $userAspect
Definition: PagePermissionRestriction.php:49
‪TYPO3\CMS\Core\Database\Query\Restriction\PagePermissionRestriction\$permissions
‪int $permissions
Definition: PagePermissionRestriction.php:45
‪TYPO3\CMS\Core\Database\Query\Expression\CompositeExpression
Definition: CompositeExpression.php:25
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder\in
‪string in(string $fieldName, $value)
Definition: ExpressionBuilder.php:244
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder\andX
‪CompositeExpression andX(... $expressions)
Definition: ExpressionBuilder.php:70
‪TYPO3\CMS\Core\Database\Query\Restriction
Definition: AbstractRestrictionContainer.php:18
‪TYPO3\CMS\Core\Database\Query\Restriction\PagePermissionRestriction\__construct
‪__construct(UserAspect $userAspect, int $permissions)
Definition: PagePermissionRestriction.php:51
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder\orX
‪CompositeExpression orX(... $expressions)
Definition: ExpressionBuilder.php:82
‪TYPO3\CMS\Core\Database\Query\Restriction\PagePermissionRestriction
Definition: PagePermissionRestriction.php:42
‪TYPO3\CMS\Core\Database\Query\Restriction\PagePermissionRestriction\buildUserConstraints
‪string CompositeExpression null buildUserConstraints(ExpressionBuilder $expressionBuilder, string $tableAlias)
Definition: PagePermissionRestriction.php:88
‪TYPO3\CMS\Core\Context\UserAspect
Definition: UserAspect.php:38
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder\comparison
‪string comparison($leftExpression, string $operator, $rightExpression)
Definition: ExpressionBuilder.php:96