Skip to content

Commit a5f9b37

Browse files
authored
Merge pull request #5554 from magento-tsg/2.3.6-develop-pr120
[TSG] Fixes for 2.3 (pr120) (2.3.6-develop)
2 parents 6a5ada5 + 83745d1 commit a5f9b37

File tree

21 files changed

+519
-132
lines changed

21 files changed

+519
-132
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Customer\Model\Customer\Attribute;
9+
10+
use Magento\Customer\Model\Customer\Attribute\ValidatorInterface;
11+
use Magento\Framework\Api\AttributeInterface;
12+
13+
/**
14+
* Customer custom attribute validator.
15+
*/
16+
class CompositeValidator implements ValidatorInterface
17+
{
18+
/**
19+
* @var ValidatorInterface[]
20+
*/
21+
private $validators;
22+
23+
/**
24+
* @param array $validators
25+
*/
26+
public function __construct(
27+
array $validators = []
28+
) {
29+
$this->validators = $validators;
30+
}
31+
32+
/**
33+
* @inheritdoc
34+
*/
35+
public function validate(AttributeInterface $customAttribute): void
36+
{
37+
foreach ($this->validators as $validator) {
38+
$validator->validate($customAttribute);
39+
}
40+
}
41+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Customer\Model\Customer\Attribute\Validator;
9+
10+
use Magento\Customer\Model\Customer\Attribute\ValidatorInterface;
11+
use Magento\Framework\Api\AttributeInterface;
12+
use Magento\Eav\Model\Config as EavConfig;
13+
use Magento\Customer\Model\Customer;
14+
use Magento\Framework\Exception\LocalizedException;
15+
16+
/**
17+
* Validator for customer custom file attribute.
18+
*/
19+
class File implements ValidatorInterface
20+
{
21+
/**
22+
* @var EavConfig
23+
*/
24+
private $eavConfig;
25+
26+
/**
27+
* @param EavConfig $eavConfig
28+
*/
29+
public function __construct(EavConfig $eavConfig)
30+
{
31+
$this->eavConfig = $eavConfig;
32+
}
33+
34+
/**
35+
* @inheritdoc
36+
*/
37+
public function validate(AttributeInterface $customAttribute): void
38+
{
39+
$attribute = $this->eavConfig->getAttribute(Customer::ENTITY, $customAttribute->getAttributeCode());
40+
if ($attribute->getFrontendInput() === 'file') {
41+
if (!preg_match(
42+
'#^/[a-zA-Z0-9_-]/[a-zA-Z0-9_-]/[a-zA-Z0-9_-]+.[a-z]{3,6}$#',
43+
$customAttribute->getValue()
44+
)) {
45+
throw new LocalizedException(__("The filename has unpermitted symbols."));
46+
};
47+
}
48+
}
49+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Customer\Model\Customer\Attribute;
9+
10+
use Magento\Framework\Api\AttributeInterface;
11+
12+
/**
13+
* Interface for customer custom attribute validator.
14+
*/
15+
interface ValidatorInterface
16+
{
17+
/**
18+
* Validate customer attributes.
19+
*
20+
* Throws localized exception if is not valid.
21+
*
22+
* @param AttributeInterface $customAttribute
23+
* @return void
24+
* @throws \Throwable
25+
*/
26+
public function validate(AttributeInterface $customAttribute): void;
27+
}

app/code/Magento/Customer/Model/FileProcessor.php

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@
77

88
namespace Magento\Customer\Model;
99

10+
use Magento\Framework\App\Filesystem\DirectoryList;
11+
use Magento\Framework\App\ObjectManager;
12+
use Magento\Framework\Filesystem\Directory\WriteFactory;
13+
1014
/**
11-
* Processor class for work with uploaded files
15+
* Processor class for work with uploaded files.
16+
*
17+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1218
*/
1319
class FileProcessor
1420
{
@@ -20,7 +26,7 @@ class FileProcessor
2026
/**
2127
* @var \Magento\Framework\Filesystem\Directory\WriteInterface
2228
*/
23-
private $mediaDirectory;
29+
private $mediaEntityDirectory;
2430

2531
/**
2632
* @var \Magento\MediaStorage\Model\File\UploaderFactory
@@ -60,6 +66,9 @@ class FileProcessor
6066
* @param string $entityTypeCode
6167
* @param \Magento\Framework\File\Mime $mime
6268
* @param array $allowedExtensions
69+
* @param WriteFactory|null $writeFactory
70+
* @param DirectoryList|null $directoryList
71+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
6372
*/
6473
public function __construct(
6574
\Magento\Framework\Filesystem $filesystem,
@@ -68,15 +77,22 @@ public function __construct(
6877
\Magento\Framework\Url\EncoderInterface $urlEncoder,
6978
$entityTypeCode,
7079
\Magento\Framework\File\Mime $mime,
71-
array $allowedExtensions = []
80+
array $allowedExtensions = [],
81+
WriteFactory $writeFactory = null,
82+
DirectoryList $directoryList = null
7283
) {
73-
$this->mediaDirectory = $filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
7484
$this->uploaderFactory = $uploaderFactory;
7585
$this->urlBuilder = $urlBuilder;
7686
$this->urlEncoder = $urlEncoder;
7787
$this->entityTypeCode = $entityTypeCode;
7888
$this->mime = $mime;
7989
$this->allowedExtensions = $allowedExtensions;
90+
$writeFactory = $writeFactory ?? ObjectManager::getInstance()->get(WriteFactory::class);
91+
$directoryList = $directoryList ?? ObjectManager::getInstance()->get(DirectoryList::class);
92+
$this->mediaEntityDirectory = $writeFactory->create(
93+
$directoryList->getPath(DirectoryList::MEDIA)
94+
. DIRECTORY_SEPARATOR . $this->entityTypeCode
95+
);
8096
}
8197

8298
/**
@@ -87,9 +103,7 @@ public function __construct(
87103
*/
88104
public function getBase64EncodedData($fileName)
89105
{
90-
$filePath = $this->entityTypeCode . '/' . ltrim($fileName, '/');
91-
92-
$fileContent = $this->mediaDirectory->readFile($filePath);
106+
$fileContent = $this->mediaEntityDirectory->readFile($fileName);
93107

94108
$encodedContent = base64_encode($fileContent);
95109
return $encodedContent;
@@ -103,9 +117,7 @@ public function getBase64EncodedData($fileName)
103117
*/
104118
public function getStat($fileName)
105119
{
106-
$filePath = $this->entityTypeCode . '/' . ltrim($fileName, '/');
107-
108-
$result = $this->mediaDirectory->stat($filePath);
120+
$result = $this->mediaEntityDirectory->stat($fileName);
109121
return $result;
110122
}
111123

@@ -117,8 +129,7 @@ public function getStat($fileName)
117129
*/
118130
public function getMimeType($fileName)
119131
{
120-
$filePath = $this->entityTypeCode . '/' . ltrim($fileName, '/');
121-
$absoluteFilePath = $this->mediaDirectory->getAbsolutePath($filePath);
132+
$absoluteFilePath = $this->mediaEntityDirectory->getAbsolutePath($fileName);
122133

123134
$result = $this->mime->getMimeType($absoluteFilePath);
124135
return $result;
@@ -132,9 +143,7 @@ public function getMimeType($fileName)
132143
*/
133144
public function isExist($fileName)
134145
{
135-
$filePath = $this->entityTypeCode . '/' . ltrim($fileName, '/');
136-
137-
$result = $this->mediaDirectory->isExist($filePath);
146+
$result = $this->mediaEntityDirectory->isExist($fileName);
138147
return $result;
139148
}
140149

@@ -150,9 +159,9 @@ public function getViewUrl($filePath, $type)
150159
$viewUrl = '';
151160

152161
if ($this->entityTypeCode == \Magento\Customer\Api\AddressMetadataInterface::ENTITY_TYPE_ADDRESS) {
153-
$filePath = $this->entityTypeCode . '/' . ltrim($filePath, '/');
162+
$filePath = $this->entityTypeCode . DIRECTORY_SEPARATOR . ltrim($filePath, '/');
154163
$viewUrl = $this->urlBuilder->getBaseUrl(['_type' => \Magento\Framework\UrlInterface::URL_TYPE_MEDIA])
155-
. $this->mediaDirectory->getRelativePath($filePath);
164+
. $this->mediaEntityDirectory->getRelativePath($filePath);
156165
}
157166

158167
if ($this->entityTypeCode == \Magento\Customer\Api\CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER) {
@@ -181,8 +190,8 @@ public function saveTemporaryFile($fileId)
181190
$uploader->setAllowRenameFiles(true);
182191
$uploader->setAllowedExtensions($this->allowedExtensions);
183192

184-
$path = $this->mediaDirectory->getAbsolutePath(
185-
$this->entityTypeCode . '/' . self::TMP_DIR
193+
$path = $this->mediaEntityDirectory->getAbsolutePath(
194+
DIRECTORY_SEPARATOR . self::TMP_DIR
186195
);
187196

188197
$result = $uploader->save($path);
@@ -207,38 +216,36 @@ public function moveTemporaryFile($fileName)
207216
{
208217
$fileName = ltrim($fileName, '/');
209218

210-
$dispersionPath = \Magento\MediaStorage\Model\File\Uploader::getDispersionPath($fileName);
211-
$destinationPath = $this->entityTypeCode . $dispersionPath;
219+
$destinationPath = \Magento\MediaStorage\Model\File\Uploader::getDispersionPath($fileName);
212220

213-
if (!$this->mediaDirectory->create($destinationPath)) {
221+
if (!$this->mediaEntityDirectory->create($destinationPath)) {
214222
throw new \Magento\Framework\Exception\LocalizedException(
215223
__('Unable to create directory %1.', $destinationPath)
216224
);
217225
}
218226

219-
if (!$this->mediaDirectory->isWritable($destinationPath)) {
227+
if (!$this->mediaEntityDirectory->isWritable($destinationPath)) {
220228
throw new \Magento\Framework\Exception\LocalizedException(
221229
__('Destination folder is not writable or does not exists.')
222230
);
223231
}
224232

225233
$destinationFileName = \Magento\MediaStorage\Model\File\Uploader::getNewFileName(
226-
$this->mediaDirectory->getAbsolutePath($destinationPath) . '/' . $fileName
234+
$this->mediaEntityDirectory->getAbsolutePath($destinationPath) . '/' . $fileName
227235
);
228236

229237
try {
230-
$this->mediaDirectory->renameFile(
231-
$this->entityTypeCode . '/' . self::TMP_DIR . '/' . $fileName,
232-
$destinationPath . '/' . $destinationFileName
238+
$this->mediaEntityDirectory->renameFile(
239+
DIRECTORY_SEPARATOR . self::TMP_DIR . DIRECTORY_SEPARATOR . $fileName,
240+
$destinationPath . DIRECTORY_SEPARATOR . $destinationFileName
233241
);
234242
} catch (\Exception $e) {
235243
throw new \Magento\Framework\Exception\LocalizedException(
236244
__('Something went wrong while saving the file.')
237245
);
238246
}
239247

240-
$fileName = $dispersionPath . '/' . $destinationFileName;
241-
return $fileName;
248+
return $destinationPath . DIRECTORY_SEPARATOR . $destinationFileName;
242249
}
243250

244251
/**
@@ -249,9 +256,9 @@ public function moveTemporaryFile($fileName)
249256
*/
250257
public function removeUploadedFile($fileName)
251258
{
252-
$filePath = $this->entityTypeCode . '/' . ltrim($fileName, '/');
259+
$filePath = DIRECTORY_SEPARATOR . ltrim($fileName, '/');
253260

254-
$result = $this->mediaDirectory->delete($filePath);
261+
$result = $this->mediaEntityDirectory->delete($filePath);
255262
return $result;
256263
}
257264
}

0 commit comments

Comments
 (0)