Skip to content

Commit

Permalink
Merge branch '2.4-develop' into 2.4-gl-graphql-v1
Browse files Browse the repository at this point in the history
  • Loading branch information
sumesh-GL authored Oct 25, 2023
2 parents 224b79f + 0c1f257 commit bac4885
Show file tree
Hide file tree
Showing 14 changed files with 643 additions and 595 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<!--Login to Admin Panel-->
<actionGroup ref="AdminLoginActionGroup" stepKey="logInAsAdmin"/>
<!-- Create tax rate for CA -->
<createData entity="US_CA_Rate_1" stepKey="createTaxRateCA"/>
<createData entity="TaxRateCalifornia" stepKey="createTaxRateCA"/>
<!-- Create tax rate for TX -->
<createData entity="ThirdTaxRateTexas" stepKey="createTaxRateTX"/>
<!-- Create Tax Rules -->
Expand Down
57 changes: 11 additions & 46 deletions app/code/Magento/Customer/Model/AccountManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Magento\Customer\Api\Data\ValidationResultsInterfaceFactory;
use Magento\Customer\Api\SessionCleanerInterface;
use Magento\Customer\Helper\View as CustomerViewHelper;
use Magento\Customer\Model\AccountManagement\Authenticate;
use Magento\Customer\Model\Config\Share as ConfigShare;
use Magento\Customer\Model\Customer as CustomerModel;
use Magento\Customer\Model\Customer\CredentialsValidator;
Expand Down Expand Up @@ -400,6 +401,11 @@ class AccountManagement implements AccountManagementInterface
*/
private CustomerLogger $customerLogger;

/**
* @var Authenticate
*/
private Authenticate $authenticate;

/**
* @param CustomerFactory $customerFactory
* @param ManagerInterface $eventManager
Expand Down Expand Up @@ -439,6 +445,7 @@ class AccountManagement implements AccountManagementInterface
* @param AuthenticationInterface|null $authentication
* @param Backend|null $eavValidator
* @param CustomerLogger|null $customerLogger
* @param Authenticate|null $authenticate
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
* @SuppressWarnings(PHPMD.NPathComplexity)
Expand Down Expand Up @@ -483,7 +490,8 @@ public function __construct(
AuthorizationInterface $authorization = null,
AuthenticationInterface $authentication = null,
Backend $eavValidator = null,
?CustomerLogger $customerLogger = null
CustomerLogger $customerLogger = null,
Authenticate $authenticate = null
) {
$this->customerFactory = $customerFactory;
$this->eventManager = $eventManager;
Expand Down Expand Up @@ -527,6 +535,7 @@ public function __construct(
$this->authentication = $authentication ?? $objectManager->get(AuthenticationInterface::class);
$this->eavValidator = $eavValidator ?? $objectManager->get(Backend::class);
$this->customerLogger = $customerLogger ?? $objectManager->get(CustomerLogger::class);
$this->authenticate = $authenticate ?? $objectManager->get(Authenticate::class);
}

/**
Expand Down Expand Up @@ -620,51 +629,7 @@ private function activateCustomer($customer, $confirmationKey)
*/
public function authenticate($username, $password)
{
try {
$customer = $this->customerRepository->get($username);
} catch (NoSuchEntityException $e) {
throw new InvalidEmailOrPasswordException(__('Invalid login or password.'));
}

$customerId = $customer->getId();
if ($this->authentication->isLocked($customerId)) {
throw new UserLockedException(__('The account is locked.'));
}
try {
$this->authentication->authenticate($customerId, $password);
} catch (InvalidEmailOrPasswordException $e) {
throw new InvalidEmailOrPasswordException(__('Invalid login or password.'));
}

if ($customer->getConfirmation()
&& ($this->isConfirmationRequired($customer) || $this->isEmailChangedConfirmationRequired($customer))) {
throw new EmailNotConfirmedException(__("This account isn't confirmed. Verify and try again."));
}

$customerModel = $this->customerFactory->create()->updateData($customer);
$this->eventManager->dispatch(
'customer_customer_authenticated',
['model' => $customerModel, 'password' => $password]
);

$this->eventManager->dispatch('customer_data_object_login', ['customer' => $customer]);

return $customer;
}

/**
* Checks if account confirmation is required if the email address has been changed
*
* @param CustomerInterface $customer
* @return bool
*/
private function isEmailChangedConfirmationRequired(CustomerInterface $customer): bool
{
return $this->accountConfirmation->isEmailChangedConfirmationRequired(
(int)$customer->getWebsiteId(),
(int)$customer->getId(),
$customer->getEmail()
);
return $this->authenticate->execute((string) $username, (string) $password);
}

/**
Expand Down
157 changes: 157 additions & 0 deletions app/code/Magento/Customer/Model/AccountManagement/Authenticate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php
/************************************************************************
*
* Copyright 2023 Adobe
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe and its suppliers, if any. The intellectual
* and technical concepts contained herein are proprietary to Adobe
* and its suppliers and are protected by all applicable intellectual
* property laws, including trade secret and copyright laws.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe.
* ************************************************************************
*/
declare(strict_types=1);

namespace Magento\Customer\Model\AccountManagement;

use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Customer\Model\AccountConfirmation;
use Magento\Customer\Model\AuthenticationInterface;
use Magento\Customer\Model\CustomerFactory;
use Magento\Customer\Model\ResourceModel\CustomerRepository;
use Magento\Framework\Event\ManagerInterface;
use Magento\Framework\Exception\EmailNotConfirmedException;
use Magento\Framework\Exception\InvalidEmailOrPasswordException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\State\UserLockedException;

/**
* Authenticate customer
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Authenticate
{
/**
* @var CustomerRepository
*/
private CustomerRepository $customerRepository;

/**
* @var CustomerFactory
*/
private CustomerFactory $customerFactory;

/**
* @var AuthenticationInterface
*/
private AuthenticationInterface $authentication;

/**
* @var AccountConfirmation
*/
private AccountConfirmation $accountConfirmation;

/**
* @var ManagerInterface
*/
private ManagerInterface $eventManager;

/**
* @param CustomerRepository $customerRepository
* @param CustomerFactory $customerFactory
* @param AuthenticationInterface $authentication
* @param AccountConfirmation $accountConfirmation
* @param ManagerInterface $eventManager
*/
public function __construct(
CustomerRepository $customerRepository,
CustomerFactory $customerFactory,
AuthenticationInterface $authentication,
AccountConfirmation $accountConfirmation,
ManagerInterface $eventManager
) {
$this->customerRepository = $customerRepository;
$this->customerFactory = $customerFactory;
$this->authentication = $authentication;
$this->accountConfirmation = $accountConfirmation;
$this->eventManager = $eventManager;
}

/**
* Authenticate a customer by username and password
*
* @param string $email
* @param string $password
* @return CustomerInterface
* @throws LocalizedException
*/
public function execute(string $email, string $password): CustomerInterface
{
try {
$customer = $this->customerRepository->get($email);
} catch (NoSuchEntityException $exception) {
throw new InvalidEmailOrPasswordException(__('Invalid login or password.'));
}

$customerId = $customer->getId();
if ($this->authentication->isLocked($customerId)) {
throw new UserLockedException(__('The account is locked.'));
}
try {
$this->authentication->authenticate($customerId, $password);
} catch (InvalidEmailOrPasswordException $exception) {
throw new InvalidEmailOrPasswordException(__('Invalid login or password.'));
}

if ($customer->getConfirmation()
&& ($this->isConfirmationRequired($customer) || $this->isEmailChangedConfirmationRequired($customer))) {
throw new EmailNotConfirmedException(__('This account isn\'t confirmed. Verify and try again.'));
}

$customerModel = $this->customerFactory->create()->updateData($customer);
$this->eventManager->dispatch(
'customer_customer_authenticated',
['model' => $customerModel, 'password' => $password]
);

$this->eventManager->dispatch('customer_data_object_login', ['customer' => $customer]);

return $customer;
}

/**
* Check if accounts confirmation is required in config
*
* @param CustomerInterface $customer
* @return bool
*/
private function isConfirmationRequired($customer)
{
return $this->accountConfirmation->isConfirmationRequired(
$customer->getWebsiteId(),
$customer->getId(),
$customer->getEmail()
);
}

/**
* Checks if account confirmation is required if the email address has been changed
*
* @param CustomerInterface $customer
* @return bool
*/
private function isEmailChangedConfirmationRequired(CustomerInterface $customer): bool
{
return $this->accountConfirmation->isEmailChangedConfirmationRequired(
(int)$customer->getWebsiteId(),
(int)$customer->getId(),
$customer->getEmail()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2072,67 +2072,6 @@ public function testChangePasswordException(): void
$this->accountManagement->changePassword($email, $currentPassword, $newPassword);
}

/**
* @return void
* @throws LocalizedException
*/
public function testAuthenticate(): void
{
$username = 'login';
$password = '1234567';
$passwordHash = '1a2b3f4c';

$customerData = $this->getMockBuilder(Customer::class)
->disableOriginalConstructor()
->getMock();

$customerModel = $this->getMockBuilder(\Magento\Customer\Model\Customer::class)
->disableOriginalConstructor()
->getMock();
$customerModel->expects($this->once())
->method('updateData')
->willReturn($customerModel);

$this->customerRepository
->expects($this->once())
->method('get')
->with($username)
->willReturn($customerData);

$this->authenticationMock->expects($this->once())
->method('authenticate');

$customerSecure = $this->getMockBuilder(CustomerSecure::class)
->addMethods(['getPasswordHash'])
->disableOriginalConstructor()
->getMock();
$customerSecure->expects($this->any())
->method('getPasswordHash')
->willReturn($passwordHash);

$this->customerRegistry->expects($this->any())
->method('retrieveSecureData')
->willReturn($customerSecure);

$this->customerFactory->expects($this->once())
->method('create')
->willReturn($customerModel);

$this->manager->expects($this->exactly(2))
->method('dispatch')
->withConsecutive(
[
'customer_customer_authenticated',
['model' => $customerModel, 'password' => $password]
],
[
'customer_data_object_login', ['customer' => $customerData]
]
);

$this->assertEquals($customerData, $this->accountManagement->authenticate($username, $password));
}

/**
* @param int $isConfirmationRequired
* @param string|null $confirmation
Expand Down
45 changes: 0 additions & 45 deletions app/code/Magento/Customer/Test/Unit/Model/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,51 +250,6 @@ protected function prepareLoginDataMock(int $customerId): MockObject
return $customerDataMock;
}

/**
* @param bool $expectedResult
* @param bool $isCustomerIdValid
* @param bool $isCustomerEmulated
*
* @return void
* @dataProvider getIsLoggedInDataProvider
*/
public function testIsLoggedIn(
bool $expectedResult,
bool $isCustomerIdValid,
bool $isCustomerEmulated
): void {
$customerId = 1;
$this->_storageMock->expects($this->any())->method('getData')->with('customer_id')
->willReturn($customerId);

if ($isCustomerIdValid) {
$this->customerRepositoryMock->expects($this->once())
->method('getById')
->with($customerId);
} else {
$this->customerRepositoryMock->expects($this->once())
->method('getById')
->with($customerId)
->willThrowException(new \Exception('Customer ID is invalid.'));
}
$this->_storageMock->expects($this->any())->method('getIsCustomerEmulated')
->willReturn($isCustomerEmulated);
$this->assertEquals($expectedResult, $this->_model->isLoggedIn());
}

/**
* @return array
*/
public function getIsLoggedInDataProvider(): array
{
return [
['expectedResult' => true, 'isCustomerIdValid' => true, 'isCustomerEmulated' => false],
['expectedResult' => false, 'isCustomerIdValid' => true, 'isCustomerEmulated' => true],
['expectedResult' => false, 'isCustomerIdValid' => false, 'isCustomerEmulated' => false],
['expectedResult' => false, 'isCustomerIdValid' => false, 'isCustomerEmulated' => true]
];
}

/**
* @return void
*/
Expand Down
Loading

0 comments on commit bac4885

Please sign in to comment.