[!!!][TASK] Drop "documentation" extension
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Database / Query / Restriction / FrontendGroupRestriction.php
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\Database\Query\Expression\CompositeExpression;
19 use TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder;
20
21 /**
22 * Restriction to filter records, which are limited to the given user groups
23 */
24 class FrontendGroupRestriction implements QueryRestrictionInterface
25 {
26 /**
27 * @var array
28 */
29 protected $frontendGroupIds;
30
31 /**
32 * @param array $frontendGroupIds Normalized array with user groups of currently logged in user (typically expolded value of $GLOBALS['TSFE']->gr_list
33 */
34 public function __construct(array $frontendGroupIds = null)
35 {
36 $this->frontendGroupIds = $frontendGroupIds ?? explode(',', $GLOBALS['TSFE']->gr_list);
37 }
38
39 /**
40 * Main method to build expressions for given tables
41 * Evaluates the ctrl/enablecolumns/fe_group flag of the table and adds the according restriction if set
42 *
43 * @param array $queriedTables Array of tables, where array key is table alias and value is a table name
44 * @param ExpressionBuilder $expressionBuilder Expression builder instance to add restrictions with
45 * @return CompositeExpression The result of query builder expression(s)
46 */
47 public function buildExpression(array $queriedTables, ExpressionBuilder $expressionBuilder): CompositeExpression
48 {
49 $constraints = [];
50 foreach ($queriedTables as $tableAlias => $tableName) {
51 $groupFieldName = $GLOBALS['TCA'][$tableName]['ctrl']['enablecolumns']['fe_group'] ?? null;
52 if (!empty($groupFieldName)) {
53 $fieldName = $tableAlias . '.' . $groupFieldName;
54 // Allow records where no group access has been configured (field values NULL, 0 or empty string)
55 $constraints = [
56 $expressionBuilder->isNull($fieldName),
57 $expressionBuilder->eq($fieldName, $expressionBuilder->literal('')),
58 $expressionBuilder->eq($fieldName, $expressionBuilder->literal('0')),
59 ];
60 foreach ($this->frontendGroupIds as $frontendGroupId) {
61 $constraints[] = $expressionBuilder->inSet(
62 $fieldName,
63 $expressionBuilder->literal((string)$frontendGroupId)
64 );
65 }
66 }
67 }
68 return $expressionBuilder->orX(...$constraints);
69 }
70 }