Skip to content

Commit 664056f

Browse files
committed
Fix for the issue #24547 Magento\Customer\Model\Account\Redirect::setRedirectCookie() not properly working
1 parent a15f335 commit 664056f

File tree

5 files changed

+139
-15
lines changed

5 files changed

+139
-15
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
namespace Magento\Customer\Api;
8+
9+
use Magento\Store\Api\Data\StoreInterface;
10+
11+
/**
12+
* Customer redirect cookie manager interface
13+
*
14+
* @api
15+
*/
16+
interface RedirectCookieManagerInterface
17+
{
18+
/**
19+
* Get redirect route from cookie for case of successful login/registration
20+
*
21+
* @return null|string
22+
*/
23+
public function getRedirectCookie();
24+
25+
/**
26+
* Save redirect route to cookie for case of successful login/registration
27+
*
28+
* @param string $route
29+
* @param StoreInterface $store
30+
* @return void
31+
*/
32+
public function setRedirectCookie($route, StoreInterface $store);
33+
34+
/**
35+
* Clear cookie with requested route
36+
*
37+
* @param StoreInterface $store
38+
* @return void
39+
*/
40+
public function clearRedirectCookie(StoreInterface $store);
41+
}

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

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@
2020
use Magento\Framework\Url\DecoderInterface;
2121
use Magento\Framework\App\ObjectManager;
2222
use Magento\Framework\Stdlib\CookieManagerInterface;
23+
use Magento\Customer\Api\RedirectCookieManagerInterface;
2324

2425
/**
2526
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2627
*/
2728
class Redirect
2829
{
29-
/** URL to redirect user on successful login or registration */
30+
/** @deprecated
31+
* URL to redirect user on successful login or registration
32+
*/
3033
const LOGIN_REDIRECT_URL = 'login_redirect';
3134

3235
/**
@@ -71,9 +74,9 @@ class Redirect
7174
protected $cookieManager;
7275

7376
/**
74-
* @var CookieMetadataFactory
77+
* @var RedirectCookieManagerInterface
7578
*/
76-
protected $cookieMetadataFactory;
79+
protected $redirectCookieManager;
7780

7881
/**
7982
* @var HostChecker
@@ -94,7 +97,7 @@ class Redirect
9497
* @param DecoderInterface $urlDecoder
9598
* @param CustomerUrl $customerUrl
9699
* @param ResultFactory $resultFactory
97-
* @param CookieMetadataFactory $cookieMetadataFactory
100+
* @param RedirectCookieManagerInterface $redirectCookieManager
98101
* @param HostChecker|null $hostChecker
99102
*/
100103
public function __construct(
@@ -106,7 +109,7 @@ public function __construct(
106109
DecoderInterface $urlDecoder,
107110
CustomerUrl $customerUrl,
108111
ResultFactory $resultFactory,
109-
CookieMetadataFactory $cookieMetadataFactory,
112+
RedirectCookieManagerInterface $redirectCookieManager,
110113
HostChecker $hostChecker = null
111114
) {
112115
$this->request = $request;
@@ -117,7 +120,7 @@ public function __construct(
117120
$this->urlDecoder = $urlDecoder;
118121
$this->customerUrl = $customerUrl;
119122
$this->resultFactory = $resultFactory;
120-
$this->cookieMetadataFactory = $cookieMetadataFactory;
123+
$this->redirectCookieManager = $redirectCookieManager;
121124
$this->hostChecker = $hostChecker ?: ObjectManager::getInstance()->get(HostChecker::class);
122125
}
123126

@@ -277,7 +280,7 @@ public function setCookieManager($value)
277280
*/
278281
public function getRedirectCookie()
279282
{
280-
return $this->getCookieManager()->getCookie(self::LOGIN_REDIRECT_URL, null);
283+
return $this->redirectCookieManager->getRedirectCookie();
281284
}
282285

283286
/**
@@ -288,11 +291,7 @@ public function getRedirectCookie()
288291
*/
289292
public function setRedirectCookie($route)
290293
{
291-
$cookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()
292-
->setHttpOnly(true)
293-
->setDuration(3600)
294-
->setPath($this->storeManager->getStore()->getStorePath());
295-
$this->getCookieManager()->setPublicCookie(self::LOGIN_REDIRECT_URL, $route, $cookieMetadata);
294+
$this->redirectCookieManager->setRedirectCookie($route, $this->storeManager->getStore());
296295
}
297296

298297
/**
@@ -302,8 +301,6 @@ public function setRedirectCookie($route)
302301
*/
303302
public function clearRedirectCookie()
304303
{
305-
$cookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()
306-
->setPath($this->storeManager->getStore()->getStorePath());
307-
$this->getCookieManager()->deleteCookie(self::LOGIN_REDIRECT_URL, $cookieMetadata);
304+
$this->redirectCookieManager->clearRedirectCookie($this->storeManager->getStore());
308305
}
309306
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
namespace Magento\Customer\Model;
8+
9+
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
10+
use Magento\Framework\Stdlib\CookieManagerInterface;
11+
use Magento\Store\Api\Data\StoreInterface;
12+
use Magento\Customer\Api\RedirectCookieManagerInterface;
13+
14+
class RedirectCookieManager implements RedirectCookieManagerInterface
15+
{
16+
/**
17+
* Cookie name
18+
*/
19+
const COOKIE_NAME = 'login_redirect';
20+
21+
/**
22+
* @var CookieMetadataFactory
23+
*/
24+
protected $cookieMetadataFactory;
25+
26+
/**
27+
* @var CookieManagerInterface
28+
*/
29+
protected $cookieManager;
30+
31+
/**
32+
* @param CookieMetadataFactory $cookieMetadataFactory
33+
* @param CookieManagerInterface $cookieManager
34+
*/
35+
public function __construct(
36+
CookieMetadataFactory $cookieMetadataFactory,
37+
CookieManagerInterface $cookieManager
38+
) {
39+
$this->cookieMetadataFactory = $cookieMetadataFactory;
40+
$this->cookieManager = $cookieManager;
41+
}
42+
43+
/**
44+
* {@inheritdoc}
45+
*/
46+
public function getRedirectCookie()
47+
{
48+
return $this->cookieManager->getCookie(self::COOKIE_NAME, null);
49+
}
50+
51+
/**
52+
* {@inheritdoc}
53+
*/
54+
public function setRedirectCookie($route, StoreInterface $store)
55+
{
56+
$cookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()
57+
->setHttpOnly(true)
58+
->setDuration(3600)
59+
->setPath($store->getStorePath());
60+
$this->cookieManager->setPublicCookie(self::COOKIE_NAME, $route, $cookieMetadata);
61+
}
62+
63+
/**
64+
* {@inheritdoc}
65+
*/
66+
public function clearRedirectCookie(StoreInterface $store)
67+
{
68+
$cookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()
69+
->setPath($store->getStorePath());
70+
$this->cookieManager->deleteCookie(self::COOKIE_NAME, $cookieMetadata);
71+
}
72+
}

app/code/Magento/Customer/Test/Unit/Model/Account/RedirectTest.php

100644100755
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Magento\Customer\Test\Unit\Model\Account;
1010

11+
use Magento\Customer\Api\RedirectCookieManagerInterface;
1112
use Magento\Customer\Model\Account\Redirect;
1213
use Magento\Customer\Model\Url as CustomerUrl;
1314
use Magento\Framework\Controller\ResultFactory;
@@ -80,6 +81,11 @@ class RedirectTest extends \PHPUnit\Framework\TestCase
8081
*/
8182
protected $resultFactory;
8283

84+
/**
85+
* @var RedirectCookieManagerInterface | \PHPUnit_Framework_MockObject_MockObject
86+
*/
87+
protected $redirectCookieManager;
88+
8389
/**
8490
* @var HostChecker | \PHPUnit_Framework_MockObject_MockObject
8591
*/
@@ -139,6 +145,10 @@ protected function setUp()
139145
->disableOriginalConstructor()
140146
->getMock();
141147

148+
$this->redirectCookieManager = $this->getMockBuilder(RedirectCookieManagerInterface::class)
149+
->disableOriginalConstructor()
150+
->getMock();
151+
142152
$this->hostChecker = $this->getMockBuilder(HostChecker::class)
143153
->disableOriginalConstructor()
144154
->getMock();
@@ -155,6 +165,7 @@ protected function setUp()
155165
'urlDecoder' => $this->urlDecoder,
156166
'customerUrl' => $this->customerUrl,
157167
'resultFactory' => $this->resultFactory,
168+
'redirectCookieManager' => $this->redirectCookieManager,
158169
'hostChecker' => $this->hostChecker,
159170
]
160171
);

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

100644100755
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,4 +468,7 @@
468468
<preference
469469
for="Magento\Customer\Api\AccountDelegationInterface"
470470
type="Magento\Customer\Model\Delegation\AccountDelegation" />
471+
<preference
472+
for="Magento\Customer\Api\RedirectCookieManagerInterface"
473+
type="Magento\Customer\Model\RedirectCookieManager" />
471474
</config>

0 commit comments

Comments
 (0)