Skip to content

Commit 1e26bd9

Browse files
committed
MC-23545: Customer attribute of type file doesn't display in Account Information after created
1 parent f55f411 commit 1e26bd9

File tree

13 files changed

+444
-167
lines changed

13 files changed

+444
-167
lines changed

app/code/Magento/Customer/Controller/Account/EditPost.php

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
use Magento\Framework\Exception\State\UserLockedException;
3232
use Magento\Customer\Controller\AbstractAccount;
3333
use Magento\Framework\Phrase;
34+
use Magento\Framework\Filesystem;
35+
use Magento\Framework\App\Filesystem\DirectoryList;
3436

3537
/**
3638
* Customer edit account information controller
@@ -94,6 +96,11 @@ class EditPost extends AbstractAccount implements CsrfAwareActionInterface, Http
9496
*/
9597
private $addressRegistry;
9698

99+
/**
100+
* @var Filesystem
101+
*/
102+
private $filesystem;
103+
97104
/**
98105
* @param Context $context
99106
* @param Session $customerSession
@@ -103,6 +110,7 @@ class EditPost extends AbstractAccount implements CsrfAwareActionInterface, Http
103110
* @param CustomerExtractor $customerExtractor
104111
* @param Escaper|null $escaper
105112
* @param AddressRegistry|null $addressRegistry
113+
* @param Filesystem $filesystem
106114
*/
107115
public function __construct(
108116
Context $context,
@@ -112,7 +120,8 @@ public function __construct(
112120
Validator $formKeyValidator,
113121
CustomerExtractor $customerExtractor,
114122
?Escaper $escaper = null,
115-
AddressRegistry $addressRegistry = null
123+
AddressRegistry $addressRegistry = null,
124+
Filesystem $filesystem = null
116125
) {
117126
parent::__construct($context);
118127
$this->session = $customerSession;
@@ -122,6 +131,7 @@ public function __construct(
122131
$this->customerExtractor = $customerExtractor;
123132
$this->escaper = $escaper ?: ObjectManager::getInstance()->get(Escaper::class);
124133
$this->addressRegistry = $addressRegistry ?: ObjectManager::getInstance()->get(AddressRegistry::class);
134+
$this->filesystem = $filesystem ?: ObjectManager::getInstance()->get(Filesystem::class);
125135
}
126136

127137
/**
@@ -201,6 +211,12 @@ public function execute()
201211
$currentCustomerDataObject
202212
);
203213

214+
$attributeToDelete = $this->_request->getParam('delete_attribute_value');
215+
$this->deleteCustomerFileAttribute(
216+
$customerCandidateDataObject,
217+
$attributeToDelete
218+
);
219+
204220
try {
205221
// whether a customer enabled change email option
206222
$this->processChangeEmailRequest($currentCustomerDataObject);
@@ -388,4 +404,41 @@ private function disableAddressValidation($customer)
388404
$addressModel->setShouldIgnoreValidation(true);
389405
}
390406
}
407+
408+
/**
409+
* Removes file attribute from customer entity and file from filesystem
410+
*
411+
* @param CustomerInterface $customerCandidateDataObject
412+
* @param string $attributeToDelete
413+
* @return void
414+
*/
415+
private function deleteCustomerFileAttribute(
416+
CustomerInterface $customerCandidateDataObject,
417+
string $attributeToDelete
418+
) : void {
419+
if ($attributeToDelete !== '') {
420+
if (strpos($attributeToDelete, ',') !== false) {
421+
$attributes = explode(',', $attributeToDelete);
422+
} else {
423+
$attributes[] = $attributeToDelete;
424+
}
425+
foreach ($attributes as $attr) {
426+
$attributeValue = $customerCandidateDataObject->getCustomAttribute($attr);
427+
if ($attributeValue!== null) {
428+
if ($attributeValue->getValue() !== '') {
429+
$mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
430+
$fileName = $attributeValue->getValue();
431+
$path = $mediaDirectory->getAbsolutePath('customer' . $fileName);
432+
if ($fileName && $mediaDirectory->isFile($path)) {
433+
$mediaDirectory->delete($path);
434+
}
435+
$customerCandidateDataObject->setCustomAttribute(
436+
$attr,
437+
''
438+
);
439+
}
440+
}
441+
}
442+
}
443+
}
391444
}

app/code/Magento/Customer/Controller/Address/FormPost.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
use Magento\Framework\Exception\InputException;
2525
use Magento\Framework\Reflection\DataObjectProcessor;
2626
use Magento\Framework\View\Result\PageFactory;
27+
use Magento\Framework\Filesystem;
28+
use Magento\Framework\App\Filesystem\DirectoryList;
29+
use Magento\Framework\Exception\NotFoundException;
2730

2831
/**
2932
* Customer Address Form Post Controller
@@ -47,6 +50,11 @@ class FormPost extends \Magento\Customer\Controller\Address implements HttpPostA
4750
*/
4851
private $customerAddressMapper;
4952

53+
/**
54+
* @var Filesystem
55+
*/
56+
private $filesystem;
57+
5058
/**
5159
* @param Context $context
5260
* @param Session $customerSession
@@ -61,6 +69,7 @@ class FormPost extends \Magento\Customer\Controller\Address implements HttpPostA
6169
* @param PageFactory $resultPageFactory
6270
* @param RegionFactory $regionFactory
6371
* @param HelperData $helperData
72+
* @param Filesystem $filesystem
6473
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
6574
*/
6675
public function __construct(
@@ -76,10 +85,12 @@ public function __construct(
7685
ForwardFactory $resultForwardFactory,
7786
PageFactory $resultPageFactory,
7887
RegionFactory $regionFactory,
79-
HelperData $helperData
88+
HelperData $helperData,
89+
Filesystem $filesystem = null
8090
) {
8191
$this->regionFactory = $regionFactory;
8292
$this->helperData = $helperData;
93+
$this->filesystem = $filesystem ?: ObjectManager::getInstance()->get(Filesystem::class);
8394
parent::__construct(
8495
$context,
8596
$customerSession,
@@ -150,7 +161,7 @@ protected function getExistingAddressData()
150161
if ($addressId = $this->getRequest()->getParam('id')) {
151162
$existingAddress = $this->_addressRepository->getById($addressId);
152163
if ($existingAddress->getCustomerId() !== $this->_getSession()->getCustomerId()) {
153-
throw new \Exception();
164+
throw new NotFoundException(__('Address not found.'));
154165
}
155166
$existingAddressData = $this->getCustomerAddressMapper()->toFlatArray($existingAddress);
156167
}
@@ -210,6 +221,9 @@ public function execute()
210221

211222
try {
212223
$address = $this->_extractAddress();
224+
if ($this->_request->getParam('delete_attribute_value')) {
225+
$address = $this->deleteAddressFileAttribute($address);
226+
}
213227
$this->_addressRepository->save($address);
214228
$this->messageManager->addSuccessMessage(__('You saved the address.'));
215229
$url = $this->_buildUrl('*/*/index', ['_secure' => true]);
@@ -249,4 +263,31 @@ private function getCustomerAddressMapper()
249263
}
250264
return $this->customerAddressMapper;
251265
}
266+
267+
/**
268+
* Removes file attribute from customer address and file from filesystem
269+
*
270+
* @param \Magento\Customer\Api\Data\AddressInterface $address
271+
* @return mixed
272+
*/
273+
private function deleteAddressFileAttribute($address)
274+
{
275+
$attributeValue = $address->getCustomAttribute($this->_request->getParam('delete_attribute_value'));
276+
if ($attributeValue!== null) {
277+
if ($attributeValue->getValue() !== '') {
278+
$mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
279+
$fileName = $attributeValue->getValue();
280+
$path = $mediaDirectory->getAbsolutePath('customer_address' . $fileName);
281+
if ($fileName && $mediaDirectory->isFile($path)) {
282+
$mediaDirectory->delete($path);
283+
}
284+
$address->setCustomAttribute(
285+
$this->_request->getParam('delete_attribute_value'),
286+
''
287+
);
288+
}
289+
}
290+
291+
return $address;
292+
}
252293
}

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

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,33 @@ public function validate()
100100
* @throws LocalizedException
101101
*/
102102
public function upload()
103+
{
104+
return $this->uploadFile();
105+
}
106+
107+
/**
108+
* File uploading process
109+
*
110+
* @param bool $useScope
111+
* @return string[]
112+
* @throws LocalizedException
113+
*/
114+
public function uploadFile($useScope = true)
103115
{
104116
/** @var FileProcessor $fileProcessor */
105-
$fileProcessor = $this->fileProcessorFactory->create([
106-
'entityTypeCode' => $this->entityTypeCode,
107-
'allowedExtensions' => $this->getAllowedExtensions(),
108-
]);
117+
$fileProcessor = $this->fileProcessorFactory->create(
118+
[
119+
'entityTypeCode' => $this->entityTypeCode,
120+
'allowedExtensions' => $this->getAllowedExtensions(),
121+
]
122+
);
109123

110-
$result = $fileProcessor->saveTemporaryFile($this->scope . '[' . $this->getAttributeCode() . ']');
124+
if ($useScope === true) {
125+
$fileId = $this->scope . '[' . $this->getAttributeCode() . ']';
126+
} else {
127+
$fileId = $this->getAttributeCode();
128+
}
129+
$result = $fileProcessor->saveTemporaryFile($fileId);
111130

112131
// Update tmp_name param. Required for attribute validation!
113132
$result['tmp_name'] = ltrim($result['file'], '/');
@@ -127,7 +146,14 @@ public function upload()
127146
*/
128147
private function getAttributeCode()
129148
{
130-
return key($_FILES[$this->scope]['name']);
149+
// phpcs:disable Magento2.Security.Superglobal
150+
if (is_array($_FILES[$this->scope]['name'])) {
151+
$code = key($_FILES[$this->scope]['name']);
152+
} else {
153+
$code = $this->scope;
154+
}
155+
// phpcs:enable Magento2.Security.Superglobal
156+
return $code;
131157
}
132158

133159
/**
@@ -139,10 +165,16 @@ private function getData()
139165
{
140166
$data = [];
141167

168+
// phpcs:disable Magento2.Security.Superglobal
142169
$fileAttributes = $_FILES[$this->scope];
143170
foreach ($fileAttributes as $attributeName => $attributeValue) {
144-
$data[$attributeName] = $attributeValue[$this->getAttributeCode()];
171+
if (is_array($attributeValue)) {
172+
$data[$attributeName] = $attributeValue[$this->getAttributeCode()];
173+
} else {
174+
$data[$attributeName] = $attributeValue;
175+
}
145176
}
177+
// phpcs:enable Magento2.Security.Superglobal
146178

147179
return $data;
148180
}
@@ -160,9 +192,12 @@ private function getAllowedExtensions()
160192
foreach ($validationRules as $validationRule) {
161193
if ($validationRule->getName() == 'file_extensions') {
162194
$allowedExtensions = explode(',', $validationRule->getValue());
163-
array_walk($allowedExtensions, function (&$value) {
164-
$value = strtolower(trim($value));
165-
});
195+
array_walk(
196+
$allowedExtensions,
197+
function (&$value) {
198+
$value = strtolower(trim($value));
199+
}
200+
);
166201
break;
167202
}
168203
}

0 commit comments

Comments
 (0)