[TASK] EXT:form - Tests for PropertyMappingConfiguration
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Mvc / Property / PropertyMappingConfiguration.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Form\Mvc\Property;
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
18 use TYPO3\CMS\Core\Resource\ResourceFactory;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Core\Utility\PathUtility;
21 use TYPO3\CMS\Extbase\Object\ObjectManager;
22 use TYPO3\CMS\Form\Domain\Model\FormElements\FileUpload;
23 use TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface;
24 use TYPO3\CMS\Form\Mvc\Property\TypeConverter\UploadedFileReferenceConverter;
25 use TYPO3\CMS\Form\Mvc\Validation\MimeTypeValidator;
26
27 /**
28 * Scope: frontend
29 */
30 class PropertyMappingConfiguration
31 {
32
33 /**
34 * Set the property mapping configuration for the file upload element.
35 * * Add the UploadedFileReferenceConverter to convert an uploaded file to an
36 * FileReference.
37 * * Add the MimeTypeValidator to the UploadedFileReferenceConverter to
38 * delete non valid filetypes directly.
39 * * Setup the storage:
40 * If the property "saveToFileMount" exist for this element it will be used.
41 * If this file mount or the property "saveToFileMount" does not exist
42 * the folder in which the form definition lies (persistence identifier) will be used.
43 * If the form is generated programmatically and therefore no
44 * persistence identifier exist the default storage "1:/user_upload/" will be used.
45 *
46 * @param RenderableInterface $renderable
47 * @internal
48 * @todo: could we find a not so ugly solution for that?
49 */
50 public function afterBuildingFinished(RenderableInterface $renderable)
51 {
52 if ($renderable instanceof FileUpload) {
53 /** @var \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration $propertyMappingConfiguration */
54 $propertyMappingConfiguration = $renderable->getRootForm()->getProcessingRule($renderable->getIdentifier())->getPropertyMappingConfiguration();
55
56 $mimeTypeValidator = GeneralUtility::makeInstance(ObjectManager::class)
57 ->get(MimeTypeValidator::class, ['allowedMimeTypes' => $renderable->getProperties()['allowedMimeTypes']]);
58
59 $uploadConfiguration = [
60 UploadedFileReferenceConverter::CONFIGURATION_FILE_VALIDATORS => [$mimeTypeValidator],
61 UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_CONFLICT_MODE => 'rename',
62 ];
63
64 $saveToFileMountIdentifier = (isset($renderable->getProperties()['saveToFileMount'])) ? $renderable->getProperties()['saveToFileMount'] : '';
65 if ($this->checkSaveFileMountAccess($saveToFileMountIdentifier)) {
66 $uploadConfiguration[UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER] = $saveToFileMountIdentifier;
67 } else {
68 $persistenceIdentifier = $renderable->getRootForm()->getPersistenceIdentifier();
69 if (!empty($persistenceIdentifier)) {
70 $pathinfo = PathUtility::pathinfo($persistenceIdentifier);
71 $saveToFileMountIdentifier = $pathinfo['dirname'];
72 if ($this->checkSaveFileMountAccess($saveToFileMountIdentifier)) {
73 $uploadConfiguration[UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER] = $saveToFileMountIdentifier;
74 }
75 }
76 }
77
78 $propertyMappingConfiguration->setTypeConverterOptions(UploadedFileReferenceConverter::class, $uploadConfiguration);
79 }
80 }
81
82 /**
83 * @param string $saveToFileMountIdentifier
84 * @return bool
85 * @internal
86 */
87 protected function checkSaveFileMountAccess(string $saveToFileMountIdentifier): bool
88 {
89 if (empty($saveToFileMountIdentifier)) {
90 return false;
91 }
92
93 $resourceFactory = GeneralUtility::makeInstance(ObjectManager::class)
94 ->get(ResourceFactory::class);
95
96 try {
97 $resourceFactory->getFolderObjectFromCombinedIdentifier($saveToFileMountIdentifier);
98 return true;
99 } catch (\InvalidArgumentException $e) {
100 return false;
101 }
102 }
103 }