Skip to content

Commit 66fc9a3

Browse files
author
Stanislav Idolov
authored
ENGCOM-3102: #17488 Fix Authenticating a customer via REST API does not update the last logged in data #17978
2 parents 8418e99 + 2ee8b72 commit 66fc9a3

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
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

1819
class CustomerTokenService implements \Magento\Integration\Api\CustomerTokenServiceInterface
1920
{
@@ -48,24 +49,33 @@ class CustomerTokenService implements \Magento\Integration\Api\CustomerTokenServ
4849
*/
4950
private $requestThrottler;
5051

52+
/**
53+
* @var Magento\Framework\Event\ManagerInterface
54+
*/
55+
private $eventManager;
56+
5157
/**
5258
* Initialize service
5359
*
5460
* @param TokenModelFactory $tokenModelFactory
5561
* @param AccountManagementInterface $accountManagement
5662
* @param TokenCollectionFactory $tokenModelCollectionFactory
5763
* @param \Magento\Integration\Model\CredentialsValidator $validatorHelper
64+
* @param \Magento\Framework\Event\ManagerInterface $eventManager
5865
*/
5966
public function __construct(
6067
TokenModelFactory $tokenModelFactory,
6168
AccountManagementInterface $accountManagement,
6269
TokenCollectionFactory $tokenModelCollectionFactory,
63-
CredentialsValidator $validatorHelper
70+
CredentialsValidator $validatorHelper,
71+
ManagerInterface $eventManager = null
6472
) {
6573
$this->tokenModelFactory = $tokenModelFactory;
6674
$this->accountManagement = $accountManagement;
6775
$this->tokenModelCollectionFactory = $tokenModelCollectionFactory;
6876
$this->validatorHelper = $validatorHelper;
77+
$this->eventManager = $eventManager ?: \Magento\Framework\App\ObjectManager::getInstance()
78+
->get(ManagerInterface::class);
6979
}
7080

7181
/**
@@ -83,6 +93,7 @@ public function createCustomerAccessToken($username, $password)
8393
__('You did not sign in correctly or your account is temporarily disabled.')
8494
);
8595
}
96+
$this->eventManager->dispatch('customer_login', ['customer' => $customerDataObject]);
8697
$this->getRequestThrottler()->resetAuthenticationFailuresCount($username, RequestThrottler::USER_TYPE_CUSTOMER);
8798
return $this->tokenModelFactory->create()->createCustomerToken($customerDataObject->getId())->getToken();
8899
}

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)