Skip to content

Commit

Permalink
ENGCOM-6891: Fix for the issue #24547 Magento\Customer\Model\Account\…
Browse files Browse the repository at this point in the history
…Redirect::setRedirectCookie() not properly working #24612
  • Loading branch information
slavvka authored Feb 14, 2020
2 parents 97c8132 + a7ba3fc commit 0d910ea
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 71 deletions.
54 changes: 43 additions & 11 deletions app/code/Magento/Customer/Model/Account/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,28 @@

use Magento\Customer\Model\Session;
use Magento\Customer\Model\Url as CustomerUrl;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\Result\Forward as ResultForward;
use Magento\Framework\Controller\Result\Redirect as ResultRedirect;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
use Magento\Framework\Stdlib\CookieManagerInterface;
use Magento\Framework\Url\DecoderInterface;
use Magento\Framework\Url\HostChecker;
use Magento\Framework\UrlInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Controller\Result\Redirect as ResultRedirect;
use Magento\Framework\Controller\Result\Forward as ResultForward;
use Magento\Framework\Url\DecoderInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Stdlib\CookieManagerInterface;

/**
* Account Redirect
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
*/
class Redirect
{

/** URL to redirect user on successful login or registration */
const LOGIN_REDIRECT_URL = 'login_redirect';

Expand Down Expand Up @@ -64,10 +68,15 @@ class Redirect
*/
protected $resultFactory;

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

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

/**
* @var HostChecker
Expand All @@ -80,6 +89,7 @@ class Redirect
private $session;

/**
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
* @param RequestInterface $request
* @param Session $customerSession
* @param ScopeConfigInterface $scopeConfig
Expand All @@ -88,6 +98,7 @@ class Redirect
* @param DecoderInterface $urlDecoder
* @param CustomerUrl $customerUrl
* @param ResultFactory $resultFactory
* @param CookieMetadataFactory $cookieMetadataFactory
* @param HostChecker|null $hostChecker
*/
public function __construct(
Expand All @@ -99,6 +110,7 @@ public function __construct(
DecoderInterface $urlDecoder,
CustomerUrl $customerUrl,
ResultFactory $resultFactory,
CookieMetadataFactory $cookieMetadataFactory,
HostChecker $hostChecker = null
) {
$this->request = $request;
Expand All @@ -108,6 +120,7 @@ public function __construct(
$this->url = $url;
$this->urlDecoder = $urlDecoder;
$this->customerUrl = $customerUrl;
$this->cookieMetadataFactory = $cookieMetadataFactory;
$this->resultFactory = $resultFactory;
$this->hostChecker = $hostChecker ?: ObjectManager::getInstance()->get(HostChecker::class);
}
Expand Down Expand Up @@ -238,7 +251,8 @@ private function applyRedirect($url)
/**
* Get Cookie manager. For release backward compatibility.
*
* @deprecated 100.0.10
* @deprecated 100.0.10 This is legacy method to pass login_redirect cookie
* @see Magento/Checkout/view/frontend/web/js/sidebar.js
* @return CookieManagerInterface
*/
protected function getCookieManager()
Expand All @@ -252,7 +266,8 @@ protected function getCookieManager()
/**
* Set cookie manager. For unit tests.
*
* @deprecated 100.0.10
* @deprecated 100.0.10 This is legacy method to pass login_redirect cookie
* @see Magento/Checkout/view/frontend/web/js/sidebar.js
* @param object $value
* @return void
*/
Expand All @@ -264,6 +279,8 @@ public function setCookieManager($value)
/**
* Get redirect route from cookie for case of successful login/registration
*
* @deprecated 100.0.10 This is legacy method to pass login_redirect cookie
* @see Magento/Checkout/view/frontend/web/js/sidebar.js
* @return null|string
*/
public function getRedirectCookie()
Expand All @@ -274,21 +291,36 @@ public function getRedirectCookie()
/**
* Save redirect route to cookie for case of successful login/registration
*
* @deprecated 100.0.10 This is legacy method to pass login_redirect cookie
* @see Magento/Checkout/view/frontend/web/js/sidebar.js
* @param string $route
* @return void
*/
public function setRedirectCookie($route)
{
$this->getCookieManager()->setPublicCookie(self::LOGIN_REDIRECT_URL, $route);
$this->getCookieManager()->setPublicCookie(
self::LOGIN_REDIRECT_URL,
$route,
$this->cookieMetadataFactory->createPublicCookieMetadata()
->setHttpOnly(true)
->setDuration(3600)
->setPath($this->storeManager->getStore()->getStorePath())
);
}

/**
* Clear cookie with requested route
*
* @deprecated 100.0.10 This is legacy method to pass login_redirect cookie
* @see Magento/Checkout/view/frontend/web/js/sidebar.js
* @return void
*/
public function clearRedirectCookie()
{
$this->getCookieManager()->deleteCookie(self::LOGIN_REDIRECT_URL);
$this->getCookieManager()->deleteCookie(
self::LOGIN_REDIRECT_URL,
$this->cookieMetadataFactory->createPublicCookieMetadata()
->setPath($this->storeManager->getStore()->getStorePath())
);
}
}
Loading

0 comments on commit 0d910ea

Please sign in to comment.