TYPO3  7.6
CategoryPermissionsAspect.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Backend\Security;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
21 
30 {
34  protected $categoryTableName = 'sys_category';
35 
40 
44  public function __construct($backendUserAuthentication = null)
45  {
46  $this->backendUserAuthentication = $backendUserAuthentication ?: $GLOBALS['BE_USER'];
47  }
48 
56  public function addUserPermissionsToCategoryTreeData(DatabaseTreeDataProvider $dataProvider, $treeData)
57  {
58  if (!$this->backendUserAuthentication->isAdmin() && $dataProvider->getTableName() === $this->categoryTableName) {
59 
60  // Get User permissions related to category
61  $categoryMountPoints = $this->backendUserAuthentication->getCategoryMountPoints();
62 
63  // Backup child nodes to be processed.
64  $treeNodeCollection = $treeData->getChildNodes();
65 
66  if (!empty($categoryMountPoints) && !empty($treeNodeCollection)) {
67 
68  // First, remove all child nodes which must be analysed to be considered as "secure".
69  // The nodes were backed up in variable $treeNodeCollection beforehand.
70  $treeData->removeChildNodes();
71 
72  // Create an empty tree node collection to receive the secured nodes.
74  $securedTreeNodeCollection = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Tree\TreeNodeCollection::class);
75 
76  foreach ($categoryMountPoints as $categoryMountPoint) {
77  $treeNode = $this->lookUpCategoryMountPointInTreeNodes((int)$categoryMountPoint, $treeNodeCollection);
78  if (!is_null($treeNode)) {
79  $securedTreeNodeCollection->append($treeNode);
80  }
81  }
82 
83  // Reset child nodes.
84  $treeData->setChildNodes($securedTreeNodeCollection);
85  }
86  }
87  }
88 
96  protected function lookUpCategoryMountPointInTreeNodes($categoryMountPoint, TreeNodeCollection $treeNodeCollection)
97  {
98  $result = null;
99 
100  // If any User permission, recursively traverse the tree and set tree part as mount point
101  foreach ($treeNodeCollection as $treeNode) {
102 
104  if ((int)$treeNode->getId() === $categoryMountPoint) {
105  $result = $treeNode;
106  break;
107  }
108 
109  if ($treeNode->hasChildNodes()) {
110 
112  $node = $this->lookUpCategoryMountPointInTreeNodes($categoryMountPoint, $treeNode->getChildNodes());
113  if (! is_null($node)) {
114  $result = $node;
115  break;
116  }
117  }
118  }
119  return $result;
120  }
121 }