‪TYPO3CMS  ‪main
StoragePermissionsAspect.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 
20 use Psr\Http\Message\ServerRequestInterface;
28 
39 {
43  #[AsEventListener('backend-user-permissions')]
45  {
46  $storage = $event->‪getStorage();
47  if ((‪$GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface
48  && ‪ApplicationType::fromRequest(‪$GLOBALS['TYPO3_REQUEST'])->isBackend()
49  && !$this->‪getBackendUser()->isAdmin()
50  && !$storage->isFallbackStorage()
51  ) {
52  $storage->setEvaluatePermissions(true);
53  $storage->setUserPermissions($this->‪getFilePermissionsForStorage($storage));
54  $this->‪addFileMountsToStorage($storage);
55  }
56  }
57 
61  private function ‪addFileMountsToStorage(‪ResourceStorage $storage): void
62  {
63  foreach ($this->‪getBackendUser()->getFileMountRecords() as $fileMountRow) {
64  if (!str_contains($fileMountRow['identifier'], ':')) {
65  // Skip record since the file mount identifier is invalid
66  continue;
67  }
68  [$base, $path] = ‪GeneralUtility::trimExplode(':', $fileMountRow['identifier'], false, 2);
69  if ((int)$base === $storage->‪getUid()) {
70  try {
71  $storage->‪addFileMount($path, $fileMountRow);
72  } catch (‪FolderDoesNotExistException $e) {
73  // That file mount does not seem to be valid, fail silently
74  }
75  }
76  }
77  }
78 
85  private function ‪getFilePermissionsForStorage(‪ResourceStorage $storageObject): array
86  {
87  $backendUser = $this->‪getBackendUser();
88  $finalUserPermissions = $backendUser->getFilePermissions();
89  if ($backendUser->isAdmin()) {
90  return $finalUserPermissions;
91  }
92  $storageFilePermissions = $backendUser->getTSConfig()['permissions.']['file.']['storage.'][$storageObject->‪getUid() . '.'] ?? [];
93  if (!empty($storageFilePermissions)) {
94  array_walk(
95  $storageFilePermissions,
96  static function (string $value, string $permission) use (&$finalUserPermissions): void {
97  $finalUserPermissions[$permission] = (bool)$value;
98  }
99  );
100  }
101  return $finalUserPermissions;
102  }
103 
105  {
106  return ‪$GLOBALS['BE_USER'];
107  }
108 }
‪TYPO3\CMS\Core\Resource\Security\StoragePermissionsAspect\addUserPermissionsToStorage
‪addUserPermissionsToStorage(AfterResourceStorageInitializationEvent $event)
Definition: StoragePermissionsAspect.php:44
‪TYPO3\CMS\Core\Resource\ResourceStorage\getUid
‪int getUid()
Definition: ResourceStorage.php:338
‪TYPO3\CMS\Core\Resource\Event\AfterResourceStorageInitializationEvent\getStorage
‪getStorage()
Definition: AfterResourceStorageInitializationEvent.php:31
‪TYPO3\CMS\Core\Attribute\AsEventListener
Definition: AsEventListener.php:25
‪TYPO3\CMS\Core\Resource\Security
Definition: FileMetadataPermissionsAspect.php:16
‪TYPO3\CMS\Core\Resource\ResourceStorage\addFileMount
‪addFileMount($folderIdentifier, $additionalData=[])
Definition: ResourceStorage.php:555
‪TYPO3\CMS\Core\Resource\Security\StoragePermissionsAspect\getBackendUser
‪getBackendUser()
Definition: StoragePermissionsAspect.php:104
‪TYPO3\CMS\Core\Resource\Security\StoragePermissionsAspect\getFilePermissionsForStorage
‪getFilePermissionsForStorage(ResourceStorage $storageObject)
Definition: StoragePermissionsAspect.php:85
‪TYPO3\CMS\Core\Resource\Event\AfterResourceStorageInitializationEvent
Definition: AfterResourceStorageInitializationEvent.php:28
‪TYPO3\CMS\Core\Resource\Exception\FolderDoesNotExistException
Definition: FolderDoesNotExistException.php:21
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:129
‪TYPO3\CMS\Core\Resource\Security\StoragePermissionsAspect
Definition: StoragePermissionsAspect.php:39
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Http\fromRequest
‪@ fromRequest
Definition: ApplicationType.php:66
‪TYPO3\CMS\Core\Resource\Security\StoragePermissionsAspect\addFileMountsToStorage
‪addFileMountsToStorage(ResourceStorage $storage)
Definition: StoragePermissionsAspect.php:61
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:822
‪TYPO3\CMS\Core\Http\ApplicationType
‪ApplicationType
Definition: ApplicationType.php:55