7
7
8
8
namespace Magento \Customer \Controller \Account ;
9
9
10
+ use Exception ;
10
11
use Magento \Customer \Api \Data \CustomerInterface ;
12
+ use Magento \Customer \Api \SessionCleanerInterface ;
11
13
use Magento \Customer \Model \AddressRegistry ;
12
14
use Magento \Framework \App \Action \HttpPostActionInterface as HttpPostActionInterface ;
13
15
use Magento \Customer \Model \AuthenticationInterface ;
27
29
use Magento \Framework \Escaper ;
28
30
use Magento \Framework \Exception \InputException ;
29
31
use Magento \Framework \Exception \InvalidEmailOrPasswordException ;
32
+ use Magento \Framework \Exception \LocalizedException ;
30
33
use Magento \Framework \Exception \NoSuchEntityException ;
31
34
use Magento \Framework \Exception \State \UserLockedException ;
32
35
use Magento \Customer \Controller \AbstractAccount ;
33
36
use Magento \Framework \Phrase ;
34
37
35
38
/**
36
- * Class EditPost
39
+ * Customer edit page.
40
+ *
37
41
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
38
42
*/
39
43
class EditPost extends AbstractAccount implements CsrfAwareActionInterface, HttpPostActionInterface
@@ -69,7 +73,7 @@ class EditPost extends AbstractAccount implements CsrfAwareActionInterface, Http
69
73
protected $ session ;
70
74
71
75
/**
72
- * @var \Magento\Customer\Model\ EmailNotificationInterface
76
+ * @var EmailNotificationInterface
73
77
*/
74
78
private $ emailNotification ;
75
79
@@ -93,6 +97,11 @@ class EditPost extends AbstractAccount implements CsrfAwareActionInterface, Http
93
97
*/
94
98
private $ addressRegistry ;
95
99
100
+ /**
101
+ * @var SessionCleanerInterface|null
102
+ */
103
+ private $ sessionCleaner ;
104
+
96
105
/**
97
106
* @param Context $context
98
107
* @param Session $customerSession
@@ -102,6 +111,7 @@ class EditPost extends AbstractAccount implements CsrfAwareActionInterface, Http
102
111
* @param CustomerExtractor $customerExtractor
103
112
* @param Escaper|null $escaper
104
113
* @param AddressRegistry|null $addressRegistry
114
+ * @param SessionCleanerInterface|null $sessionCleaner
105
115
*/
106
116
public function __construct (
107
117
Context $ context ,
@@ -111,7 +121,8 @@ public function __construct(
111
121
Validator $ formKeyValidator ,
112
122
CustomerExtractor $ customerExtractor ,
113
123
?Escaper $ escaper = null ,
114
- AddressRegistry $ addressRegistry = null
124
+ AddressRegistry $ addressRegistry = null ,
125
+ ?SessionCleanerInterface $ sessionCleaner = null
115
126
) {
116
127
parent ::__construct ($ context );
117
128
$ this ->session = $ customerSession ;
@@ -121,6 +132,7 @@ public function __construct(
121
132
$ this ->customerExtractor = $ customerExtractor ;
122
133
$ this ->escaper = $ escaper ?: ObjectManager::getInstance ()->get (Escaper::class);
123
134
$ this ->addressRegistry = $ addressRegistry ?: ObjectManager::getInstance ()->get (AddressRegistry::class);
135
+ $ this ->sessionCleaner = $ sessionCleaner ?: ObjectManager::getInstance ()->get (SessionCleanerInterface::class);
124
136
}
125
137
126
138
/**
@@ -132,9 +144,7 @@ private function getAuthentication()
132
144
{
133
145
134
146
if (!($ this ->authentication instanceof AuthenticationInterface)) {
135
- return ObjectManager::getInstance ()->get (
136
- \Magento \Customer \Model \AuthenticationInterface::class
137
- );
147
+ return ObjectManager::getInstance ()->get (AuthenticationInterface::class);
138
148
} else {
139
149
return $ this ->authentication ;
140
150
}
@@ -149,9 +159,7 @@ private function getAuthentication()
149
159
private function getEmailNotification ()
150
160
{
151
161
if (!($ this ->emailNotification instanceof EmailNotificationInterface)) {
152
- return ObjectManager::getInstance ()->get (
153
- EmailNotificationInterface::class
154
- );
162
+ return ObjectManager::getInstance ()->get (EmailNotificationInterface::class);
155
163
} else {
156
164
return $ this ->emailNotification ;
157
165
}
@@ -160,9 +168,8 @@ private function getEmailNotification()
160
168
/**
161
169
* @inheritDoc
162
170
*/
163
- public function createCsrfValidationException (
164
- RequestInterface $ request
165
- ): ?InvalidRequestException {
171
+ public function createCsrfValidationException (RequestInterface $ request ): ?InvalidRequestException
172
+ {
166
173
/** @var Redirect $resultRedirect */
167
174
$ resultRedirect = $ this ->resultRedirectFactory ->create ();
168
175
$ resultRedirect ->setPath ('*/*/edit ' );
@@ -184,11 +191,11 @@ public function validateForCsrf(RequestInterface $request): ?bool
184
191
/**
185
192
* Change customer email or password action
186
193
*
187
- * @return \Magento\Framework\Controller\Result\ Redirect
194
+ * @return Redirect
188
195
*/
189
196
public function execute ()
190
197
{
191
- /** @var \Magento\Framework\Controller\Result\ Redirect $resultRedirect */
198
+ /** @var Redirect $resultRedirect */
192
199
$ resultRedirect = $ this ->resultRedirectFactory ->create ();
193
200
$ validFormKey = $ this ->formKeyValidator ->validate ($ this ->getRequest ());
194
201
@@ -217,6 +224,7 @@ public function execute()
217
224
);
218
225
$ this ->dispatchSuccessEvent ($ customerCandidateDataObject );
219
226
$ this ->messageManager ->addSuccessMessage (__ ('You saved the account information. ' ));
227
+
220
228
return $ resultRedirect ->setPath ('customer/account ' );
221
229
} catch (InvalidEmailOrPasswordException $ e ) {
222
230
$ this ->messageManager ->addErrorMessage ($ this ->escaper ->escapeHtml ($ e ->getMessage ()));
@@ -228,15 +236,16 @@ public function execute()
228
236
$ this ->session ->logout ();
229
237
$ this ->session ->start ();
230
238
$ this ->messageManager ->addErrorMessage ($ message );
239
+
231
240
return $ resultRedirect ->setPath ('customer/account/login ' );
232
241
} catch (InputException $ e ) {
233
242
$ this ->messageManager ->addErrorMessage ($ this ->escaper ->escapeHtml ($ e ->getMessage ()));
234
243
foreach ($ e ->getErrors () as $ error ) {
235
244
$ this ->messageManager ->addErrorMessage ($ this ->escaper ->escapeHtml ($ error ->getMessage ()));
236
245
}
237
- } catch (\ Magento \ Framework \ Exception \ LocalizedException $ e ) {
246
+ } catch (LocalizedException $ e ) {
238
247
$ this ->messageManager ->addErrorMessage ($ e ->getMessage ());
239
- } catch (\ Exception $ e ) {
248
+ } catch (Exception $ e ) {
240
249
$ this ->messageManager ->addException ($ e , __ ('We can \'t save the customer. ' ));
241
250
}
242
251
@@ -246,16 +255,17 @@ public function execute()
246
255
/** @var Redirect $resultRedirect */
247
256
$ resultRedirect = $ this ->resultRedirectFactory ->create ();
248
257
$ resultRedirect ->setPath ('*/*/edit ' );
258
+
249
259
return $ resultRedirect ;
250
260
}
251
261
252
262
/**
253
263
* Account editing action completed successfully event
254
264
*
255
- * @param \Magento\Customer\Api\Data\ CustomerInterface $customerCandidateDataObject
265
+ * @param CustomerInterface $customerCandidateDataObject
256
266
* @return void
257
267
*/
258
- private function dispatchSuccessEvent (\ Magento \ Customer \ Api \ Data \ CustomerInterface $ customerCandidateDataObject )
268
+ private function dispatchSuccessEvent (CustomerInterface $ customerCandidateDataObject )
259
269
{
260
270
$ this ->_eventManager ->dispatch (
261
271
'customer_account_edited ' ,
@@ -268,7 +278,7 @@ private function dispatchSuccessEvent(\Magento\Customer\Api\Data\CustomerInterfa
268
278
*
269
279
* @param int $customerId
270
280
*
271
- * @return \Magento\Customer\Api\Data\ CustomerInterface
281
+ * @return CustomerInterface
272
282
*/
273
283
private function getCustomerDataObject ($ customerId )
274
284
{
@@ -278,13 +288,13 @@ private function getCustomerDataObject($customerId)
278
288
/**
279
289
* Create Data Transfer Object of customer candidate
280
290
*
281
- * @param \Magento\Framework\App\ RequestInterface $inputData
282
- * @param \Magento\Customer\Api\Data\ CustomerInterface $currentCustomerData
283
- * @return \Magento\Customer\Api\Data\ CustomerInterface
291
+ * @param RequestInterface $inputData
292
+ * @param CustomerInterface $currentCustomerData
293
+ * @return CustomerInterface
284
294
*/
285
295
private function populateNewCustomerDataObject (
286
- \ Magento \ Framework \ App \ RequestInterface $ inputData ,
287
- \ Magento \ Customer \ Api \ Data \ CustomerInterface $ currentCustomerData
296
+ RequestInterface $ inputData ,
297
+ CustomerInterface $ currentCustomerData
288
298
) {
289
299
$ attributeValues = $ this ->getCustomerMapper ()->toFlatArray ($ currentCustomerData );
290
300
$ customerDto = $ this ->customerExtractor ->extract (
@@ -330,12 +340,12 @@ protected function changeCustomerPassword($email)
330
340
/**
331
341
* Process change email request
332
342
*
333
- * @param \Magento\Customer\Api\Data\ CustomerInterface $currentCustomerDataObject
343
+ * @param CustomerInterface $currentCustomerDataObject
334
344
* @return void
335
345
* @throws InvalidEmailOrPasswordException
336
346
* @throws UserLockedException
337
347
*/
338
- private function processChangeEmailRequest (\ Magento \ Customer \ Api \ Data \ CustomerInterface $ currentCustomerDataObject )
348
+ private function processChangeEmailRequest (CustomerInterface $ currentCustomerDataObject )
339
349
{
340
350
if ($ this ->getRequest ()->getParam ('change_email ' )) {
341
351
// authenticate user for changing email
@@ -344,6 +354,7 @@ private function processChangeEmailRequest(\Magento\Customer\Api\Data\CustomerIn
344
354
$ currentCustomerDataObject ->getId (),
345
355
$ this ->getRequest ()->getPost ('current_password ' )
346
356
);
357
+ $ this ->sessionCleaner ->clearFor ($ currentCustomerDataObject ->getId ());
347
358
} catch (InvalidEmailOrPasswordException $ e ) {
348
359
throw new InvalidEmailOrPasswordException (
349
360
__ ("The password doesn't match this account. Verify the password and try again. " )
@@ -362,7 +373,7 @@ private function processChangeEmailRequest(\Magento\Customer\Api\Data\CustomerIn
362
373
private function getCustomerMapper ()
363
374
{
364
375
if ($ this ->customerMapper === null ) {
365
- $ this ->customerMapper = ObjectManager::getInstance ()->get (\ Magento \ Customer \ Model \ Customer \ Mapper::class);
376
+ $ this ->customerMapper = ObjectManager::getInstance ()->get (Mapper::class);
366
377
}
367
378
return $ this ->customerMapper ;
368
379
}
0 commit comments