Skip to content

Commit

Permalink
Fix for the issue #24547 Magento\Customer\Model\Account\Redirect::set…
Browse files Browse the repository at this point in the history
…RedirectCookie() not properly working
  • Loading branch information
sashas777 committed Sep 18, 2019
1 parent a15f335 commit 664056f
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 15 deletions.
41 changes: 41 additions & 0 deletions app/code/Magento/Customer/Api/RedirectCookieManagerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Api;

use Magento\Store\Api\Data\StoreInterface;

/**
* Customer redirect cookie manager interface
*
* @api
*/
interface RedirectCookieManagerInterface
{
/**
* Get redirect route from cookie for case of successful login/registration
*
* @return null|string
*/
public function getRedirectCookie();

/**
* Save redirect route to cookie for case of successful login/registration
*
* @param string $route
* @param StoreInterface $store
* @return void
*/
public function setRedirectCookie($route, StoreInterface $store);

/**
* Clear cookie with requested route
*
* @param StoreInterface $store
* @return void
*/
public function clearRedirectCookie(StoreInterface $store);
}
27 changes: 12 additions & 15 deletions app/code/Magento/Customer/Model/Account/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
use Magento\Framework\Url\DecoderInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Stdlib\CookieManagerInterface;
use Magento\Customer\Api\RedirectCookieManagerInterface;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Redirect
{
/** URL to redirect user on successful login or registration */
/** @deprecated
* URL to redirect user on successful login or registration
*/
const LOGIN_REDIRECT_URL = 'login_redirect';

/**
Expand Down Expand Up @@ -71,9 +74,9 @@ class Redirect
protected $cookieManager;

/**
* @var CookieMetadataFactory
* @var RedirectCookieManagerInterface
*/
protected $cookieMetadataFactory;
protected $redirectCookieManager;

/**
* @var HostChecker
Expand All @@ -94,7 +97,7 @@ class Redirect
* @param DecoderInterface $urlDecoder
* @param CustomerUrl $customerUrl
* @param ResultFactory $resultFactory
* @param CookieMetadataFactory $cookieMetadataFactory
* @param RedirectCookieManagerInterface $redirectCookieManager
* @param HostChecker|null $hostChecker
*/
public function __construct(
Expand All @@ -106,7 +109,7 @@ public function __construct(
DecoderInterface $urlDecoder,
CustomerUrl $customerUrl,
ResultFactory $resultFactory,
CookieMetadataFactory $cookieMetadataFactory,
RedirectCookieManagerInterface $redirectCookieManager,
HostChecker $hostChecker = null
) {
$this->request = $request;
Expand All @@ -117,7 +120,7 @@ public function __construct(
$this->urlDecoder = $urlDecoder;
$this->customerUrl = $customerUrl;
$this->resultFactory = $resultFactory;
$this->cookieMetadataFactory = $cookieMetadataFactory;
$this->redirectCookieManager = $redirectCookieManager;
$this->hostChecker = $hostChecker ?: ObjectManager::getInstance()->get(HostChecker::class);
}

Expand Down Expand Up @@ -277,7 +280,7 @@ public function setCookieManager($value)
*/
public function getRedirectCookie()
{
return $this->getCookieManager()->getCookie(self::LOGIN_REDIRECT_URL, null);
return $this->redirectCookieManager->getRedirectCookie();
}

/**
Expand All @@ -288,11 +291,7 @@ public function getRedirectCookie()
*/
public function setRedirectCookie($route)
{
$cookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()
->setHttpOnly(true)
->setDuration(3600)
->setPath($this->storeManager->getStore()->getStorePath());
$this->getCookieManager()->setPublicCookie(self::LOGIN_REDIRECT_URL, $route, $cookieMetadata);
$this->redirectCookieManager->setRedirectCookie($route, $this->storeManager->getStore());
}

/**
Expand All @@ -302,8 +301,6 @@ public function setRedirectCookie($route)
*/
public function clearRedirectCookie()
{
$cookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()
->setPath($this->storeManager->getStore()->getStorePath());
$this->getCookieManager()->deleteCookie(self::LOGIN_REDIRECT_URL, $cookieMetadata);
$this->redirectCookieManager->clearRedirectCookie($this->storeManager->getStore());
}
}
72 changes: 72 additions & 0 deletions app/code/Magento/Customer/Model/RedirectCookieManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Model;

use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
use Magento\Framework\Stdlib\CookieManagerInterface;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Customer\Api\RedirectCookieManagerInterface;

class RedirectCookieManager implements RedirectCookieManagerInterface
{
/**
* Cookie name
*/
const COOKIE_NAME = 'login_redirect';

/**
* @var CookieMetadataFactory
*/
protected $cookieMetadataFactory;

/**
* @var CookieManagerInterface
*/
protected $cookieManager;

/**
* @param CookieMetadataFactory $cookieMetadataFactory
* @param CookieManagerInterface $cookieManager
*/
public function __construct(
CookieMetadataFactory $cookieMetadataFactory,
CookieManagerInterface $cookieManager
) {
$this->cookieMetadataFactory = $cookieMetadataFactory;
$this->cookieManager = $cookieManager;
}

/**
* {@inheritdoc}
*/
public function getRedirectCookie()
{
return $this->cookieManager->getCookie(self::COOKIE_NAME, null);
}

/**
* {@inheritdoc}
*/
public function setRedirectCookie($route, StoreInterface $store)
{
$cookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()
->setHttpOnly(true)
->setDuration(3600)
->setPath($store->getStorePath());
$this->cookieManager->setPublicCookie(self::COOKIE_NAME, $route, $cookieMetadata);
}

/**
* {@inheritdoc}
*/
public function clearRedirectCookie(StoreInterface $store)
{
$cookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()
->setPath($store->getStorePath());
$this->cookieManager->deleteCookie(self::COOKIE_NAME, $cookieMetadata);
}
}
11 changes: 11 additions & 0 deletions app/code/Magento/Customer/Test/Unit/Model/Account/RedirectTest.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

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

use Magento\Customer\Api\RedirectCookieManagerInterface;
use Magento\Customer\Model\Account\Redirect;
use Magento\Customer\Model\Url as CustomerUrl;
use Magento\Framework\Controller\ResultFactory;
Expand Down Expand Up @@ -80,6 +81,11 @@ class RedirectTest extends \PHPUnit\Framework\TestCase
*/
protected $resultFactory;

/**
* @var RedirectCookieManagerInterface | \PHPUnit_Framework_MockObject_MockObject
*/
protected $redirectCookieManager;

/**
* @var HostChecker | \PHPUnit_Framework_MockObject_MockObject
*/
Expand Down Expand Up @@ -139,6 +145,10 @@ protected function setUp()
->disableOriginalConstructor()
->getMock();

$this->redirectCookieManager = $this->getMockBuilder(RedirectCookieManagerInterface::class)
->disableOriginalConstructor()
->getMock();

$this->hostChecker = $this->getMockBuilder(HostChecker::class)
->disableOriginalConstructor()
->getMock();
Expand All @@ -155,6 +165,7 @@ protected function setUp()
'urlDecoder' => $this->urlDecoder,
'customerUrl' => $this->customerUrl,
'resultFactory' => $this->resultFactory,
'redirectCookieManager' => $this->redirectCookieManager,
'hostChecker' => $this->hostChecker,
]
);
Expand Down
3 changes: 3 additions & 0 deletions app/code/Magento/Customer/etc/di.xml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -468,4 +468,7 @@
<preference
for="Magento\Customer\Api\AccountDelegationInterface"
type="Magento\Customer\Model\Delegation\AccountDelegation" />
<preference
for="Magento\Customer\Api\RedirectCookieManagerInterface"
type="Magento\Customer\Model\RedirectCookieManager" />
</config>

0 comments on commit 664056f

Please sign in to comment.