‪TYPO3CMS  10.4
PagePermissionAssembler.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
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 
19 
24 
34 {
35 
41  protected ‪$defaultPermissions = [
42  'user' => 'show,edit,delete,new,editcontent',
43  'group' => 'show,edit,new,editcontent',
44  'everybody' => ''
45  ];
46 
47  public function ‪__construct(array ‪$defaultPermissions = null)
48  {
49  // Initializing default permissions for pages
50  if (isset(‪$defaultPermissions['user'])) {
51  $this->defaultPermissions['user'] = ‪$defaultPermissions['user'];
52  }
53  if (isset(‪$defaultPermissions['group'])) {
54  $this->defaultPermissions['group'] = ‪$defaultPermissions['group'];
55  }
56  if (isset(‪$defaultPermissions['everybody'])) {
57  $this->defaultPermissions['everybody'] = ‪$defaultPermissions['everybody'];
58  }
59  }
60 
70  public function ‪applyDefaults(array $fieldArray, int $pid, int $backendUserId, int $backendUserGroupId): array
71  {
72  $fieldArray['perms_userid'] = $backendUserId;
73  $fieldArray['perms_groupid'] = $backendUserGroupId;
74  $fieldArray['perms_user'] = $this->‪assemblePermissions($this->defaultPermissions['user']);
75  $fieldArray['perms_group'] = $this->‪assemblePermissions($this->defaultPermissions['group']);
76  $fieldArray['perms_everybody'] = $this->‪assemblePermissions($this->defaultPermissions['everybody']);
77  $TSConfig = ‪BackendUtility::getPagesTSconfig($pid)['TCEMAIN.'] ?? [];
78  if (isset($TSConfig['permissions.']) && is_array($TSConfig['permissions.'])) {
79  return $this->‪setTSconfigPermissions($fieldArray, $TSConfig['permissions.']);
80  }
81  return $fieldArray;
82  }
83 
92  protected function ‪setTSconfigPermissions(array $fieldArray, array $tsconfig): array
93  {
94  if ((string)$tsconfig['userid'] !== '') {
95  $fieldArray['perms_userid'] = (int)$tsconfig['userid'];
96  }
97  if ((string)$tsconfig['groupid'] !== '') {
98  $fieldArray['perms_groupid'] = (int)$tsconfig['groupid'];
99  }
100  if ((string)$tsconfig['user'] !== '') {
101  $fieldArray['perms_user'] = $this->‪assemblePermissions($tsconfig['user']);
102  }
103  if ((string)$tsconfig['group'] !== '') {
104  $fieldArray['perms_group'] = $this->‪assemblePermissions($tsconfig['group']);
105  }
106  if ((string)$tsconfig['everybody'] !== '') {
107  $fieldArray['perms_everybody'] = $this->‪assemblePermissions($tsconfig['everybody']);
108  }
109  return $fieldArray;
110  }
111 
118  protected function ‪assemblePermissions($listOfPermissions): int
119  {
120  // Already set as integer, so this one is used.
121  if (‪MathUtility::canBeInterpretedAsInteger($listOfPermissions)) {
122  return (int)$listOfPermissions;
123  }
124  $keyArr = ‪GeneralUtility::trimExplode(',', $listOfPermissions, true);
125  $value = 0;
126  $permissionMap = ‪Permission::getMap();
127  foreach ($keyArr as $key) {
128  if ($key && isset($permissionMap[$key])) {
129  $value |= $permissionMap[$key];
130  }
131  }
132  return $value;
133  }
134 }
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger($var)
Definition: MathUtility.php:74
‪TYPO3\CMS\Core\DataHandling
Definition: DataHandler.php:16
‪TYPO3\CMS\Core\DataHandling\PagePermissionAssembler\$defaultPermissions
‪array $defaultPermissions
Definition: PagePermissionAssembler.php:40
‪TYPO3\CMS\Core\Type\Bitmask\Permission
Definition: Permission.php:24
‪TYPO3\CMS\Core\DataHandling\PagePermissionAssembler\setTSconfigPermissions
‪array setTSconfigPermissions(array $fieldArray, array $tsconfig)
Definition: PagePermissionAssembler.php:91
‪TYPO3\CMS\Backend\Utility\BackendUtility\getPagesTSconfig
‪static array getPagesTSconfig($id)
Definition: BackendUtility.php:698
‪TYPO3\CMS\Core\Type\Bitmask\Permission\getMap
‪static array getMap()
Definition: Permission.php:67
‪TYPO3\CMS\Backend\Utility\BackendUtility
Definition: BackendUtility.php:75
‪TYPO3\CMS\Core\DataHandling\PagePermissionAssembler\assemblePermissions
‪int assemblePermissions($listOfPermissions)
Definition: PagePermissionAssembler.php:117
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static string[] trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:1059
‪TYPO3\CMS\Core\DataHandling\PagePermissionAssembler
Definition: PagePermissionAssembler.php:34
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:22
‪TYPO3\CMS\Core\DataHandling\PagePermissionAssembler\applyDefaults
‪array applyDefaults(array $fieldArray, int $pid, int $backendUserId, int $backendUserGroupId)
Definition: PagePermissionAssembler.php:69
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\DataHandling\PagePermissionAssembler\__construct
‪__construct(array $defaultPermissions=null)
Definition: PagePermissionAssembler.php:46