From 3e5a08c5aeba3cbf260b87028efaa611237ecd4c Mon Sep 17 00:00:00 2001 From: prakashpatel07 Date: Sat, 8 Sep 2018 09:46:18 +0000 Subject: [PATCH 1/5] Fixed Last Logged-in date when customer authenticate via REST API. --- .../Integration/Model/CustomerTokenService.php | 16 +++++++++++++--- .../Integration/etc/webapi_rest/events.xml | 12 ++++++++++++ .../Integration/etc/webapi_soap/events.xml | 12 ++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 app/code/Magento/Integration/etc/webapi_rest/events.xml create mode 100644 app/code/Magento/Integration/etc/webapi_soap/events.xml diff --git a/app/code/Magento/Integration/Model/CustomerTokenService.php b/app/code/Magento/Integration/Model/CustomerTokenService.php index 3c245804a9f6..0bb682619477 100644 --- a/app/code/Magento/Integration/Model/CustomerTokenService.php +++ b/app/code/Magento/Integration/Model/CustomerTokenService.php @@ -14,6 +14,7 @@ use Magento\Integration\Model\ResourceModel\Oauth\Token\CollectionFactory as TokenCollectionFactory; use Magento\Integration\Model\Oauth\Token\RequestThrottler; use Magento\Framework\Exception\AuthenticationException; +use Magento\Framework\Event\ManagerInterface; class CustomerTokenService implements \Magento\Integration\Api\CustomerTokenServiceInterface { @@ -48,6 +49,11 @@ class CustomerTokenService implements \Magento\Integration\Api\CustomerTokenServ */ private $requestThrottler; + /** + * @var Magento\Framework\Event\ManagerInterface + */ + private $eventManager; + /** * Initialize service * @@ -55,16 +61,19 @@ class CustomerTokenService implements \Magento\Integration\Api\CustomerTokenServ * @param AccountManagementInterface $accountManagement * @param TokenCollectionFactory $tokenModelCollectionFactory * @param \Magento\Integration\Model\CredentialsValidator $validatorHelper + * @param \Magento\Framework\Event\ManagerInterface $eventManager */ public function __construct( TokenModelFactory $tokenModelFactory, AccountManagementInterface $accountManagement, - TokenCollectionFactory $tokenModelCollectionFactory, - CredentialsValidator $validatorHelper + TokenCollectionFactory $tokenModelCollectionFactory, + CredentialsValidator $validatorHelper, + ManagerInterface $eventManager ) { $this->tokenModelFactory = $tokenModelFactory; $this->accountManagement = $accountManagement; $this->tokenModelCollectionFactory = $tokenModelCollectionFactory; + $this->eventManager = $eventManager; $this->validatorHelper = $validatorHelper; } @@ -85,7 +94,8 @@ public function createCustomerAccessToken($username, $password) . 'Please wait and try again later.' ) ); - } + } + $this->eventManager->dispatch('customer_login', ['customer' => $customerDataObject]); $this->getRequestThrottler()->resetAuthenticationFailuresCount($username, RequestThrottler::USER_TYPE_CUSTOMER); return $this->tokenModelFactory->create()->createCustomerToken($customerDataObject->getId())->getToken(); } diff --git a/app/code/Magento/Integration/etc/webapi_rest/events.xml b/app/code/Magento/Integration/etc/webapi_rest/events.xml new file mode 100644 index 000000000000..e97869873427 --- /dev/null +++ b/app/code/Magento/Integration/etc/webapi_rest/events.xml @@ -0,0 +1,12 @@ + + + + + + + diff --git a/app/code/Magento/Integration/etc/webapi_soap/events.xml b/app/code/Magento/Integration/etc/webapi_soap/events.xml new file mode 100644 index 000000000000..e97869873427 --- /dev/null +++ b/app/code/Magento/Integration/etc/webapi_soap/events.xml @@ -0,0 +1,12 @@ + + + + + + + From 3b28c8ca5836d654f64ade40eeb57600e5fa6ada Mon Sep 17 00:00:00 2001 From: prakashpatel07 Date: Thu, 13 Sep 2018 10:16:29 +0000 Subject: [PATCH 2/5] Add unit test coverage for this changes. --- .../Magento/Integration/Model/CustomerTokenService.php | 4 ++-- .../Test/Unit/Model/CustomerTokenServiceTest.php | 8 +++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Integration/Model/CustomerTokenService.php b/app/code/Magento/Integration/Model/CustomerTokenService.php index 0bb682619477..7347390b301a 100644 --- a/app/code/Magento/Integration/Model/CustomerTokenService.php +++ b/app/code/Magento/Integration/Model/CustomerTokenService.php @@ -66,15 +66,15 @@ class CustomerTokenService implements \Magento\Integration\Api\CustomerTokenServ public function __construct( TokenModelFactory $tokenModelFactory, AccountManagementInterface $accountManagement, - TokenCollectionFactory $tokenModelCollectionFactory, + TokenCollectionFactory $tokenModelCollectionFactory, CredentialsValidator $validatorHelper, ManagerInterface $eventManager ) { $this->tokenModelFactory = $tokenModelFactory; $this->accountManagement = $accountManagement; $this->tokenModelCollectionFactory = $tokenModelCollectionFactory; - $this->eventManager = $eventManager; $this->validatorHelper = $validatorHelper; + $this->eventManager = $eventManager; } /** diff --git a/app/code/Magento/Integration/Test/Unit/Model/CustomerTokenServiceTest.php b/app/code/Magento/Integration/Test/Unit/Model/CustomerTokenServiceTest.php index 1a7c81934329..4aac05808b61 100644 --- a/app/code/Magento/Integration/Test/Unit/Model/CustomerTokenServiceTest.php +++ b/app/code/Magento/Integration/Test/Unit/Model/CustomerTokenServiceTest.php @@ -32,6 +32,9 @@ class CustomerTokenServiceTest extends \PHPUnit\Framework\TestCase /** @var \Magento\Integration\Model\Oauth\Token|\PHPUnit_Framework_MockObject_MockObject */ private $_tokenMock; + /** @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject */ + protected $manager; + protected function setUp() { $this->_tokenFactoryMock = $this->getMockBuilder(\Magento\Integration\Model\Oauth\TokenFactory::class) @@ -67,11 +70,14 @@ protected function setUp() \Magento\Integration\Model\CredentialsValidator::class )->disableOriginalConstructor()->getMock(); + $this->manager = $this->createMock(\Magento\Framework\Event\ManagerInterface::class); + $this->_tokenService = new \Magento\Integration\Model\CustomerTokenService( $this->_tokenFactoryMock, $this->_accountManagementMock, $this->_tokenModelCollectionFactoryMock, - $this->validatorHelperMock + $this->validatorHelperMock, + $this->manager ); } From f6b77003c673a0304922f9ab4b080fa08213b91c Mon Sep 17 00:00:00 2001 From: prakashpatel07 Date: Thu, 13 Sep 2018 11:31:45 +0000 Subject: [PATCH 3/5] Removed white spaces from code. --- app/code/Magento/Integration/Model/CustomerTokenService.php | 2 +- .../Integration/Test/Unit/Model/CustomerTokenServiceTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Integration/Model/CustomerTokenService.php b/app/code/Magento/Integration/Model/CustomerTokenService.php index 7347390b301a..175aefa74b48 100644 --- a/app/code/Magento/Integration/Model/CustomerTokenService.php +++ b/app/code/Magento/Integration/Model/CustomerTokenService.php @@ -94,7 +94,7 @@ public function createCustomerAccessToken($username, $password) . 'Please wait and try again later.' ) ); - } + } $this->eventManager->dispatch('customer_login', ['customer' => $customerDataObject]); $this->getRequestThrottler()->resetAuthenticationFailuresCount($username, RequestThrottler::USER_TYPE_CUSTOMER); return $this->tokenModelFactory->create()->createCustomerToken($customerDataObject->getId())->getToken(); diff --git a/app/code/Magento/Integration/Test/Unit/Model/CustomerTokenServiceTest.php b/app/code/Magento/Integration/Test/Unit/Model/CustomerTokenServiceTest.php index 4aac05808b61..1bc7d4247080 100644 --- a/app/code/Magento/Integration/Test/Unit/Model/CustomerTokenServiceTest.php +++ b/app/code/Magento/Integration/Test/Unit/Model/CustomerTokenServiceTest.php @@ -71,7 +71,7 @@ protected function setUp() )->disableOriginalConstructor()->getMock(); $this->manager = $this->createMock(\Magento\Framework\Event\ManagerInterface::class); - + $this->_tokenService = new \Magento\Integration\Model\CustomerTokenService( $this->_tokenFactoryMock, $this->_accountManagementMock, From d087319b12646df8e22e8ca96bbc50d38732a57b Mon Sep 17 00:00:00 2001 From: prakashpatel07 Date: Thu, 4 Oct 2018 18:02:24 +0000 Subject: [PATCH 4/5] Add backward compatible into code --- app/code/Magento/Integration/Model/CustomerTokenService.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Integration/Model/CustomerTokenService.php b/app/code/Magento/Integration/Model/CustomerTokenService.php index 175aefa74b48..cab9b5ec8799 100644 --- a/app/code/Magento/Integration/Model/CustomerTokenService.php +++ b/app/code/Magento/Integration/Model/CustomerTokenService.php @@ -68,13 +68,13 @@ public function __construct( AccountManagementInterface $accountManagement, TokenCollectionFactory $tokenModelCollectionFactory, CredentialsValidator $validatorHelper, - ManagerInterface $eventManager + ManagerInterface $eventManager = null ) { $this->tokenModelFactory = $tokenModelFactory; $this->accountManagement = $accountManagement; $this->tokenModelCollectionFactory = $tokenModelCollectionFactory; $this->validatorHelper = $validatorHelper; - $this->eventManager = $eventManager; + $this->eventManager = $eventManager ?: \Magento\Framework\App\ObjectManager::getInstance()->get(ManagerInterface::class); } /** From 3c9b40a22bcd9cc8e66ea9e75f63ac332b04ece6 Mon Sep 17 00:00:00 2001 From: prakashpatel07 Date: Fri, 5 Oct 2018 07:44:30 +0000 Subject: [PATCH 5/5] Fixed Code Sniffer issue. --- app/code/Magento/Integration/Model/CustomerTokenService.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Integration/Model/CustomerTokenService.php b/app/code/Magento/Integration/Model/CustomerTokenService.php index cab9b5ec8799..cc717a2b3ec0 100644 --- a/app/code/Magento/Integration/Model/CustomerTokenService.php +++ b/app/code/Magento/Integration/Model/CustomerTokenService.php @@ -74,7 +74,8 @@ public function __construct( $this->accountManagement = $accountManagement; $this->tokenModelCollectionFactory = $tokenModelCollectionFactory; $this->validatorHelper = $validatorHelper; - $this->eventManager = $eventManager ?: \Magento\Framework\App\ObjectManager::getInstance()->get(ManagerInterface::class); + $this->eventManager = $eventManager ?: \Magento\Framework\App\ObjectManager::getInstance() + ->get(ManagerInterface::class); } /**