From 9a4f260d33ea4603e34b112a4d72d934700de3de 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 947ca6f9f8821..c719179947624 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; } @@ -82,7 +91,8 @@ public function createCustomerAccessToken($username, $password) throw new AuthenticationException( __('You did not sign in correctly or your account is temporarily disabled.') ); - } + } + $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 0000000000000..e978698734277 --- /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 0000000000000..e978698734277 --- /dev/null +++ b/app/code/Magento/Integration/etc/webapi_soap/events.xml @@ -0,0 +1,12 @@ + + + + + + + From 55047c71ec782b86026450628c594ba414744c9c 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 c719179947624..03253821eebc0 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 ecd4788545c0a..55a8c1a2064bf 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 da2c92027aaf3e9ac00d161af88ba6874037ffe9 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 03253821eebc0..1433361e3a9b9 100644 --- a/app/code/Magento/Integration/Model/CustomerTokenService.php +++ b/app/code/Magento/Integration/Model/CustomerTokenService.php @@ -91,7 +91,7 @@ public function createCustomerAccessToken($username, $password) throw new AuthenticationException( __('You did not sign in correctly or your account is temporarily disabled.') ); - } + } $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 55a8c1a2064bf..170e7e42d919e 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 e8636ef98ec3b747c0773f30bd7adac6153d6bf7 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 1433361e3a9b9..296605a9315a5 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 141b0e4be95ab843a7659759e79b36654930a004 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 296605a9315a5..adacf5ebacf71 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); } /**