Skip to content

Commit 72aa3de

Browse files
committed
Merge remote-tracking branch 'origin/MC-25187' into 2.4-develop-pr4
2 parents f6a1af4 + dd7403c commit 72aa3de

File tree

9 files changed

+274
-241
lines changed

9 files changed

+274
-241
lines changed

app/code/Magento/Customer/view/frontend/web/js/customer-data.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ define([
214214
this.reload(storageInvalidation.keys(), false);
215215
}
216216
}
217+
218+
if (!_.isEmpty($.cookieStorage.get('section_data_clean'))) {
219+
this.reload(sectionConfig.getSectionNames(), true);
220+
$.cookieStorage.set('section_data_clean', '');
221+
}
217222
},
218223

219224
/**

app/code/Magento/Store/Controller/Store/Redirect.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,25 @@
77

88
namespace Magento\Store\Controller\Store;
99

10+
use Magento\Framework\App\Action\Action;
1011
use Magento\Framework\App\Action\Context;
12+
use Magento\Framework\App\Action\HttpGetActionInterface;
13+
use Magento\Framework\App\Action\HttpPostActionInterface;
1114
use Magento\Framework\App\ResponseInterface;
15+
use Magento\Framework\Controller\ResultInterface;
1216
use Magento\Framework\Exception\NoSuchEntityException;
17+
use Magento\Framework\Session\Generic as Session;
18+
use Magento\Framework\Session\SidResolverInterface;
1319
use Magento\Store\Api\StoreRepositoryInterface;
1420
use Magento\Store\Api\StoreResolverInterface;
21+
use Magento\Store\Model\Store;
1522
use Magento\Store\Model\StoreResolver;
16-
use Magento\Framework\Session\SidResolverInterface;
17-
use Magento\Framework\Session\Generic as Session;
23+
use Magento\Store\Model\StoreSwitcher\HashGenerator;
1824

1925
/**
2026
* Builds correct url to target store and performs redirect.
2127
*/
22-
class Redirect extends \Magento\Framework\App\Action\Action
28+
class Redirect extends Action implements HttpGetActionInterface, HttpPostActionInterface
2329
{
2430
/**
2531
* @var StoreRepositoryInterface
@@ -41,34 +47,44 @@ class Redirect extends \Magento\Framework\App\Action\Action
4147
*/
4248
private $session;
4349

50+
/**
51+
* @var HashGenerator
52+
*/
53+
private $hashGenerator;
54+
4455
/**
4556
* @param Context $context
4657
* @param StoreRepositoryInterface $storeRepository
4758
* @param StoreResolverInterface $storeResolver
4859
* @param Session $session
4960
* @param SidResolverInterface $sidResolver
61+
* @param HashGenerator $hashGenerator
5062
*/
5163
public function __construct(
5264
Context $context,
5365
StoreRepositoryInterface $storeRepository,
5466
StoreResolverInterface $storeResolver,
5567
Session $session,
56-
SidResolverInterface $sidResolver
68+
SidResolverInterface $sidResolver,
69+
HashGenerator $hashGenerator
5770
) {
5871
parent::__construct($context);
5972
$this->storeRepository = $storeRepository;
6073
$this->storeResolver = $storeResolver;
6174
$this->session = $session;
6275
$this->sidResolver = $sidResolver;
76+
$this->hashGenerator = $hashGenerator;
6377
}
6478

6579
/**
66-
* @return ResponseInterface|\Magento\Framework\Controller\ResultInterface
80+
* Performs store redirect
81+
*
82+
* @return ResponseInterface|ResultInterface
6783
* @throws NoSuchEntityException
6884
*/
6985
public function execute()
7086
{
71-
/** @var \Magento\Store\Model\Store $currentStore */
87+
/** @var Store $currentStore */
7288
$currentStore = $this->storeRepository->getById($this->storeResolver->getCurrentStoreId());
7389
$targetStoreCode = $this->_request->getParam(StoreResolver::PARAM_NAME);
7490
$fromStoreCode = $this->_request->getParam('___from_store');
@@ -79,7 +95,7 @@ public function execute()
7995
}
8096

8197
try {
82-
/** @var \Magento\Store\Model\Store $targetStore */
98+
/** @var Store $fromStore */
8399
$fromStore = $this->storeRepository->get($fromStoreCode);
84100
} catch (NoSuchEntityException $e) {
85101
$error = __('Requested store is not found');
@@ -103,11 +119,16 @@ public function execute()
103119
$query[$sidName] = $this->session->getSessionId();
104120
}
105121

122+
$customerHash = $this->hashGenerator->generateHash($fromStore);
123+
$query = array_merge($query, $customerHash);
124+
106125
$arguments = [
107126
'_nosid' => true,
108127
'_query' => $query
109128
];
110129
$this->_redirect->redirect($this->_response, 'stores/store/switch', $arguments);
111130
}
131+
132+
return null;
112133
}
113134
}

app/code/Magento/Store/Controller/Store/SwitchAction.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Magento\Framework\App\Action\Action;
1212
use Magento\Framework\App\Action\Context as ActionContext;
1313
use Magento\Framework\App\Http\Context as HttpContext;
14-
use Magento\Framework\App\ObjectManager;
1514
use Magento\Framework\Exception\NoSuchEntityException;
1615
use Magento\Store\Api\StoreCookieManagerInterface;
1716
use Magento\Store\Api\StoreRepositoryInterface;
@@ -21,6 +20,7 @@
2120
use Magento\Store\Model\StoreSwitcherInterface;
2221
use Magento\Framework\App\Action\HttpPostActionInterface;
2322
use Magento\Framework\App\Action\HttpGetActionInterface;
23+
use Magento\Store\Controller\Store\SwitchAction\CookieManager;
2424

2525
/**
2626
* Handles store switching url and makes redirect.
@@ -56,6 +56,11 @@ class SwitchAction extends Action implements HttpGetActionInterface, HttpPostAct
5656
*/
5757
private $storeSwitcher;
5858

59+
/**
60+
* @var CookieManager
61+
*/
62+
private $cookieManager;
63+
5964
/**
6065
* Initialize dependencies.
6166
*
@@ -65,35 +70,39 @@ class SwitchAction extends Action implements HttpGetActionInterface, HttpPostAct
6570
* @param StoreRepositoryInterface $storeRepository
6671
* @param StoreManagerInterface $storeManager
6772
* @param StoreSwitcherInterface $storeSwitcher
73+
* @param CookieManager $cookieManager
6874
*/
6975
public function __construct(
7076
ActionContext $context,
7177
StoreCookieManagerInterface $storeCookieManager,
7278
HttpContext $httpContext,
7379
StoreRepositoryInterface $storeRepository,
7480
StoreManagerInterface $storeManager,
75-
StoreSwitcherInterface $storeSwitcher = null
81+
StoreSwitcherInterface $storeSwitcher,
82+
CookieManager $cookieManager
7683
) {
7784
parent::__construct($context);
7885
$this->storeCookieManager = $storeCookieManager;
7986
$this->httpContext = $httpContext;
8087
$this->storeRepository = $storeRepository;
8188
$this->storeManager = $storeManager;
8289
$this->messageManager = $context->getMessageManager();
83-
$this->storeSwitcher = $storeSwitcher ?: ObjectManager::getInstance()->get(StoreSwitcherInterface::class);
90+
$this->storeSwitcher = $storeSwitcher;
91+
$this->cookieManager = $cookieManager;
8492
}
8593

8694
/**
8795
* Execute action
8896
*
8997
* @return void
9098
* @throws StoreSwitcher\CannotSwitchStoreException
99+
* @throws \Magento\Framework\Exception\InputException
100+
* @throws \Magento\Framework\Stdlib\Cookie\CookieSizeLimitReachedException
101+
* @throws \Magento\Framework\Stdlib\Cookie\FailureToSendException
91102
*/
92103
public function execute()
93104
{
94-
$targetStoreCode = $this->_request->getParam(
95-
\Magento\Store\Model\StoreManagerInterface::PARAM_NAME
96-
);
105+
$targetStoreCode = $this->_request->getParam(StoreManagerInterface::PARAM_NAME);
97106
$fromStoreCode = $this->_request->getParam(
98107
'___from_store',
99108
$this->storeCookieManager->getStoreCodeFromCookie()
@@ -115,6 +124,7 @@ public function execute()
115124
$this->messageManager->addErrorMessage($error);
116125
} else {
117126
$redirectUrl = $this->storeSwitcher->switch($fromStore, $targetStore, $requestedUrlToRedirect);
127+
$this->cookieManager->setCookieForStore($targetStore);
118128
}
119129

120130
$this->getResponse()->setRedirect($redirectUrl);
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Magento\Store\Controller\Store\SwitchAction;
10+
11+
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
12+
use Magento\Framework\Stdlib\CookieManagerInterface;
13+
use Magento\Store\Api\Data\StoreInterface;
14+
15+
/**
16+
* Handles store switching cookie for the frontend storage clean
17+
*
18+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
19+
*/
20+
class CookieManager
21+
{
22+
/**
23+
* @var string
24+
*/
25+
const COOKIE_NAME = 'section_data_clean';
26+
27+
/**
28+
* @var CookieMetadataFactory
29+
*/
30+
private $cookieMetadataFactory;
31+
32+
/**
33+
* @var CookieManagerInterface
34+
*/
35+
private $cookieManager;
36+
37+
/**
38+
* @param CookieMetadataFactory $cookieMetadataFactory
39+
* @param CookieManagerInterface $cookieManager
40+
*/
41+
public function __construct(
42+
CookieMetadataFactory $cookieMetadataFactory,
43+
CookieManagerInterface $cookieManager
44+
) {
45+
$this->cookieMetadataFactory = $cookieMetadataFactory;
46+
$this->cookieManager = $cookieManager;
47+
}
48+
49+
/**
50+
* Set cookie for store
51+
*
52+
* @param StoreInterface $targetStore
53+
* @throws \Magento\Framework\Exception\InputException
54+
* @throws \Magento\Framework\Stdlib\Cookie\CookieSizeLimitReachedException
55+
* @throws \Magento\Framework\Stdlib\Cookie\FailureToSendException
56+
*/
57+
public function setCookieForStore(StoreInterface $targetStore)
58+
{
59+
$cookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()
60+
->setHttpOnly(false)
61+
->setDuration(15)
62+
->setPath($targetStore->getStorePath());
63+
$this->cookieManager->setPublicCookie(self::COOKIE_NAME, $targetStore->getCode(), $cookieMetadata);
64+
}
65+
}

app/code/Magento/Store/Model/StoreSwitcher/HashGenerator.php

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,18 @@
77

88
namespace Magento\Store\Model\StoreSwitcher;
99

10+
use Magento\Authorization\Model\UserContextInterface;
11+
use Magento\Framework\App\ActionInterface;
12+
use Magento\Framework\App\DeploymentConfig as DeploymentConfig;
13+
use Magento\Framework\Config\ConfigOptionsListConstants;
14+
use Magento\Framework\Url\Helper\Data as UrlHelper;
1015
use Magento\Store\Api\Data\StoreInterface;
1116
use Magento\Store\Model\StoreSwitcher\HashGenerator\HashData;
12-
use Magento\Store\Model\StoreSwitcherInterface;
13-
use \Magento\Framework\App\DeploymentConfig as DeploymentConfig;
14-
use Magento\Framework\Url\Helper\Data as UrlHelper;
15-
use Magento\Framework\Config\ConfigOptionsListConstants;
16-
use Magento\Authorization\Model\UserContextInterface;
17-
use \Magento\Framework\App\ActionInterface;
1817

1918
/**
2019
* Generate one time token and build redirect url
2120
*/
22-
class HashGenerator implements StoreSwitcherInterface
21+
class HashGenerator
2322
{
2423
/**
2524
* @var \Magento\Framework\App\DeploymentConfig
@@ -52,48 +51,40 @@ public function __construct(
5251
}
5352

5453
/**
55-
* Builds redirect url with token
54+
* Generate hash data for customer
5655
*
57-
* @param StoreInterface $fromStore store where we came from
58-
* @param StoreInterface $targetStore store where to go to
59-
* @param string $redirectUrl original url requested for redirect after switching
60-
* @return string redirect url
61-
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
56+
* @param StoreInterface $fromStore
57+
* @return array
6258
*/
63-
public function switch(StoreInterface $fromStore, StoreInterface $targetStore, string $redirectUrl): string
59+
public function generateHash(StoreInterface $fromStore): array
6460
{
65-
$targetUrl = $redirectUrl;
61+
$key = (string)$this->deploymentConfig->get(ConfigOptionsListConstants::CONFIG_PATH_CRYPT_KEY);
62+
$timeStamp = time();
63+
6664
$customerId = null;
67-
$encodedUrl = $this->urlHelper->getEncodedUrl($redirectUrl);
65+
$result = [];
6866

6967
if ($this->currentUser->getUserType() == UserContextInterface::USER_TYPE_CUSTOMER) {
7068
$customerId = $this->currentUser->getUserId();
71-
}
7269

73-
if ($customerId) {
74-
// phpcs:ignore
75-
$urlParts = parse_url($targetUrl);
76-
$host = $urlParts['host'];
77-
$scheme = $urlParts['scheme'];
78-
$key = (string)$this->deploymentConfig->get(ConfigOptionsListConstants::CONFIG_PATH_CRYPT_KEY);
79-
$timeStamp = time();
80-
$fromStoreCode = $fromStore->getCode();
81-
$data = implode(',', [$customerId, $timeStamp, $fromStoreCode]);
82-
$signature = hash_hmac('sha256', $data, $key);
83-
$targetUrl = $scheme . "://" . $host . '/stores/store/switchrequest';
84-
$targetUrl = $this->urlHelper->addRequestParam(
85-
$targetUrl,
86-
['customer_id' => $customerId]
87-
);
88-
$targetUrl = $this->urlHelper->addRequestParam($targetUrl, ['time_stamp' => $timeStamp]);
89-
$targetUrl = $this->urlHelper->addRequestParam($targetUrl, ['signature' => $signature]);
90-
$targetUrl = $this->urlHelper->addRequestParam($targetUrl, ['___from_store' => $fromStoreCode]);
91-
$targetUrl = $this->urlHelper->addRequestParam(
92-
$targetUrl,
93-
[ActionInterface::PARAM_NAME_URL_ENCODED => $encodedUrl]
70+
$data = implode(
71+
',',
72+
[
73+
$customerId,
74+
$timeStamp,
75+
$fromStore->getCode()
76+
]
9477
);
78+
$signature = hash_hmac('sha256', $data, $key);
79+
80+
$result = [
81+
'customer_id' => $customerId,
82+
'time_stamp' => $timeStamp,
83+
'signature' => $signature
84+
];
9585
}
96-
return $targetUrl;
86+
87+
return $result;
9788
}
9889

9990
/**

0 commit comments

Comments
 (0)