Skip to content

Commit a7ba3fc

Browse files
ENGCOM-6891: Fix for the issue #24547 Magento\Customer\Model\Account\Redirect::setRedirectCookie() not properly working #24612
- Merge Pull Request #24612 from sashas777/magento2:issue-24547 - Merged commits: 1. a15f335 2. 664056f 3. 5188efd 4. 3cf8f22 5. 7597003 6. 9413892 7. 9f2bbc2 8. bfa636f 9. 6446aed 10. 162ca60 11. 16da706 12. 2c24ef9 13. a514803 14. bfeadd7 15. cd5ef3c 16. dc416a9 17. 41ce59e 18. f021f1e 19. 511e16a
2 parents 293cd41 + 511e16a commit a7ba3fc

File tree

2 files changed

+196
-71
lines changed

2 files changed

+196
-71
lines changed

app/code/Magento/Customer/Model/Account/Redirect.php

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,28 @@
77

88
use Magento\Customer\Model\Session;
99
use Magento\Customer\Model\Url as CustomerUrl;
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Framework\App\ObjectManager;
1012
use Magento\Framework\App\RequestInterface;
13+
use Magento\Framework\Controller\Result\Forward as ResultForward;
14+
use Magento\Framework\Controller\Result\Redirect as ResultRedirect;
1115
use Magento\Framework\Controller\ResultFactory;
16+
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
17+
use Magento\Framework\Stdlib\CookieManagerInterface;
18+
use Magento\Framework\Url\DecoderInterface;
1219
use Magento\Framework\Url\HostChecker;
1320
use Magento\Framework\UrlInterface;
1421
use Magento\Store\Model\ScopeInterface;
1522
use Magento\Store\Model\StoreManagerInterface;
16-
use Magento\Framework\App\Config\ScopeConfigInterface;
17-
use Magento\Framework\Controller\Result\Redirect as ResultRedirect;
18-
use Magento\Framework\Controller\Result\Forward as ResultForward;
19-
use Magento\Framework\Url\DecoderInterface;
20-
use Magento\Framework\App\ObjectManager;
21-
use Magento\Framework\Stdlib\CookieManagerInterface;
2223

2324
/**
25+
* Account Redirect
2426
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
2528
*/
2629
class Redirect
2730
{
31+
2832
/** URL to redirect user on successful login or registration */
2933
const LOGIN_REDIRECT_URL = 'login_redirect';
3034

@@ -64,10 +68,15 @@ class Redirect
6468
*/
6569
protected $resultFactory;
6670

71+
/**
72+
* @var CookieMetadataFactory
73+
*/
74+
protected $cookieMetadataFactory;
75+
6776
/**
6877
* @var CookieManagerInterface
6978
*/
70-
protected $cookieManager;
79+
private $cookieManager;
7180

7281
/**
7382
* @var HostChecker
@@ -80,6 +89,7 @@ class Redirect
8089
private $session;
8190

8291
/**
92+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
8393
* @param RequestInterface $request
8494
* @param Session $customerSession
8595
* @param ScopeConfigInterface $scopeConfig
@@ -88,6 +98,7 @@ class Redirect
8898
* @param DecoderInterface $urlDecoder
8999
* @param CustomerUrl $customerUrl
90100
* @param ResultFactory $resultFactory
101+
* @param CookieMetadataFactory $cookieMetadataFactory
91102
* @param HostChecker|null $hostChecker
92103
*/
93104
public function __construct(
@@ -99,6 +110,7 @@ public function __construct(
99110
DecoderInterface $urlDecoder,
100111
CustomerUrl $customerUrl,
101112
ResultFactory $resultFactory,
113+
CookieMetadataFactory $cookieMetadataFactory,
102114
HostChecker $hostChecker = null
103115
) {
104116
$this->request = $request;
@@ -108,6 +120,7 @@ public function __construct(
108120
$this->url = $url;
109121
$this->urlDecoder = $urlDecoder;
110122
$this->customerUrl = $customerUrl;
123+
$this->cookieMetadataFactory = $cookieMetadataFactory;
111124
$this->resultFactory = $resultFactory;
112125
$this->hostChecker = $hostChecker ?: ObjectManager::getInstance()->get(HostChecker::class);
113126
}
@@ -238,7 +251,8 @@ private function applyRedirect($url)
238251
/**
239252
* Get Cookie manager. For release backward compatibility.
240253
*
241-
* @deprecated 100.0.10
254+
* @deprecated 100.0.10 This is legacy method to pass login_redirect cookie
255+
* @see Magento/Checkout/view/frontend/web/js/sidebar.js
242256
* @return CookieManagerInterface
243257
*/
244258
protected function getCookieManager()
@@ -252,7 +266,8 @@ protected function getCookieManager()
252266
/**
253267
* Set cookie manager. For unit tests.
254268
*
255-
* @deprecated 100.0.10
269+
* @deprecated 100.0.10 This is legacy method to pass login_redirect cookie
270+
* @see Magento/Checkout/view/frontend/web/js/sidebar.js
256271
* @param object $value
257272
* @return void
258273
*/
@@ -264,6 +279,8 @@ public function setCookieManager($value)
264279
/**
265280
* Get redirect route from cookie for case of successful login/registration
266281
*
282+
* @deprecated 100.0.10 This is legacy method to pass login_redirect cookie
283+
* @see Magento/Checkout/view/frontend/web/js/sidebar.js
267284
* @return null|string
268285
*/
269286
public function getRedirectCookie()
@@ -274,21 +291,36 @@ public function getRedirectCookie()
274291
/**
275292
* Save redirect route to cookie for case of successful login/registration
276293
*
294+
* @deprecated 100.0.10 This is legacy method to pass login_redirect cookie
295+
* @see Magento/Checkout/view/frontend/web/js/sidebar.js
277296
* @param string $route
278297
* @return void
279298
*/
280299
public function setRedirectCookie($route)
281300
{
282-
$this->getCookieManager()->setPublicCookie(self::LOGIN_REDIRECT_URL, $route);
301+
$this->getCookieManager()->setPublicCookie(
302+
self::LOGIN_REDIRECT_URL,
303+
$route,
304+
$this->cookieMetadataFactory->createPublicCookieMetadata()
305+
->setHttpOnly(true)
306+
->setDuration(3600)
307+
->setPath($this->storeManager->getStore()->getStorePath())
308+
);
283309
}
284310

285311
/**
286312
* Clear cookie with requested route
287313
*
314+
* @deprecated 100.0.10 This is legacy method to pass login_redirect cookie
315+
* @see Magento/Checkout/view/frontend/web/js/sidebar.js
288316
* @return void
289317
*/
290318
public function clearRedirectCookie()
291319
{
292-
$this->getCookieManager()->deleteCookie(self::LOGIN_REDIRECT_URL);
320+
$this->getCookieManager()->deleteCookie(
321+
self::LOGIN_REDIRECT_URL,
322+
$this->cookieMetadataFactory->createPublicCookieMetadata()
323+
->setPath($this->storeManager->getStore()->getStorePath())
324+
);
293325
}
294326
}

0 commit comments

Comments
 (0)