Skip to content

Commit 9e86fa6

Browse files
committed
MAGETWO-52117: Customer group is not changed for logged in customer
1 parent ec42e11 commit 9e86fa6

File tree

9 files changed

+95
-32
lines changed

9 files changed

+95
-32
lines changed

app/code/Magento/Customer/Controller/Account/UpdateSession.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Customer\Model\ResourceModel\CustomerRepository;
1111
use Magento\Framework\App\Action\Context;
1212
use Magento\Customer\Model\Session;
13+
use Magento\Framework\Json\Helper\Data;
1314
use Magento\Framework\Stdlib\CookieManagerInterface;
1415

1516
class UpdateSession extends AbstractAccount
@@ -34,36 +35,48 @@ class UpdateSession extends AbstractAccount
3435
*/
3536
private $cookieManager;
3637

38+
/**
39+
* @var Data $helper
40+
*/
41+
private $jsonHelper;
42+
3743
/**
3844
* @param Context $context
3945
* @param NotificationStorage $notificationStorage
4046
* @param CustomerRepository $customerRepository
4147
* @param Session $customerSession
48+
* @param CookieManagerInterface $cookieManager
49+
* @param Data $jsonHelper
4250
*/
4351
public function __construct(
4452
Context $context,
4553
NotificationStorage $notificationStorage,
4654
CustomerRepository $customerRepository,
4755
Session $customerSession,
48-
CookieManagerInterface $cookieManager
56+
CookieManagerInterface $cookieManager,
57+
Data $jsonHelper
4958
) {
5059
parent::__construct($context);
5160
$this->notificationStorage = $notificationStorage;
5261
$this->customerRepository = $customerRepository;
62+
$this->session = $customerSession;
5363
$this->cookieManager = $cookieManager;
64+
$this->jsonHelper = $jsonHelper;
5465
}
5566

5667
/**
5768
* @return void
5869
*/
5970
public function execute()
6071
{
61-
$notification = $this->getRequest()->getPost('notification');
62-
$customerId = $this->getRequest()->getPost('customer_id');
63-
if ($notification && $customerId && $this->notificationStorage->isExists($notification, $customerId)) {
64-
$customer = $this->customerRepository->getById($customerId);
72+
$customerData = $this->jsonHelper->jsonDecode($this->getRequest()->getContent());
73+
$result = $this->notificationStorage->isExists(NotificationStorage::UPDATE_CUSTOMER_SESSION, $customerData['customer_id']);
74+
if (isset($customerData['customer_id']) && $result) {
75+
$customer = $this->customerRepository->getById($customerData['customer_id']);
6576
$this->session->setCustomerData($customer);
66-
$this->cookieManager->deleteCookie(NotificationStorage::UPDATE_CUSTOMER_SESSION);
77+
$this->session->setCustomerGroupId($customer->getGroupId());
78+
$this->session->regenerateId();
79+
$this->notificationStorage->remove(NotificationStorage::UPDATE_CUSTOMER_SESSION, $customerData['customer_id']);
6780
}
6881
}
6982
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Customer\Model\Cache\Type;
7+
8+
class Notification extends \Magento\Framework\Cache\Frontend\Decorator\TagScope
9+
{
10+
/**
11+
* Cache type code unique among all cache types
12+
*/
13+
const TYPE_IDENTIFIER = 'customer_notification';
14+
15+
/**
16+
* Cache tag used to distinguish the cache type from all other cache
17+
*/
18+
const CACHE_TAG = 'CUSTOMER_NOTIFICATION';
19+
20+
/**
21+
* @param \Magento\Framework\App\Cache\Type\FrontendPool $cacheFrontendPool
22+
*/
23+
public function __construct(\Magento\Framework\App\Cache\Type\FrontendPool $cacheFrontendPool)
24+
{
25+
parent::__construct($cacheFrontendPool->get(self::TYPE_IDENTIFIER), self::CACHE_TAG);
26+
}
27+
}

app/code/Magento/Customer/Model/Customer/NotificationStorage.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,13 @@ public function __construct(FrontendInterface $cache)
3333
*/
3434
public function add($notificationType, $customerId)
3535
{
36-
if (!$this->isExists($notificationType, $customerId)) {
37-
$this->cache->save(
38-
serialize([
39-
'customer_id' => $customerId,
40-
'notification_type' => $notificationType
41-
]),
42-
$this->getCacheKey($notificationType, $customerId)
43-
);
44-
}
36+
$this->cache->save(
37+
serialize([
38+
'customer_id' => $customerId,
39+
'notification_type' => $notificationType
40+
]),
41+
$this->getCacheKey($notificationType, $customerId)
42+
);
4543
}
4644

4745
/**
@@ -53,7 +51,7 @@ public function add($notificationType, $customerId)
5351
*/
5452
public function isExists($notificationType, $customerId)
5553
{
56-
return $this->cache->load($this->getCacheKey($notificationType, $customerId));
54+
return $this->cache->test($this->getCacheKey($notificationType, $customerId));
5755
}
5856

5957
/**
@@ -77,6 +75,6 @@ public function remove($notificationType, $customerId)
7775
*/
7876
private function getCacheKey($notificationType, $customerId)
7977
{
80-
return 'notification-' . $notificationType . '-' . $customerId;
78+
return 'notification_' . $notificationType . '_' . $customerId;
8179
}
8280
}

app/code/Magento/Customer/Model/Plugin/CustomerNotification.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,27 +72,24 @@ public function __construct(
7272
*/
7373
public function beforeDispatch(AbstractAction $subject, RequestInterface $request)
7474
{
75-
if (
76-
$this->state->getAreaCode() === Area::AREA_FRONTEND
75+
if ($this->state->getAreaCode() == Area::AREA_FRONTEND
7776
&& $this->notificationStorage->isExists(
7877
NotificationStorage::UPDATE_CUSTOMER_SESSION,
7978
$this->session->getCustomerId()
8079
)
8180
) {
8281
$publicCookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()
83-
->setDuration(315360000)
82+
->setDurationOneYear()
8483
->setPath('/')
8584
->setHttpOnly(false);
8685
$this->cookieManager->setPublicCookie(
8786
NotificationStorage::UPDATE_CUSTOMER_SESSION,
88-
'1',
87+
$this->session->getCustomerId(),
8988
$publicCookieMetadata
9089
);
91-
$this->notificationStorage->remove(
92-
NotificationStorage::UPDATE_CUSTOMER_SESSION,
93-
$this->session->getCustomerId()
94-
);
95-
$this->cookieManager->deleteCookie(Http::COOKIE_VARY_STRING);
90+
91+
$cookieMetadata = $this->cookieMetadataFactory->createSensitiveCookieMetadata()->setPath('/');
92+
$this->cookieManager->deleteCookie(Http::COOKIE_VARY_STRING, $cookieMetadata);
9693
}
9794
}
9895
}

app/code/Magento/Customer/Model/ResourceModel/Customer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ protected function _afterSave(\Magento\Framework\DataObject $customer)
195195
{
196196
$this->getNotificationStorage()->add(
197197
NotificationStorage::UPDATE_CUSTOMER_SESSION,
198-
$customer->getData('id')
198+
$customer->getId()
199199
);
200200
return parent::_afterSave($customer);
201201
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © 2016 Magento. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Cache/etc/cache.xsd">
9+
<type name="customer_notification" translate="label,description" instance="Magento\Customer\Model\Cache\Type\Notification">
10+
<label>Customer Notification</label>
11+
<description>Customer Notification</description>
12+
</type>
13+
</config>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,9 @@
310310
<type name="Magento\Framework\App\Action\AbstractAction">
311311
<plugin name="customerNotification" type="Magento\Customer\Model\Plugin\CustomerNotification"/>
312312
</type>
313-
<type name="\Magento\Customer\Model\Customer\NotificationStorage">
313+
<type name="Magento\Customer\Model\Customer\NotificationStorage">
314314
<arguments>
315-
<argument name="cache" xsi:type="object">Magento\Framework\App\Cache\Type\Config</argument>
315+
<argument name="cache" xsi:type="object">Magento\Customer\Model\Cache\Type\Notification</argument>
316316
</arguments>
317317
</type>
318318
</config>

app/code/Magento/Customer/view/frontend/templates/js/customer-data.phtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
'*' => ['Magento_Customer/js/customer-data' => [
1414
'sectionLoadUrl' => $block->getCustomerDataUrl('customer/section/load'),
1515
'cookieLifeTime' => $block->getCookieLifeTime(),
16+
'updateSessionUrl' => $block->getCustomerDataUrl('customer/account/updateSession'),
1617
]],
1718
]);
1819
?>

app/code/Magento/Customer/view/frontend/web/js/customer-data.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ define([
77
'underscore',
88
'ko',
99
'Magento_Customer/js/section-config',
10+
'mage/storage',
1011
'jquery/jquery-storageapi'
11-
], function ($, _, ko, sectionConfig) {
12+
], function ($, _, ko, sectionConfig, mageStorage) {
1213
'use strict';
1314

1415
var options,
@@ -187,8 +188,21 @@ define([
187188
*/
188189
init: function() {
189190
var countryData,
190-
privateContent = $.cookieStorage.get('private_content_version');
191-
console.log('update_customer_session', $.cookieStorage.get('update_customer_session'));
191+
privateContent = $.cookieStorage.get('private_content_version'),
192+
updateSession = $.cookieStorage.get('update_customer_session');
193+
if (updateSession) {
194+
mageStorage.post(
195+
options.updateSessionUrl,
196+
JSON.stringify({
197+
'customer_id': updateSession,
198+
'form_key': window.FORM_KEY
199+
})
200+
).done(
201+
function () {
202+
$.cookieStorage.setConf({path: '/', expires: -1}).set('update_customer_session', null);
203+
}
204+
);
205+
}
192206

193207
if (_.isEmpty(storage.keys())) {
194208
if (!_.isEmpty(privateContent)) {

0 commit comments

Comments
 (0)