[BUGFIX] Respect user mount points within FormPersistenceManager 15/62415/6
authorRalf Zimmermann <ralf.zimmermann@tritum.de>
Sat, 23 Nov 2019 12:07:38 +0000 (13:07 +0100)
committerRalf Zimmermann <ralf.zimmermann@tritum.de>
Sun, 24 Nov 2019 10:48:50 +0000 (11:48 +0100)
Let the FormPersistenceManager respect user mount points and
auto created file mounts while retrieving and creating folders
which are configured within
"TYPO3.CMS.Form.persistenceManager.allowedFileMounts".

Resolves: #88895
Releases: master, 9.5
Change-Id: Ic422c692e268165de986e43239d9f293a71e0b25
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/62415
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Mathias Brodala <mbrodala@pagemachine.de>
Tested-by: Björn Jacob <bjoern.jacob@tritum.de>
Tested-by: Ralf Zimmermann <ralf.zimmermann@tritum.de>
Reviewed-by: Mathias Brodala <mbrodala@pagemachine.de>
Reviewed-by: Björn Jacob <bjoern.jacob@tritum.de>
Reviewed-by: Ralf Zimmermann <ralf.zimmermann@tritum.de>
typo3/sysext/form/Classes/Mvc/Persistence/FormPersistenceManager.php

index 6cf3e30..425b6dc 100644 (file)
@@ -405,6 +405,7 @@ class FormPersistenceManager implements FormPersistenceManagerInterface
     public function getAccessibleFormStorageFolders(): array
     {
         $storageFolders = [];
+
         if (
             !isset($this->formSettings['persistenceManager']['allowedFileMounts'])
             || !is_array($this->formSettings['persistenceManager']['allowedFileMounts'])
@@ -414,8 +415,9 @@ class FormPersistenceManager implements FormPersistenceManagerInterface
         }
 
         foreach ($this->formSettings['persistenceManager']['allowedFileMounts'] as $allowedFileMount) {
-            list($storageUid, $fileMountIdentifier) = explode(':', $allowedFileMount, 2);
-            $fileMountIdentifier = rtrim($fileMountIdentifier, '/') . '/';
+            [$storageUid, $fileMountPath] = explode(':', $allowedFileMount, 2);
+            // like "/form_definitions/" or "/group_homes/1/form_definitions/"
+            $fileMountPath = rtrim($fileMountPath, '/') . '/';
 
             try {
                 $storage = $this->getStorageByUid((int)$storageUid);
@@ -423,15 +425,41 @@ class FormPersistenceManager implements FormPersistenceManagerInterface
                 continue;
             }
 
+            $isStorageFileMount = false;
+            $parentFolder = $storage->getRootLevelFolder(false);
+
+            foreach ($storage->getFileMounts() as $storageFileMount) {
+                /** @var \TYPO3\CMS\Core\Resource\Folder */
+                $storageFileMountFolder = $storageFileMount['folder'];
+
+                // Normally should use ResourceStorage::isWithinFolder() to check if the configured file mount path is within a storage file mount but this requires a valid Folder object and thus a directory which already exists. And the folder could simply not exist yet.
+                if (StringUtility::beginsWith($fileMountPath, $storageFileMountFolder->getIdentifier())) {
+                    $isStorageFileMount = true;
+                    $parentFolder = $storageFileMountFolder;
+                }
+            }
+
+            // Get storage folder object, create it if missing
             try {
-                $folder = $storage->getFolder($fileMountIdentifier);
-            } catch (FolderDoesNotExistException $e) {
-                $storage->createFolder($fileMountIdentifier);
-                continue;
+                $fileMountFolder = $storage->getFolder($fileMountPath);
             } catch (InsufficientFolderAccessPermissionsException $e) {
                 continue;
+            } catch (FolderDoesNotExistException $e) {
+                if ($isStorageFileMount) {
+                    $fileMountPath = substr(
+                        $fileMountPath,
+                        strlen($parentFolder->getIdentifier())
+                    );
+                }
+
+                try {
+                    $fileMountFolder = $storage->createFolder($fileMountPath, $parentFolder);
+                } catch (InsufficientFolderAccessPermissionsException $e) {
+                    continue;
+                }
             }
-            $storageFolders[$allowedFileMount] = $folder;
+
+            $storageFolders[$allowedFileMount] = $fileMountFolder;
         }
         return $storageFolders;
     }