Skip to content

Commit 1121148

Browse files
author
Momotenko,Natalia(nmomotenko)
committed
Merge pull request #625 from magento-south/MAGETWO-52117
[South] Bug Fixes
2 parents 8a1f653 + 6f9b9ce commit 1121148

File tree

12 files changed

+656
-2
lines changed

12 files changed

+656
-2
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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\Api\CustomerRepositoryInterface;
11+
use Magento\Framework\App\Action\Context;
12+
use Magento\Customer\Model\Session;
13+
use Magento\Framework\Json\Helper\Data;
14+
15+
class UpdateSession extends AbstractAccount
16+
{
17+
/**
18+
* @var NotificationStorage
19+
*/
20+
private $notificationStorage;
21+
22+
/**
23+
* @var CustomerRepositoryInterface
24+
*/
25+
private $customerRepository;
26+
27+
/**
28+
* @var Session
29+
*/
30+
private $session;
31+
32+
/**
33+
* @var Data $helper
34+
*/
35+
private $jsonHelper;
36+
37+
/**
38+
* @param Context $context
39+
* @param NotificationStorage $notificationStorage
40+
* @param CustomerRepositoryInterface $customerRepository
41+
* @param Session $customerSession
42+
* @param Data $jsonHelper
43+
*/
44+
public function __construct(
45+
Context $context,
46+
NotificationStorage $notificationStorage,
47+
CustomerRepositoryInterface $customerRepository,
48+
Session $customerSession,
49+
Data $jsonHelper
50+
) {
51+
parent::__construct($context);
52+
$this->notificationStorage = $notificationStorage;
53+
$this->customerRepository = $customerRepository;
54+
$this->session = $customerSession;
55+
$this->jsonHelper = $jsonHelper;
56+
}
57+
58+
/**
59+
* @return void
60+
*/
61+
public function execute()
62+
{
63+
$customerData = $this->jsonHelper->jsonDecode($this->getRequest()->getContent());
64+
if (isset($customerData['customer_id'])
65+
&& $this->notificationStorage->isExists(
66+
NotificationStorage::UPDATE_CUSTOMER_SESSION,
67+
$customerData['customer_id']
68+
)
69+
) {
70+
$customer = $this->customerRepository->getById($customerData['customer_id']);
71+
$this->session->setCustomerData($customer);
72+
$this->session->setCustomerGroupId($customer->getGroupId());
73+
$this->session->regenerateId();
74+
$this->notificationStorage->remove(NotificationStorage::UPDATE_CUSTOMER_SESSION, $customer->getId());
75+
}
76+
}
77+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
/**
9+
* System / Cache Management / Cache type "Customer Notification"
10+
*/
11+
class Notification extends \Magento\Framework\Cache\Frontend\Decorator\TagScope
12+
{
13+
/**
14+
* Cache type code unique among all cache types
15+
*/
16+
const TYPE_IDENTIFIER = 'customer_notification';
17+
18+
/**
19+
* Cache tag used to distinguish the cache type from all other cache
20+
*/
21+
const CACHE_TAG = 'CUSTOMER_NOTIFICATION';
22+
23+
/**
24+
* @param \Magento\Framework\App\Cache\Type\FrontendPool $cacheFrontendPool
25+
*/
26+
public function __construct(\Magento\Framework\App\Cache\Type\FrontendPool $cacheFrontendPool)
27+
{
28+
parent::__construct($cacheFrontendPool->get(self::TYPE_IDENTIFIER), self::CACHE_TAG);
29+
}
30+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
$this->cache->save(
37+
serialize([
38+
'customer_id' => $customerId,
39+
'notification_type' => $notificationType
40+
]),
41+
$this->getCacheKey($notificationType, $customerId)
42+
);
43+
}
44+
45+
/**
46+
* Check whether notification is exists in cache
47+
*
48+
* @param string $notificationType
49+
* @param string $customerId
50+
* @return bool
51+
*/
52+
public function isExists($notificationType, $customerId)
53+
{
54+
return $this->cache->test($this->getCacheKey($notificationType, $customerId));
55+
}
56+
57+
/**
58+
* Remove notification from cache
59+
*
60+
* @param string $notificationType
61+
* @param string $customerId
62+
* @return void
63+
*/
64+
public function remove($notificationType, $customerId)
65+
{
66+
$this->cache->remove($this->getCacheKey($notificationType, $customerId));
67+
}
68+
69+
/**
70+
* Retrieve cache key
71+
*
72+
* @param string $notificationType
73+
* @param string $customerId
74+
* @return string
75+
*/
76+
private function getCacheKey($notificationType, $customerId)
77+
{
78+
return 'notification_' . $notificationType . '_' . $customerId;
79+
}
80+
}
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+
* CustomerNotification constructor.
49+
*
50+
* @param Session $session
51+
* @param NotificationStorage $notificationStorage
52+
* @param CookieManagerInterface $cookieManager
53+
* @param CookieMetadataFactory $cookieMetadataFactory
54+
* @param State $state
55+
*/
56+
public function __construct(
57+
Session $session,
58+
NotificationStorage $notificationStorage,
59+
CookieManagerInterface $cookieManager,
60+
CookieMetadataFactory $cookieMetadataFactory,
61+
State $state
62+
) {
63+
$this->session = $session;
64+
$this->notificationStorage = $notificationStorage;
65+
$this->cookieManager = $cookieManager;
66+
$this->cookieMetadataFactory = $cookieMetadataFactory;
67+
$this->state = $state;
68+
}
69+
70+
/**
71+
* @param AbstractAction $subject
72+
* @param RequestInterface $request
73+
* @return void
74+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
75+
*/
76+
public function beforeDispatch(AbstractAction $subject, RequestInterface $request)
77+
{
78+
if ($this->state->getAreaCode() == Area::AREA_FRONTEND
79+
&& $this->notificationStorage->isExists(
80+
NotificationStorage::UPDATE_CUSTOMER_SESSION,
81+
$this->session->getCustomerId()
82+
)
83+
) {
84+
$publicCookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata();
85+
$publicCookieMetadata->setDurationOneYear();
86+
$publicCookieMetadata->setPath('/');
87+
$publicCookieMetadata->setHttpOnly(false);
88+
$this->cookieManager->setPublicCookie(
89+
NotificationStorage::UPDATE_CUSTOMER_SESSION,
90+
$this->session->getCustomerId(),
91+
$publicCookieMetadata
92+
);
93+
94+
$cookieMetadata = $this->cookieMetadataFactory->createSensitiveCookieMetadata()->setPath('/');
95+
$this->cookieManager->deleteCookie(Http::COOKIE_VARY_STRING, $cookieMetadata);
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->getId()
199+
);
176200
return parent::_afterSave($customer);
177201
}
178202

0 commit comments

Comments
 (0)