Skip to content

Proposed code change to patch #34863 #34890

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions app/code/Magento/Theme/Controller/Result/MessagePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Magento\Framework\Translate\Inline\ParserInterface;
use Magento\Framework\Translate\InlineInterface;
use Magento\Framework\Session\Config\ConfigInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;

/**
* Plugin for putting messages to cookies
Expand Down Expand Up @@ -59,6 +60,11 @@ class MessagePlugin
*/
protected $sessionConfig;

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* @param \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager
* @param \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory
Expand All @@ -67,6 +73,7 @@ class MessagePlugin
* @param \Magento\Framework\Serialize\Serializer\Json $serializer
* @param InlineInterface $inlineTranslate
* @param ConfigInterface $sessionConfig
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(
\Magento\Framework\Stdlib\CookieManagerInterface $cookieManager,
Expand All @@ -75,7 +82,8 @@ public function __construct(
\Magento\Framework\View\Element\Message\InterpretationStrategyInterface $interpretationStrategy,
\Magento\Framework\Serialize\Serializer\Json $serializer,
InlineInterface $inlineTranslate,
ConfigInterface $sessionConfig
ConfigInterface $sessionConfig,
ScopeConfigInterface $scopeConfig
) {
$this->cookieManager = $cookieManager;
$this->cookieMetadataFactory = $cookieMetadataFactory;
Expand All @@ -84,6 +92,7 @@ public function __construct(
$this->interpretationStrategy = $interpretationStrategy;
$this->inlineTranslate = $inlineTranslate;
$this->sessionConfig = $sessionConfig;
$this->scopeConfig = $scopeConfig;
}

/**
Expand Down Expand Up @@ -171,11 +180,36 @@ private function setCookie(array $messages)
}

$publicCookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata();
$publicCookieMetadata->setDurationOneYear();
$publicCookieMetadata->setPath($this->sessionConfig->getCookiePath());

if( $configLifetime = $this->scopeConfig->getValue(
'web/cookie/cookie_lifetime',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
) ) {
$publicCookieMetadata->setDuration($configLifetime);
} else {
$publicCookieMetadata->setDurationOneYear();
}

if( $configPath = $this->scopeConfig->getValue(
'web/cookie/cookie_path',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
) ) {
$publicCookieMetadata->setPath($configPath);
} else {
$publicCookieMetadata->setPath($this->sessionConfig->getCookiePath());
}

$publicCookieMetadata->setHttpOnly(false);

$publicCookieMetadata->setSameSite('Strict');

if( $configDomain = $this->scopeConfig->getValue(
'web/cookie/cookie_domain',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
) ) {
$publicCookieMetadata->setDomain($configDomain);
}

$this->cookieManager->setPublicCookie(
self::MESSAGES_COOKIES_NAME,
$this->serializer->serialize($messages),
Expand Down
41 changes: 41 additions & 0 deletions app/code/Magento/Theme/Helper/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Magento\Theme\Helper;

class Config extends \Magento\Framework\App\Helper\AbstractHelper
{

/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $_storeManager;

/**
* @param \Magento\Framework\App\Helper\Context $context
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
*/
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Store\Model\StoreManagerInterface $storeManager
) {
parent::__construct($context);
$this->_storeManager = $storeManager;
}

/**
* @param string $path
* @param null|int $store
* @return null|string
*/
public function getConfig($path, $store = null)
{
$store = $this->_storeManager->getStore($store);
$result = $this->scopeConfig->getValue(
$path,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$store
);
return $result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* See COPYING.txt for license details.
*/
?>
<?php $helperConfig = $this->helper('Magento\Theme\Helper\Config'); ?>
<div data-bind="scope: 'messages'">
<!-- ko if: cookieMessages && cookieMessages.length > 0 -->
<div aria-atomic="true" role="alert" data-bind="foreach: { data: cookieMessages, as: 'message' }" class="messages">
Expand Down Expand Up @@ -35,7 +36,8 @@
"Magento_Ui/js/core/app": {
"components": {
"messages": {
"component": "Magento_Theme/js/view/messages"
"component": "Magento_Theme/js/view/messages",
"cookieDomain": "<?= $helperConfig->getConfig( 'web/cookie/cookie_domain' ) ?>"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ define([

$.mage.cookies.set('mage-messages', '', {
samesite: 'strict',
domain: ''
domain: this.cookieDomain
});
},

Expand Down