Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion app/code/Magento/Integration/Model/CustomerTokenService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -48,24 +49,33 @@ class CustomerTokenService implements \Magento\Integration\Api\CustomerTokenServ
*/
private $requestThrottler;

/**
* @var Magento\Framework\Event\ManagerInterface
*/
private $eventManager;

/**
* Initialize service
*
* @param TokenModelFactory $tokenModelFactory
* @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
CredentialsValidator $validatorHelper,
ManagerInterface $eventManager = null
) {
$this->tokenModelFactory = $tokenModelFactory;
$this->accountManagement = $accountManagement;
$this->tokenModelCollectionFactory = $tokenModelCollectionFactory;
$this->validatorHelper = $validatorHelper;
$this->eventManager = $eventManager ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(ManagerInterface::class);
}

/**
Expand All @@ -83,6 +93,7 @@ public function createCustomerAccessToken($username, $password)
__('You did not sign in correctly or your account is temporarily disabled.')
);
}
$this->eventManager->dispatch('customer_login', ['customer' => $customerDataObject]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this event be thrown in case when token used instead of when it is acquired?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@paliarush Actually such an event should be thrown once a customer is logged id that is the case

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@slavvka Just to confirm I was on right track or should I use a @paliarush suggestion or not?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@prakashpatel07 after discussing this, I think current implementation is good. Thanks

$this->getRequestThrottler()->resetAuthenticationFailuresCount($username, RequestThrottler::USER_TYPE_CUSTOMER);
return $this->tokenModelFactory->create()->createCustomerToken($customerDataObject->getId())->getToken();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
);
}

Expand Down
12 changes: 12 additions & 0 deletions app/code/Magento/Integration/etc/webapi_rest/events.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="customer_login">
<observer name="customer_log_login" instance="Magento\Customer\Observer\LogLastLoginAtObserver" />
</event>
</config>
12 changes: 12 additions & 0 deletions app/code/Magento/Integration/etc/webapi_soap/events.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="customer_login">
<observer name="customer_log_login" instance="Magento\Customer\Observer\LogLastLoginAtObserver" />
</event>
</config>