-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
EditPost.php
307 lines (277 loc) · 10.1 KB
/
EditPost.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Controller\Account;
use Magento\Customer\Model\AuthenticationInterface;
use Magento\Customer\Model\Customer\Mapper;
use Magento\Customer\Model\EmailNotificationInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Data\Form\FormKey\Validator;
use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Model\CustomerExtractor;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\InvalidEmailOrPasswordException;
use Magento\Framework\Exception\State\UserLockedException;
/**
* Class EditPost
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class EditPost extends \Magento\Customer\Controller\AbstractAccount
{
/**
* Form code for data extractor
*/
const FORM_DATA_EXTRACTOR_CODE = 'customer_account_edit';
/**
* @var AccountManagementInterface
*/
protected $customerAccountManagement;
/**
* @var CustomerRepositoryInterface
*/
protected $customerRepository;
/**
* @var Validator
*/
protected $formKeyValidator;
/**
* @var CustomerExtractor
*/
protected $customerExtractor;
/**
* @var Session
*/
protected $session;
/** @var EmailNotificationInterface */
private $emailNotification;
/**
* @var AuthenticationInterface
*/
private $authentication;
/**
* @var Mapper
*/
private $customerMapper;
/**
* @param Context $context
* @param Session $customerSession
* @param AccountManagementInterface $customerAccountManagement
* @param CustomerRepositoryInterface $customerRepository
* @param Validator $formKeyValidator
* @param CustomerExtractor $customerExtractor
*/
public function __construct(
Context $context,
Session $customerSession,
AccountManagementInterface $customerAccountManagement,
CustomerRepositoryInterface $customerRepository,
Validator $formKeyValidator,
CustomerExtractor $customerExtractor
) {
parent::__construct($context);
$this->session = $customerSession;
$this->customerAccountManagement = $customerAccountManagement;
$this->customerRepository = $customerRepository;
$this->formKeyValidator = $formKeyValidator;
$this->customerExtractor = $customerExtractor;
}
/**
* Get authentication
*
* @return AuthenticationInterface
*/
private function getAuthentication()
{
if (!($this->authentication instanceof AuthenticationInterface)) {
return ObjectManager::getInstance()->get(
\Magento\Customer\Model\AuthenticationInterface::class
);
} else {
return $this->authentication;
}
}
/**
* Get email notification
*
* @return EmailNotificationInterface
* @deprecated
*/
private function getEmailNotification()
{
if (!($this->emailNotification instanceof EmailNotificationInterface)) {
return ObjectManager::getInstance()->get(
EmailNotificationInterface::class
);
} else {
return $this->emailNotification;
}
}
/**
* Change customer email or password action
*
* @return \Magento\Framework\Controller\Result\Redirect
*/
public function execute()
{
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
$validFormKey = $this->formKeyValidator->validate($this->getRequest());
if ($validFormKey && $this->getRequest()->isPost()) {
$currentCustomerDataObject = $this->getCustomerDataObject($this->session->getCustomerId());
$customerCandidateDataObject = $this->populateNewCustomerDataObject(
$this->_request,
$currentCustomerDataObject
);
try {
// whether a customer enabled change email option
$this->processChangeEmailRequest($currentCustomerDataObject);
// whether a customer enabled change password option
$isPasswordChanged = $this->changeCustomerPassword($currentCustomerDataObject->getEmail());
$this->customerRepository->save($customerCandidateDataObject);
$this->getEmailNotification()->credentialsChanged(
$customerCandidateDataObject,
$currentCustomerDataObject->getEmail(),
$isPasswordChanged
);
$this->dispatchSuccessEvent($customerCandidateDataObject);
$this->messageManager->addSuccess(__('You saved the account information.'));
return $resultRedirect->setPath('customer/account');
} catch (InvalidEmailOrPasswordException $e) {
$this->messageManager->addError($e->getMessage());
} catch (UserLockedException $e) {
$message = __(
'Invalid login or password.'
);
$this->session->logout();
$this->session->start();
$this->messageManager->addError($message);
return $resultRedirect->setPath('customer/account/login');
} catch (InputException $e) {
$this->messageManager->addError($e->getMessage());
foreach ($e->getErrors() as $error) {
$this->messageManager->addError($error->getMessage());
}
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$this->messageManager->addError($e->getMessage());
} catch (\Exception $e) {
$this->messageManager->addException($e, __('We can\'t save the customer.'));
}
$this->session->setCustomerFormData($this->getRequest()->getPostValue());
}
return $resultRedirect->setPath('*/*/edit');
}
/**
* Account editing action completed successfully event
*
* @param \Magento\Customer\Api\Data\CustomerInterface $customerCandidateDataObject
* @return void
*/
private function dispatchSuccessEvent(\Magento\Customer\Api\Data\CustomerInterface $customerCandidateDataObject)
{
$this->_eventManager->dispatch(
'customer_account_edited',
['email' => $customerCandidateDataObject->getEmail()]
);
}
/**
* Get customer data object
*
* @param int $customerId
*
* @return \Magento\Customer\Api\Data\CustomerInterface
*/
private function getCustomerDataObject($customerId)
{
return $this->customerRepository->getById($customerId);
}
/**
* Create Data Transfer Object of customer candidate
*
* @param \Magento\Framework\App\RequestInterface $inputData
* @param \Magento\Customer\Api\Data\CustomerInterface $currentCustomerData
* @return \Magento\Customer\Api\Data\CustomerInterface
*/
private function populateNewCustomerDataObject(
\Magento\Framework\App\RequestInterface $inputData,
\Magento\Customer\Api\Data\CustomerInterface $currentCustomerData
) {
$attributeValues = $this->getCustomerMapper()->toFlatArray($currentCustomerData);
$customerDto = $this->customerExtractor->extract(
self::FORM_DATA_EXTRACTOR_CODE,
$inputData,
$attributeValues
);
$customerDto->setId($currentCustomerData->getId());
if (!$customerDto->getAddresses()) {
$customerDto->setAddresses($currentCustomerData->getAddresses());
}
if (!$inputData->getParam('change_email')) {
$customerDto->setEmail($currentCustomerData->getEmail());
}
return $customerDto;
}
/**
* Change customer password
*
* @param string $email
* @return boolean
* @throws InvalidEmailOrPasswordException|InputException
*/
protected function changeCustomerPassword($email)
{
$isPasswordChanged = false;
if ($this->getRequest()->getParam('change_password')) {
$currPass = $this->getRequest()->getPost('current_password');
$newPass = $this->getRequest()->getPost('password');
$confPass = $this->getRequest()->getPost('password_confirmation');
if ($newPass != $confPass) {
throw new InputException(__('Password confirmation doesn\'t match entered password.'));
}
$isPasswordChanged = $this->customerAccountManagement->changePassword($email, $currPass, $newPass);
}
return $isPasswordChanged;
}
/**
* Process change email request
*
* @param \Magento\Customer\Api\Data\CustomerInterface $currentCustomerDataObject
* @return void
* @throws InvalidEmailOrPasswordException
* @throws UserLockedException
*/
private function processChangeEmailRequest(\Magento\Customer\Api\Data\CustomerInterface $currentCustomerDataObject)
{
if ($this->getRequest()->getParam('change_email')) {
// authenticate user for changing email
try {
$this->getAuthentication()->authenticate(
$currentCustomerDataObject->getId(),
$this->getRequest()->getPost('current_password')
);
} catch (InvalidEmailOrPasswordException $e) {
throw new InvalidEmailOrPasswordException(__('The password doesn\'t match this account.'));
}
}
}
/**
* Get Customer Mapper instance
*
* @return Mapper
*
* @deprecated
*/
private function getCustomerMapper()
{
if ($this->customerMapper === null) {
$this->customerMapper = ObjectManager::getInstance()->get(\Magento\Customer\Model\Customer\Mapper::class);
}
return $this->customerMapper;
}
}