Skip to content

Commit 27e3cf1

Browse files
author
Oleksii Korshenko
authored
Merge pull request #543 from magento-okapis/MAGETWO-58335-Uploaded-Image-2.1.3
Fixed issue: - MAGETWO-58335 [Backport] - Can't view uploaded image
2 parents 71420bb + 9d0b052 commit 27e3cf1

File tree

29 files changed

+3892
-406
lines changed

29 files changed

+3892
-406
lines changed

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

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
namespace Magento\Customer\Controller\Account;
88

99
use Magento\Customer\Model\AuthenticationInterface;
10+
use Magento\Customer\Model\Customer\Mapper;
1011
use Magento\Customer\Model\EmailNotificationInterface;
1112
use Magento\Framework\App\Config\ScopeConfigInterface;
13+
use Magento\Framework\App\ObjectManager;
1214
use Magento\Framework\Data\Form\FormKey\Validator;
1315
use Magento\Customer\Api\AccountManagementInterface;
1416
use Magento\Customer\Api\CustomerRepositoryInterface;
@@ -68,6 +70,11 @@ class EditPost extends \Magento\Customer\Controller\AbstractAccount
6870
*/
6971
private $authentication;
7072

73+
/**
74+
* @var Mapper
75+
*/
76+
private $customerMapper;
77+
7178
/**
7279
* @param Context $context
7380
* @param Session $customerSession
@@ -101,7 +108,7 @@ private function getAuthentication()
101108
{
102109

103110
if (!($this->authentication instanceof AuthenticationInterface)) {
104-
return \Magento\Framework\App\ObjectManager::getInstance()->get(
111+
return ObjectManager::getInstance()->get(
105112
\Magento\Customer\Model\AuthenticationInterface::class
106113
);
107114
} else {
@@ -118,7 +125,7 @@ private function getAuthentication()
118125
private function getEmailNotification()
119126
{
120127
if (!($this->emailNotification instanceof EmailNotificationInterface)) {
121-
return \Magento\Framework\App\ObjectManager::getInstance()->get(
128+
return ObjectManager::getInstance()->get(
122129
EmailNotificationInterface::class
123130
);
124131
} else {
@@ -196,7 +203,7 @@ public function execute()
196203
private function getScopeConfig()
197204
{
198205
if (!($this->scopeConfig instanceof \Magento\Framework\App\Config\ScopeConfigInterface)) {
199-
return \Magento\Framework\App\ObjectManager::getInstance()->get(
206+
return ObjectManager::getInstance()->get(
200207
\Magento\Framework\App\Config\ScopeConfigInterface::class
201208
);
202209
} else {
@@ -241,7 +248,12 @@ private function populateNewCustomerDataObject(
241248
\Magento\Framework\App\RequestInterface $inputData,
242249
\Magento\Customer\Api\Data\CustomerInterface $currentCustomerData
243250
) {
244-
$customerDto = $this->customerExtractor->extract(self::FORM_DATA_EXTRACTOR_CODE, $inputData);
251+
$attributeValues = $this->getCustomerMapper()->toFlatArray($currentCustomerData);
252+
$customerDto = $this->customerExtractor->extract(
253+
self::FORM_DATA_EXTRACTOR_CODE,
254+
$inputData,
255+
$attributeValues
256+
);
245257
$customerDto->setId($currentCustomerData->getId());
246258
if (!$customerDto->getAddresses()) {
247259
$customerDto->setAddresses($currentCustomerData->getAddresses());
@@ -299,4 +311,19 @@ private function processChangeEmailRequest(\Magento\Customer\Api\Data\CustomerIn
299311
}
300312
}
301313
}
314+
315+
/**
316+
* Get Customer Mapper instance
317+
*
318+
* @return Mapper
319+
*
320+
* @deprecated
321+
*/
322+
private function getCustomerMapper()
323+
{
324+
if ($this->customerMapper === null) {
325+
$this->customerMapper = ObjectManager::getInstance()->get('Magento\Customer\Model\Customer\Mapper');
326+
}
327+
return $this->customerMapper;
328+
}
302329
}

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

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
use Magento\Customer\Api\Data\AddressInterfaceFactory;
1010
use Magento\Customer\Api\Data\RegionInterface;
1111
use Magento\Customer\Api\Data\RegionInterfaceFactory;
12+
use Magento\Customer\Model\Address\Mapper;
1213
use Magento\Customer\Model\Metadata\FormFactory;
1314
use Magento\Customer\Model\Session;
1415
use Magento\Directory\Helper\Data as HelperData;
1516
use Magento\Directory\Model\RegionFactory;
1617
use Magento\Framework\Api\DataObjectHelper;
1718
use Magento\Framework\App\Action\Context;
19+
use Magento\Framework\App\ObjectManager;
1820
use Magento\Framework\Controller\Result\ForwardFactory;
1921
use Magento\Framework\Data\Form\FormKey\Validator as FormKeyValidator;
2022
use Magento\Framework\Exception\InputException;
@@ -36,6 +38,11 @@ class FormPost extends \Magento\Customer\Controller\Address
3638
*/
3739
protected $helperData;
3840

41+
/**
42+
* @var Mapper
43+
*/
44+
private $customerAddressMapper;
45+
3946
/**
4047
* @param Context $context
4148
* @param Session $customerSession
@@ -127,12 +134,7 @@ protected function getExistingAddressData()
127134
if ($existingAddress->getCustomerId() !== $this->_getSession()->getCustomerId()) {
128135
throw new \Exception();
129136
}
130-
$existingAddressData = $this->_dataProcessor->buildOutputDataArray(
131-
$existingAddress,
132-
'\Magento\Customer\Api\Data\AddressInterface'
133-
);
134-
$existingAddressData['region_code'] = $existingAddress->getRegion()->getRegionCode();
135-
$existingAddressData['region'] = $existingAddress->getRegion()->getRegion();
137+
$existingAddressData = $this->getCustomerAddressMapper()->toFlatArray($existingAddress);
136138
}
137139
return $existingAddressData;
138140
}
@@ -212,4 +214,19 @@ public function execute()
212214

213215
return $this->resultRedirectFactory->create()->setUrl($this->_redirect->error($url));
214216
}
217+
218+
/**
219+
* Get Customer Address Mapper instance
220+
*
221+
* @return Mapper
222+
*
223+
* @deprecated
224+
*/
225+
private function getCustomerAddressMapper()
226+
{
227+
if ($this->customerAddressMapper === null) {
228+
$this->customerAddressMapper = ObjectManager::getInstance()->get('Magento\Customer\Model\Address\Mapper');
229+
}
230+
return $this->customerAddressMapper;
231+
}
215232
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Customer\Controller\Adminhtml\File\Address;
7+
8+
use Magento\Backend\App\Action;
9+
use Magento\Backend\App\Action\Context;
10+
use Magento\Customer\Api\AddressMetadataInterface;
11+
use Magento\Customer\Model\FileUploader;
12+
use Magento\Customer\Model\FileUploaderFactory;
13+
use Magento\Framework\Controller\ResultFactory;
14+
use Magento\Framework\Exception\LocalizedException;
15+
use Psr\Log\LoggerInterface;
16+
17+
class Upload extends Action
18+
{
19+
/**
20+
* Authorization level of a basic admin session
21+
*
22+
* @see _isAllowed()
23+
*/
24+
const ADMIN_RESOURCE = 'Magento_Customer::manage';
25+
26+
/**
27+
* @var FileUploaderFactory
28+
*/
29+
private $fileUploaderFactory;
30+
31+
/**
32+
* @var AddressMetadataInterface
33+
*/
34+
private $addressMetadataService;
35+
36+
/**
37+
* @var LoggerInterface
38+
*/
39+
private $logger;
40+
41+
/**
42+
* @param Context $context
43+
* @param FileUploaderFactory $fileUploaderFactory
44+
* @param AddressMetadataInterface $addressMetadataService
45+
* @param LoggerInterface $logger
46+
*/
47+
public function __construct(
48+
Context $context,
49+
FileUploaderFactory $fileUploaderFactory,
50+
AddressMetadataInterface $addressMetadataService,
51+
LoggerInterface $logger
52+
) {
53+
$this->fileUploaderFactory = $fileUploaderFactory;
54+
$this->addressMetadataService = $addressMetadataService;
55+
$this->logger = $logger;
56+
parent::__construct($context);
57+
}
58+
59+
/**
60+
* @inheritDoc
61+
*/
62+
public function execute()
63+
{
64+
try {
65+
if (empty($_FILES)) {
66+
throw new \Exception('$_FILES array is empty.');
67+
}
68+
69+
// Must be executed before any operations with $_FILES!
70+
$this->convertFilesArray();
71+
72+
$attributeCode = key($_FILES['address']['name']);
73+
$attributeMetadata = $this->addressMetadataService->getAttributeMetadata($attributeCode);
74+
75+
/** @var FileUploader $fileUploader */
76+
$fileUploader = $this->fileUploaderFactory->create([
77+
'attributeMetadata' => $attributeMetadata,
78+
'entityTypeCode' => AddressMetadataInterface::ENTITY_TYPE_ADDRESS,
79+
'scope' => 'address',
80+
]);
81+
82+
$errors = $fileUploader->validate();
83+
if (true !== $errors) {
84+
$errorMessage = implode('</br>', $errors);
85+
throw new LocalizedException(__($errorMessage));
86+
}
87+
88+
$result = $fileUploader->upload();
89+
} catch (LocalizedException $e) {
90+
$result = [
91+
'error' => $e->getMessage(),
92+
'errorcode' => $e->getCode(),
93+
];
94+
} catch (\Exception $e) {
95+
$this->logger->critical($e);
96+
$result = [
97+
'error' => __('Something went wrong while saving file.'),
98+
'errorcode' => $e->getCode(),
99+
];
100+
}
101+
102+
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
103+
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
104+
$resultJson->setData($result);
105+
return $resultJson;
106+
}
107+
108+
/**
109+
* Update global $_FILES array. Convert data to standard form
110+
*
111+
* NOTE: This conversion is required to use \Magento\Framework\File\Uploader::_setUploadFileId($fileId) method.
112+
*
113+
* @return void
114+
*/
115+
private function convertFilesArray()
116+
{
117+
foreach ($_FILES['address'] as $itemKey => $item) {
118+
foreach ($item as $value) {
119+
if (is_array($value)) {
120+
$_FILES['address'][$itemKey] = [
121+
key($value) => current($value),
122+
];
123+
}
124+
}
125+
}
126+
}
127+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Customer\Controller\Adminhtml\File\Customer;
7+
8+
use Magento\Backend\App\Action;
9+
use Magento\Backend\App\Action\Context;
10+
use Magento\Customer\Api\CustomerMetadataInterface;
11+
use Magento\Customer\Model\FileUploader;
12+
use Magento\Customer\Model\FileUploaderFactory;
13+
use Magento\Framework\Controller\ResultFactory;
14+
use Magento\Framework\Exception\LocalizedException;
15+
use Psr\Log\LoggerInterface;
16+
17+
class Upload extends Action
18+
{
19+
/**
20+
* Authorization level of a basic admin session
21+
*
22+
* @see _isAllowed()
23+
*/
24+
const ADMIN_RESOURCE = 'Magento_Customer::manage';
25+
26+
/**
27+
* @var FileUploaderFactory
28+
*/
29+
private $fileUploaderFactory;
30+
31+
/**
32+
* @var CustomerMetadataInterface
33+
*/
34+
private $customerMetadataService;
35+
36+
/**
37+
* @var LoggerInterface
38+
*/
39+
private $logger;
40+
41+
/**
42+
* @param Context $context
43+
* @param FileUploaderFactory $fileUploaderFactory
44+
* @param CustomerMetadataInterface $customerMetadataService
45+
* @param LoggerInterface $logger
46+
*/
47+
public function __construct(
48+
Context $context,
49+
FileUploaderFactory $fileUploaderFactory,
50+
CustomerMetadataInterface $customerMetadataService,
51+
LoggerInterface $logger
52+
) {
53+
$this->fileUploaderFactory = $fileUploaderFactory;
54+
$this->customerMetadataService = $customerMetadataService;
55+
$this->logger = $logger;
56+
parent::__construct($context);
57+
}
58+
59+
/**
60+
* @inheritDoc
61+
*/
62+
public function execute()
63+
{
64+
try {
65+
if (empty($_FILES)) {
66+
throw new \Exception('$_FILES array is empty.');
67+
}
68+
69+
$attributeCode = key($_FILES['customer']['name']);
70+
$attributeMetadata = $this->customerMetadataService->getAttributeMetadata($attributeCode);
71+
72+
/** @var FileUploader $fileUploader */
73+
$fileUploader = $this->fileUploaderFactory->create([
74+
'attributeMetadata' => $attributeMetadata,
75+
'entityTypeCode' => CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
76+
'scope' => 'customer',
77+
]);
78+
79+
$errors = $fileUploader->validate();
80+
if (true !== $errors) {
81+
$errorMessage = implode('</br>', $errors);
82+
throw new LocalizedException(__($errorMessage));
83+
}
84+
85+
$result = $fileUploader->upload();
86+
} catch (LocalizedException $e) {
87+
$result = [
88+
'error' => $e->getMessage(),
89+
'errorcode' => $e->getCode(),
90+
];
91+
} catch (\Exception $e) {
92+
$this->logger->critical($e);
93+
$result = [
94+
'error' => __('Something went wrong while saving file.'),
95+
'errorcode' => $e->getCode(),
96+
];
97+
}
98+
99+
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
100+
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
101+
$resultJson->setData($result);
102+
return $resultJson;
103+
}
104+
}

0 commit comments

Comments
 (0)