Skip to content

Commit a73af29

Browse files
author
Oleksandr Karpenko
committed
MAGETWO-52117: Customer group is not changed for logged in customer
1 parent 1d6664d commit a73af29

File tree

6 files changed

+282
-0
lines changed

6 files changed

+282
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Customer\Controller\Account;
7+
8+
use Magento\Customer\Controller\AbstractAccount;
9+
use Magento\Customer\Model\Customer\NotificationStorage;
10+
use Magento\Customer\Model\ResourceModel\CustomerRepository;
11+
use Magento\Framework\App\Action\Context;
12+
use Magento\Customer\Model\Session;
13+
use Magento\Framework\Stdlib\CookieManagerInterface;
14+
15+
class UpdateSession extends AbstractAccount
16+
{
17+
/**
18+
* @var NotificationStorage
19+
*/
20+
private $notificationStorage;
21+
22+
/**
23+
* @var CustomerRepository
24+
*/
25+
private $customerRepository;
26+
27+
/**
28+
* @var Session
29+
*/
30+
private $session;
31+
32+
/**
33+
* @var CookieManagerInterface
34+
*/
35+
private $cookieManager;
36+
37+
/**
38+
* @param Context $context
39+
* @param NotificationStorage $notificationStorage
40+
* @param CustomerRepository $customerRepository
41+
* @param Session $customerSession
42+
*/
43+
public function __construct(
44+
Context $context,
45+
NotificationStorage $notificationStorage,
46+
CustomerRepository $customerRepository,
47+
Session $customerSession,
48+
CookieManagerInterface $cookieManager
49+
) {
50+
parent::__construct($context);
51+
$this->notificationStorage = $notificationStorage;
52+
$this->customerRepository = $customerRepository;
53+
$this->cookieManager = $cookieManager;
54+
}
55+
56+
/**
57+
* @return void
58+
*/
59+
public function execute()
60+
{
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);
65+
$this->session->setCustomerData($customer);
66+
$this->cookieManager->deleteCookie(NotificationStorage::UPDATE_CUSTOMER_SESSION);
67+
}
68+
}
69+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Customer\Model\Customer;
7+
8+
use Magento\Framework\Cache\FrontendInterface;
9+
10+
class NotificationStorage
11+
{
12+
const UPDATE_CUSTOMER_SESSION = 'update_customer_session';
13+
14+
/**
15+
* @var FrontendInterface
16+
*/
17+
private $cache;
18+
19+
/**
20+
* @param FrontendInterface $cache
21+
*/
22+
public function __construct(FrontendInterface $cache)
23+
{
24+
$this->cache = $cache;
25+
}
26+
27+
/**
28+
* Add notification in cache
29+
*
30+
* @param string $notificationType
31+
* @param string $customerId
32+
* @return void
33+
*/
34+
public function add($notificationType, $customerId)
35+
{
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+
}
45+
}
46+
47+
/**
48+
* Check whether notification is exists in cache
49+
*
50+
* @param string $notificationType
51+
* @param string $customerId
52+
* @return bool
53+
*/
54+
public function isExists($notificationType, $customerId)
55+
{
56+
return $this->cache->load($this->getCacheKey($notificationType, $customerId));
57+
}
58+
59+
/**
60+
* Remove notification from cache
61+
*
62+
* @param string $notificationType
63+
* @param string $customerId
64+
* @return void
65+
*/
66+
public function remove($notificationType, $customerId)
67+
{
68+
$this->cache->remove($this->getCacheKey($notificationType, $customerId));
69+
}
70+
71+
/**
72+
* Retrieve cache key
73+
*
74+
* @param string $notificationType
75+
* @param string $customerId
76+
* @return string
77+
*/
78+
private function getCacheKey($notificationType, $customerId)
79+
{
80+
return 'notification-' . $notificationType . '-' . $customerId;
81+
}
82+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Customer\Model\Plugin;
7+
8+
use Magento\Customer\Model\Customer\NotificationStorage;
9+
use Magento\Customer\Model\Session;
10+
use Magento\Framework\App\Action\AbstractAction;
11+
use Magento\Framework\App\Area;
12+
use Magento\Framework\App\RequestInterface;
13+
use Magento\Framework\App\Response\Http;
14+
use Magento\Framework\App\State;
15+
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
16+
use Magento\Framework\Stdlib\CookieManagerInterface;
17+
18+
class CustomerNotification
19+
{
20+
/**
21+
* @var Session
22+
*/
23+
private $session;
24+
25+
/**
26+
* @var NotificationStorage
27+
*/
28+
private $notificationStorage;
29+
30+
/**
31+
* Cookie Manager
32+
*
33+
* @var \Magento\Framework\Stdlib\CookieManagerInterface
34+
*/
35+
private $cookieManager;
36+
37+
/**
38+
* @var \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory
39+
*/
40+
private $cookieMetadataFactory;
41+
42+
/**
43+
* @var State
44+
*/
45+
private $state;
46+
47+
/**
48+
* @param Session $session
49+
* @param NotificationStorage $notificationStorage
50+
* @param CookieManagerInterface $cookieManager
51+
* @param CookieMetadataFactory $cookieMetadataFactory
52+
*/
53+
public function __construct(
54+
Session $session,
55+
NotificationStorage $notificationStorage,
56+
CookieManagerInterface $cookieManager,
57+
CookieMetadataFactory $cookieMetadataFactory,
58+
State $state
59+
) {
60+
$this->session = $session;
61+
$this->notificationStorage = $notificationStorage;
62+
$this->cookieManager = $cookieManager;
63+
$this->cookieMetadataFactory = $cookieMetadataFactory;
64+
$this->state = $state;
65+
}
66+
67+
/**
68+
* @param AbstractAction $subject
69+
* @param RequestInterface $request
70+
* @return void
71+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
72+
*/
73+
public function beforeDispatch(AbstractAction $subject, RequestInterface $request)
74+
{
75+
if (
76+
$this->state->getAreaCode() === Area::AREA_FRONTEND
77+
&& $this->notificationStorage->isExists(
78+
NotificationStorage::UPDATE_CUSTOMER_SESSION,
79+
$this->session->getCustomerId()
80+
)
81+
) {
82+
$publicCookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()
83+
->setDuration(315360000)
84+
->setPath('/')
85+
->setHttpOnly(false);
86+
$this->cookieManager->setPublicCookie(
87+
NotificationStorage::UPDATE_CUSTOMER_SESSION,
88+
'1',
89+
$publicCookieMetadata
90+
);
91+
$this->notificationStorage->remove(
92+
NotificationStorage::UPDATE_CUSTOMER_SESSION,
93+
$this->session->getCustomerId()
94+
);
95+
$this->cookieManager->deleteCookie(Http::COOKIE_VARY_STRING);
96+
}
97+
}
98+
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\Customer\Model\ResourceModel;
77

8+
use Magento\Customer\Model\Customer\NotificationStorage;
9+
use Magento\Framework\App\ObjectManager;
810
use Magento\Framework\Validator\Exception as ValidatorException;
911
use Magento\Framework\Exception\AlreadyExistsException;
1012

@@ -36,6 +38,11 @@ class Customer extends \Magento\Eav\Model\Entity\VersionControl\AbstractEntity
3638
*/
3739
protected $storeManager;
3840

41+
/**
42+
* @var NotificationStorage
43+
*/
44+
private $notificationStorage;
45+
3946
/**
4047
* @param \Magento\Eav\Model\Entity\Context $context
4148
* @param \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot $entitySnapshot
@@ -165,6 +172,19 @@ protected function _validate($customer)
165172
}
166173
}
167174

175+
/**
176+
* Retrieve notification storage
177+
*
178+
* @return NotificationStorage
179+
*/
180+
private function getNotificationStorage()
181+
{
182+
if ($this->notificationStorage === null) {
183+
$this->notificationStorage = ObjectManager::getInstance()->get(NotificationStorage::class);
184+
}
185+
return $this->notificationStorage;
186+
}
187+
168188
/**
169189
* Save customer addresses and set default addresses in attributes backend
170190
*
@@ -173,6 +193,10 @@ protected function _validate($customer)
173193
*/
174194
protected function _afterSave(\Magento\Framework\DataObject $customer)
175195
{
196+
$this->getNotificationStorage()->add(
197+
NotificationStorage::UPDATE_CUSTOMER_SESSION,
198+
$customer->getData('id')
199+
);
176200
return parent::_afterSave($customer);
177201
}
178202

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,4 +307,12 @@
307307
<type name="Magento\Customer\Api\CustomerRepositoryInterface">
308308
<plugin name="transactionWrapper" type="Magento\Customer\Model\Plugin\CustomerRepository\TransactionWrapper" sortOrder="-1"/>
309309
</type>
310+
<type name="Magento\Framework\App\Action\AbstractAction">
311+
<plugin name="customerNotification" type="Magento\Customer\Model\Plugin\CustomerNotification"/>
312+
</type>
313+
<type name="\Magento\Customer\Model\Customer\NotificationStorage">
314+
<arguments>
315+
<argument name="cache" xsi:type="object">Magento\Framework\App\Cache\Type\Config</argument>
316+
</arguments>
317+
</type>
310318
</config>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ define([
188188
init: function() {
189189
var countryData,
190190
privateContent = $.cookieStorage.get('private_content_version');
191+
console.log('update_customer_session', $.cookieStorage.get('update_customer_session'));
191192

192193
if (_.isEmpty(storage.keys())) {
193194
if (!_.isEmpty(privateContent)) {

0 commit comments

Comments
 (0)