‪TYPO3CMS  9.5
PagePermissionRestriction.php
Go to the documentation of this file.
1 <?php
2 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 
21 
40 {
44  protected ‪$permissions;
45 
49  protected ‪$userAspect;
50 
52  {
53  $this->permissions = ‪$permissions;
54  $this->userAspect = ‪$userAspect;
55  }
56 
64  public function ‪buildExpression(array $queriedTables, ‪ExpressionBuilder $expressionBuilder): ‪CompositeExpression
65  {
66  $constraints = [];
67 
68  foreach ($queriedTables as $tableAlias => $tableName) {
69  if ($tableName !== 'pages') {
70  continue;
71  }
72 
73  $constraint = $this->‪buildUserConstraints($expressionBuilder, $tableAlias);
74  if ($constraint) {
75  $constraints[] = $expressionBuilder->‪andX($constraint);
76  }
77  }
78 
79  return $expressionBuilder->‪andX(...$constraints);
80  }
81 
88  protected function ‪buildUserConstraints(ExpressionBuilder $expressionBuilder, string $tableAlias)
89  {
90  if (!$this->userAspect->isLoggedIn()) {
91  return $expressionBuilder->comparison(1, ‪ExpressionBuilder::EQ, 0);
92  }
93  if ($this->userAspect->isAdmin()) {
94  return null;
95  }
96  // User permissions
97  $constraint = $expressionBuilder->orX(
98  $expressionBuilder->comparison(
99  $expressionBuilder->bitAnd($tableAlias . '.perms_everybody', $this->permissions),
101  $this->permissions
102  ),
103  $expressionBuilder->andX(
104  $expressionBuilder->eq($tableAlias . '.perms_userid', $this->userAspect->get('id')),
105  $expressionBuilder->comparison(
106  $expressionBuilder->bitAnd($tableAlias . '.perms_user', $this->permissions),
108  $this->permissions
109  )
110  )
111  );
112 
113  // User groups (if any are set)
114  $groupIds = array_map('intval', $this->userAspect->getGroupIds());
115  if (!empty($groupIds)) {
116  $constraint->add(
117  $expressionBuilder->andX(
118  $expressionBuilder->in(
119  $tableAlias . '.perms_groupid',
120  $groupIds
121  ),
122  $expressionBuilder->comparison(
123  $expressionBuilder->bitAnd($tableAlias . '.perms_group', $this->permissions),
125  $this->permissions
126  )
127  )
128  );
129  }
130  return $constraint;
131  }
132 }
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder
Definition: ExpressionBuilder.php:33
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder\EQ
‪const EQ
Definition: ExpressionBuilder.php:34
‪TYPO3\CMS\Core\Database\Query\Restriction\PagePermissionRestriction\buildExpression
‪CompositeExpression buildExpression(array $queriedTables, ExpressionBuilder $expressionBuilder)
Definition: PagePermissionRestriction.php:62
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder\bitAnd
‪string bitAnd(string $fieldName, int $value)
Definition: ExpressionBuilder.php:389
‪TYPO3\CMS\Core\Database\Query\Restriction\QueryRestrictionInterface
Definition: QueryRestrictionInterface.php:25
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder\eq
‪string eq(string $fieldName, $value)
Definition: ExpressionBuilder.php:107
‪TYPO3\CMS\Core\Database\Query\Restriction\PagePermissionRestriction\$userAspect
‪UserAspect $userAspect
Definition: PagePermissionRestriction.php:47
‪TYPO3\CMS\Core\Database\Query\Restriction\PagePermissionRestriction\$permissions
‪int $permissions
Definition: PagePermissionRestriction.php:43
‪TYPO3\CMS\Core\Database\Query\Expression\CompositeExpression
Definition: CompositeExpression.php:23
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder\in
‪string in(string $fieldName, $value)
Definition: ExpressionBuilder.php:242
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder\andX
‪CompositeExpression andX(... $expressions)
Definition: ExpressionBuilder.php:68
‪TYPO3\CMS\Core\Database\Query\Restriction
Definition: AbstractRestrictionContainer.php:3
‪TYPO3\CMS\Core\Database\Query\Restriction\PagePermissionRestriction\__construct
‪__construct(UserAspect $userAspect, int $permissions)
Definition: PagePermissionRestriction.php:49
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder\orX
‪CompositeExpression orX(... $expressions)
Definition: ExpressionBuilder.php:80
‪TYPO3\CMS\Core\Database\Query\Restriction\PagePermissionRestriction
Definition: PagePermissionRestriction.php:40
‪TYPO3\CMS\Core\Database\Query\Restriction\PagePermissionRestriction\buildUserConstraints
‪string CompositeExpression null buildUserConstraints(ExpressionBuilder $expressionBuilder, string $tableAlias)
Definition: PagePermissionRestriction.php:86
‪TYPO3\CMS\Core\Context\UserAspect
Definition: UserAspect.php:36
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder\comparison
‪string comparison($leftExpression, string $operator, $rightExpression)
Definition: ExpressionBuilder.php:94