Skip to content

Commit b6bc3ce

Browse files
ENGCOM-3355: [Forwardport] Fix Authenticating a customer via REST API does not update the last logged in data #18973
- Merge Pull Request #18973 from prakashpatel07/magento2:2.3-develop-PR-port-17978 - Merged commits: 1. 97d5e9b 2. b888298 3. 876f50a 4. 80da4ea 5. a40eb48
2 parents 2a389b0 + a40eb48 commit b6bc3ce

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

app/code/Magento/Integration/Model/CustomerTokenService.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
use Magento\Integration\Model\ResourceModel\Oauth\Token\CollectionFactory as TokenCollectionFactory;
1515
use Magento\Integration\Model\Oauth\Token\RequestThrottler;
1616
use Magento\Framework\Exception\AuthenticationException;
17+
use Magento\Framework\Event\ManagerInterface;
1718

19+
/**
20+
* @inheritdoc
21+
*/
1822
class CustomerTokenService implements \Magento\Integration\Api\CustomerTokenServiceInterface
1923
{
2024
/**
@@ -24,6 +28,11 @@ class CustomerTokenService implements \Magento\Integration\Api\CustomerTokenServ
2428
*/
2529
private $tokenModelFactory;
2630

31+
/**
32+
* @var Magento\Framework\Event\ManagerInterface
33+
*/
34+
private $eventManager;
35+
2736
/**
2837
* Customer Account Service
2938
*
@@ -55,21 +64,25 @@ class CustomerTokenService implements \Magento\Integration\Api\CustomerTokenServ
5564
* @param AccountManagementInterface $accountManagement
5665
* @param TokenCollectionFactory $tokenModelCollectionFactory
5766
* @param \Magento\Integration\Model\CredentialsValidator $validatorHelper
67+
* @param \Magento\Framework\Event\ManagerInterface $eventManager
5868
*/
5969
public function __construct(
6070
TokenModelFactory $tokenModelFactory,
6171
AccountManagementInterface $accountManagement,
6272
TokenCollectionFactory $tokenModelCollectionFactory,
63-
CredentialsValidator $validatorHelper
73+
CredentialsValidator $validatorHelper,
74+
ManagerInterface $eventManager = null
6475
) {
6576
$this->tokenModelFactory = $tokenModelFactory;
6677
$this->accountManagement = $accountManagement;
6778
$this->tokenModelCollectionFactory = $tokenModelCollectionFactory;
6879
$this->validatorHelper = $validatorHelper;
80+
$this->eventManager = $eventManager ?: \Magento\Framework\App\ObjectManager::getInstance()
81+
->get(ManagerInterface::class);
6982
}
7083

7184
/**
72-
* {@inheritdoc}
85+
* @inheritdoc
7386
*/
7487
public function createCustomerAccessToken($username, $password)
7588
{
@@ -86,6 +99,7 @@ public function createCustomerAccessToken($username, $password)
8699
)
87100
);
88101
}
102+
$this->eventManager->dispatch('customer_login', ['customer' => $customerDataObject]);
89103
$this->getRequestThrottler()->resetAuthenticationFailuresCount($username, RequestThrottler::USER_TYPE_CUSTOMER);
90104
return $this->tokenModelFactory->create()->createCustomerToken($customerDataObject->getId())->getToken();
91105
}

app/code/Magento/Integration/Test/Unit/Model/CustomerTokenServiceTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class CustomerTokenServiceTest extends \PHPUnit\Framework\TestCase
3232
/** @var \Magento\Integration\Model\Oauth\Token|\PHPUnit_Framework_MockObject_MockObject */
3333
private $_tokenMock;
3434

35+
/** @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
36+
protected $manager;
37+
3538
protected function setUp()
3639
{
3740
$this->_tokenFactoryMock = $this->getMockBuilder(\Magento\Integration\Model\Oauth\TokenFactory::class)
@@ -67,11 +70,14 @@ protected function setUp()
6770
\Magento\Integration\Model\CredentialsValidator::class
6871
)->disableOriginalConstructor()->getMock();
6972

73+
$this->manager = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
74+
7075
$this->_tokenService = new \Magento\Integration\Model\CustomerTokenService(
7176
$this->_tokenFactoryMock,
7277
$this->_accountManagementMock,
7378
$this->_tokenModelCollectionFactoryMock,
74-
$this->validatorHelperMock
79+
$this->validatorHelperMock,
80+
$this->manager
7581
);
7682
}
7783

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. 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:Event/etc/events.xsd">
9+
<event name="customer_login">
10+
<observer name="customer_log_login" instance="Magento\Customer\Observer\LogLastLoginAtObserver" />
11+
</event>
12+
</config>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. 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:Event/etc/events.xsd">
9+
<event name="customer_login">
10+
<observer name="customer_log_login" instance="Magento\Customer\Observer\LogLastLoginAtObserver" />
11+
</event>
12+
</config>

0 commit comments

Comments
 (0)