‪TYPO3CMS  11.5
CategoryPermissionsAspect.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Psr\Http\Message\ServerRequestInterface;
26 
36 {
40  private ‪$categoryTableName = 'sys_category';
41 
48  {
49  // Only evaluate this in the backend
50  if (!(‪$GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface
51  || !‪ApplicationType::fromRequest(‪$GLOBALS['TYPO3_REQUEST'])->isBackend()
52  ) {
53  return;
54  }
55 
56  $dataProvider = $event->‪getProvider();
57  $treeData = $event->‪getTreeData();
58 
59  if (!‪$GLOBALS['BE_USER']->isAdmin() && $dataProvider->getTableName() === $this->categoryTableName) {
60  // Get User permissions related to category
61  $categoryMountPoints = ‪$GLOBALS['BE_USER']->getCategoryMountPoints();
62 
63  // Backup child nodes to be processed.
64  $treeNodeCollection = $treeData->getChildNodes();
65 
66  if (!empty($categoryMountPoints) && !empty($treeNodeCollection)) {
67  // @deprecated Remove merging of $dataProvider->getRootUid() in v12
68  $startingPoints = array_unique(array_merge($dataProvider->getStartingPoints(), [$dataProvider->getRootUid()]));
69  $shallRepopulateTree = false;
70 
71  // Check the rootline against categoryMountPoints when tree was filtered
72  foreach ($startingPoints as $startingPoint) {
73  if (!in_array($startingPoint, $categoryMountPoints)) {
74  $shallRepopulateTree = true;
75  break;
76  }
77  $uidsInRootline = $this->‪findUidsInRootline($startingPoint);
78  if (empty(array_intersect($categoryMountPoints, $uidsInRootline))) {
79  $shallRepopulateTree = true;
80  break;
81  }
82  }
83 
84  if ($shallRepopulateTree) {
85  // First, remove all child nodes which must be analyzed to be considered as "secure".
86  // The nodes were backed up in variable $treeNodeCollection beforehand.
87  $treeData->removeChildNodes();
88 
89  // Create an empty tree node collection to receive the secured nodes.
90  $securedTreeNodeCollection = GeneralUtility::makeInstance(TreeNodeCollection::class);
91 
92  foreach ($categoryMountPoints as $categoryMountPoint) {
93  $treeNode = $this->‪lookUpCategoryMountPointInTreeNodes((int)$categoryMountPoint, $treeNodeCollection);
94  if ($treeNode !== null) {
95  $securedTreeNodeCollection->append($treeNode);
96  }
97  }
98 
99  // Reset child nodes.
100  $treeData->setChildNodes($securedTreeNodeCollection);
101  }
102  }
103  }
104  }
105 
113  private function ‪lookUpCategoryMountPointInTreeNodes(int $categoryMountPoint, ‪TreeNodeCollection $treeNodeCollection)
114  {
115  $result = null;
116 
117  // If any User permission, recursively traverse the tree and set tree part as mount point
118  foreach ($treeNodeCollection as $treeNode) {
120  if ((int)$treeNode->getId() === $categoryMountPoint) {
121  $result = $treeNode;
122  break;
123  }
124 
125  if ($treeNode->hasChildNodes()) {
127  $node = $this->‪lookUpCategoryMountPointInTreeNodes($categoryMountPoint, $treeNode->getChildNodes());
128  if ($node !== null) {
129  $result = $node;
130  break;
131  }
132  }
133  }
134  return $result;
135  }
136 
143  private function ‪findUidsInRootline(int $uid)
144  {
145  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
146  ->getQueryBuilderForTable($this->categoryTableName);
147  $row = $queryBuilder
148  ->select('parent')
149  ->from($this->categoryTableName)
150  ->where(
151  $queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($uid, ‪Connection::PARAM_INT))
152  )
153  ->executeQuery()
154  ->fetchAssociative();
155 
156  $parentUids = [];
157  if ($row['parent'] > 0) {
158  $parentUids = $this->‪findUidsInRootline($row['parent']);
159  $parentUids[] = $row['parent'];
160  }
161  return $parentUids;
162  }
163 }
‪TYPO3\CMS\Core\Http\ApplicationType\fromRequest
‪static static fromRequest(ServerRequestInterface $request)
Definition: ApplicationType.php:62
‪TYPO3\CMS\Backend\Security\CategoryPermissionsAspect\$categoryTableName
‪string $categoryTableName
Definition: CategoryPermissionsAspect.php:39
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:49
‪TYPO3\CMS\Backend\Security\CategoryPermissionsAspect\findUidsInRootline
‪array findUidsInRootline(int $uid)
Definition: CategoryPermissionsAspect.php:142
‪TYPO3\CMS\Core\Tree\Event\ModifyTreeDataEvent\getTreeData
‪getTreeData()
Definition: ModifyTreeDataEvent.php:42
‪TYPO3\CMS\Backend\Security\CategoryPermissionsAspect\lookUpCategoryMountPointInTreeNodes
‪TreeNode null lookUpCategoryMountPointInTreeNodes(int $categoryMountPoint, TreeNodeCollection $treeNodeCollection)
Definition: CategoryPermissionsAspect.php:112
‪TYPO3\CMS\Core\Http\ApplicationType
Definition: ApplicationType.php:52
‪TYPO3\CMS\Core\Tree\Event\ModifyTreeDataEvent
Definition: ModifyTreeDataEvent.php:27
‪TYPO3\CMS\Backend\Security\CategoryPermissionsAspect\addUserPermissionsToCategoryTreeData
‪addUserPermissionsToCategoryTreeData(ModifyTreeDataEvent $event)
Definition: CategoryPermissionsAspect.php:46
‪TYPO3\CMS\Backend\Tree\TreeNodeCollection
Definition: TreeNodeCollection.php:25
‪TYPO3\CMS\Backend\Tree\TreeNode
Definition: TreeNode.php:25
‪TYPO3\CMS\Backend\Security
Definition: CategoryPermissionsAspect.php:16
‪TYPO3\CMS\Backend\Security\CategoryPermissionsAspect
Definition: CategoryPermissionsAspect.php:36
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:38
‪TYPO3\CMS\Core\Tree\Event\ModifyTreeDataEvent\getProvider
‪getProvider()
Definition: ModifyTreeDataEvent.php:52
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50