|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
namespace TYPO3\CMS\Core\Database\Query\Restriction; |
|
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
|
|
|
|
|
18
|
|
|
use TYPO3\CMS\Core\Context\UserAspect; |
|
19
|
|
|
use TYPO3\CMS\Core\Database\Query\Expression\CompositeExpression; |
|
20
|
|
|
use TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Restriction to make queries respect backend user rights for pages. |
|
24
|
|
|
* |
|
25
|
|
|
* Adds a WHERE-clause for the pages-table where user permissions according to input argument, $permissions, is validated. |
|
26
|
|
|
* $permissions is the "mask" used to select - see Permission Bitset. |
|
27
|
|
|
* E.g. if $perms is 1 then you'll get all pages that a user can actually see! |
|
28
|
|
|
* 2^0 = show (1) |
|
29
|
|
|
* 2^1 = edit (2) |
|
30
|
|
|
* 2^2 = delete (4) |
|
31
|
|
|
* 2^3 = new (8) |
|
32
|
|
|
* If the user is 'admin' no validation is used. |
|
33
|
|
|
* |
|
34
|
|
|
* If the user is not set at all (->user is not an array), then "AND 1=0" is returned (will cause no selection results at all) |
|
35
|
|
|
* |
|
36
|
|
|
* The 95% use of this function is "->getPagePermsClause(1)" which will |
|
37
|
|
|
* return WHERE clauses for *selecting* pages in backend listings - in other words this will check read permissions. |
|
38
|
|
|
*/ |
|
39
|
|
|
class PagePermissionRestriction implements QueryRestrictionInterface |
|
40
|
|
|
{ |
|
41
|
|
|
/** |
|
42
|
|
|
* @var int |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $permissions; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var UserAspect |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $userAspect; |
|
50
|
|
|
|
|
51
|
|
|
public function __construct(UserAspect $userAspect, int $permissions) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->permissions = $permissions; |
|
54
|
|
|
$this->userAspect = $userAspect; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Main method to build expressions for given tables |
|
59
|
|
|
* |
|
60
|
|
|
* @param array $queriedTables Array of tables, where array key is table alias and value is a table name |
|
61
|
|
|
* @param ExpressionBuilder $expressionBuilder Expression builder instance to add restrictions with |
|
62
|
|
|
* @return CompositeExpression The result of query builder expression(s) |
|
63
|
|
|
*/ |
|
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
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param ExpressionBuilder $expressionBuilder |
|
84
|
|
|
* @param string $tableAlias |
|
85
|
|
|
* @return string|CompositeExpression|null |
|
86
|
|
|
* @throws \TYPO3\CMS\Core\Context\Exception\AspectPropertyNotFoundException |
|
87
|
|
|
*/ |
|
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), |
|
100
|
|
|
ExpressionBuilder::EQ, |
|
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), |
|
107
|
|
|
ExpressionBuilder::EQ, |
|
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), |
|
124
|
|
|
ExpressionBuilder::EQ, |
|
125
|
|
|
$this->permissions |
|
126
|
|
|
) |
|
127
|
|
|
) |
|
128
|
|
|
); |
|
129
|
|
|
} |
|
130
|
|
|
return $constraint; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|