TYPO3 CMS  TYPO3_7-6
CategoryPermissionsAspect.php
Go to the documentation of this file.
1 <?php
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  // Check the rootline against categoryMountPoints when tree was filtered
69  if ($dataProvider->getRootUid() !== null) {
70  if (in_array($dataProvider->getRootUid(), $categoryMountPoints)) {
71  return;
72  }
73  $uidsInRootline = $this->findUidsInRootline($dataProvider->getRootUid());
74  if (!empty(array_intersect($categoryMountPoints, $uidsInRootline))) {
75  // One of the parents was found in categoryMountPoints so all children are secure
76  return;
77  }
78  }
79 
80  // First, remove all child nodes which must be analysed to be considered as "secure".
81  // The nodes were backed up in variable $treeNodeCollection beforehand.
82  $treeData->removeChildNodes();
83 
84  // Create an empty tree node collection to receive the secured nodes.
86  $securedTreeNodeCollection = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Tree\TreeNodeCollection::class);
87 
88  foreach ($categoryMountPoints as $categoryMountPoint) {
89  $treeNode = $this->lookUpCategoryMountPointInTreeNodes((int)$categoryMountPoint, $treeNodeCollection);
90  if (!is_null($treeNode)) {
91  $securedTreeNodeCollection->append($treeNode);
92  }
93  }
94 
95  // Reset child nodes.
96  $treeData->setChildNodes($securedTreeNodeCollection);
97  }
98  }
99  }
100 
108  protected function lookUpCategoryMountPointInTreeNodes($categoryMountPoint, TreeNodeCollection $treeNodeCollection)
109  {
110  $result = null;
111 
112  // If any User permission, recursively traverse the tree and set tree part as mount point
113  foreach ($treeNodeCollection as $treeNode) {
114 
116  if ((int)$treeNode->getId() === $categoryMountPoint) {
117  $result = $treeNode;
118  break;
119  }
120 
121  if ($treeNode->hasChildNodes()) {
122 
124  $node = $this->lookUpCategoryMountPointInTreeNodes($categoryMountPoint, $treeNode->getChildNodes());
125  if (! is_null($node)) {
126  $result = $node;
127  break;
128  }
129  }
130  }
131  return $result;
132  }
133 
140  protected function findUidsInRootline($uid) {
141  $row = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('parent', $this->categoryTableName, 'uid=' . (int)$uid);
142  $parentUids = [];
143  if ($row['parent'] > 0) {
144  $parentUids = $this->findUidsInRootline($row['parent']);
145  $parentUids[] = $row['parent'];
146  }
147  return $parentUids;
148  }
149 }
$uid
Definition: server.php:38
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']