Skip to content

Commit 03ac5b5

Browse files
committed
Generate new FormKey and replace for oldRequestParams
1 parent 13a933a commit 03ac5b5

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Customer\Model\Plugin;
8+
9+
use Magento\Customer\Model\Session;
10+
use Magento\Framework\Data\Form\FormKey as DataFormKey;
11+
use Magento\PageCache\Observer\FlushFormKey;
12+
13+
class CustomerFlushFormKey
14+
{
15+
/**
16+
* @var Session
17+
*/
18+
private $session;
19+
20+
/**
21+
* @var DataFormKey
22+
*/
23+
private $dataFormKey;
24+
25+
/**
26+
* Initialize dependencies.
27+
*
28+
* @param Session $session
29+
* @param DataFormKey $dataFormKey
30+
*/
31+
public function __construct(Session $session, DataFormKey $dataFormKey)
32+
{
33+
$this->session = $session;
34+
$this->dataFormKey = $dataFormKey;
35+
}
36+
37+
/**
38+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
39+
* @param FlushFormKey $subject
40+
* @param callable $proceed
41+
* @param $args
42+
*/
43+
public function aroundExecute(FlushFormKey $subject, callable $proceed, ...$args)
44+
{
45+
$currentFormKey = $this->dataFormKey->getFormKey();
46+
$proceed(...$args);
47+
$beforeParams = $this->session->getBeforeRequestParams();
48+
if ($beforeParams['form_key'] == $currentFormKey) {
49+
$beforeParams['form_key'] = $this->dataFormKey->getFormKey();
50+
$this->session->setBeforeRequestParams($beforeParams);
51+
}
52+
}
53+
}

app/code/Magento/Customer/etc/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,9 @@
323323
<type name="Magento\Framework\App\Action\AbstractAction">
324324
<plugin name="customerNotification" type="Magento\Customer\Model\Plugin\CustomerNotification"/>
325325
</type>
326+
<type name="Magento\PageCache\Observer\FlushFormKey">
327+
<plugin name="customerFlushFormKey" type="Magento\Customer\Model\Plugin\CustomerFlushFormKey"/>
328+
</type>
326329
<type name="Magento\Customer\Model\Customer\NotificationStorage">
327330
<arguments>
328331
<argument name="cache" xsi:type="object">Magento\Customer\Model\Cache\Type\Notification</argument>

0 commit comments

Comments
 (0)