|
| 1 | +<?php |
| 2 | +/************************************************************************ |
| 3 | + * |
| 4 | + * Copyright 2023 Adobe |
| 5 | + * All Rights Reserved. |
| 6 | + * |
| 7 | + * NOTICE: All information contained herein is, and remains |
| 8 | + * the property of Adobe and its suppliers, if any. The intellectual |
| 9 | + * and technical concepts contained herein are proprietary to Adobe |
| 10 | + * and its suppliers and are protected by all applicable intellectual |
| 11 | + * property laws, including trade secret and copyright laws. |
| 12 | + * Dissemination of this information or reproduction of this material |
| 13 | + * is strictly forbidden unless prior written permission is obtained |
| 14 | + * from Adobe. |
| 15 | + * ************************************************************************ |
| 16 | + */ |
| 17 | +declare(strict_types=1); |
| 18 | + |
| 19 | +namespace Magento\Customer\Model\AccountManagement; |
| 20 | + |
| 21 | +use Magento\Customer\Api\Data\CustomerInterface; |
| 22 | +use Magento\Customer\Api\Data\CustomerInterfaceFactory; |
| 23 | +use Magento\Customer\Model\AccountConfirmation; |
| 24 | +use Magento\Customer\Model\AuthenticationInterface; |
| 25 | +use Magento\Customer\Model\Customer; |
| 26 | +use Magento\Customer\Model\CustomerRegistry; |
| 27 | +use Magento\Framework\Api\DataObjectHelper; |
| 28 | +use Magento\Framework\Event\ManagerInterface; |
| 29 | +use Magento\Framework\Exception\EmailNotConfirmedException; |
| 30 | +use Magento\Framework\Exception\InvalidEmailOrPasswordException; |
| 31 | +use Magento\Framework\Exception\LocalizedException; |
| 32 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 33 | +use Magento\Framework\Exception\State\UserLockedException; |
| 34 | + |
| 35 | +/** |
| 36 | + * Authenticate customer |
| 37 | + */ |
| 38 | +class Authenticate |
| 39 | +{ |
| 40 | + /** |
| 41 | + * @var CustomerRegistry |
| 42 | + */ |
| 43 | + private CustomerRegistry $customerRegistry; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var DataObjectHelper |
| 47 | + */ |
| 48 | + private DataObjectHelper $dataObjectHelper; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var CustomerInterfaceFactory |
| 52 | + */ |
| 53 | + private CustomerInterfaceFactory $customerFactory; |
| 54 | + |
| 55 | + /** |
| 56 | + * @var AuthenticationInterface |
| 57 | + */ |
| 58 | + private AuthenticationInterface $authentication; |
| 59 | + |
| 60 | + /** |
| 61 | + * @var AccountConfirmation |
| 62 | + */ |
| 63 | + private AccountConfirmation $accountConfirmation; |
| 64 | + |
| 65 | + /** |
| 66 | + * @var ManagerInterface |
| 67 | + */ |
| 68 | + private ManagerInterface $eventManager; |
| 69 | + |
| 70 | + /** |
| 71 | + * @param CustomerRegistry $customerRegistry |
| 72 | + * @param DataObjectHelper $dataObjectHelper |
| 73 | + * @param CustomerInterfaceFactory $customerFactory |
| 74 | + * @param AuthenticationInterface $authentication |
| 75 | + * @param AccountConfirmation $accountConfirmation |
| 76 | + * @param ManagerInterface $eventManager |
| 77 | + */ |
| 78 | + public function __construct( |
| 79 | + CustomerRegistry $customerRegistry, |
| 80 | + DataObjectHelper $dataObjectHelper, |
| 81 | + CustomerInterfaceFactory $customerFactory, |
| 82 | + AuthenticationInterface $authentication, |
| 83 | + AccountConfirmation $accountConfirmation, |
| 84 | + ManagerInterface $eventManager |
| 85 | + ) { |
| 86 | + $this->customerRegistry = $customerRegistry; |
| 87 | + $this->dataObjectHelper = $dataObjectHelper; |
| 88 | + $this->customerFactory = $customerFactory; |
| 89 | + $this->authentication = $authentication; |
| 90 | + $this->accountConfirmation = $accountConfirmation; |
| 91 | + $this->eventManager = $eventManager; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Authenticate a customer by username and password |
| 96 | + * |
| 97 | + * @param string $email |
| 98 | + * @param string $password |
| 99 | + * @return CustomerInterface |
| 100 | + * @throws LocalizedException |
| 101 | + */ |
| 102 | + public function execute(string $email, string $password): CustomerInterface |
| 103 | + { |
| 104 | + try { |
| 105 | + $customerModel = $this->customerRegistry->retrieveByEmail($email); |
| 106 | + } catch (NoSuchEntityException $exception) { |
| 107 | + throw new InvalidEmailOrPasswordException(__('Invalid login or password.')); |
| 108 | + } |
| 109 | + $customer = $this->getCustomerDataObject($customerModel); |
| 110 | + |
| 111 | + $customerId = $customer->getId(); |
| 112 | + if ($this->authentication->isLocked($customerId)) { |
| 113 | + throw new UserLockedException(__('The account is locked.')); |
| 114 | + } |
| 115 | + try { |
| 116 | + $this->authentication->authenticate($customerId, $password); |
| 117 | + } catch (InvalidEmailOrPasswordException $exception) { |
| 118 | + throw new InvalidEmailOrPasswordException(__('Invalid login or password.')); |
| 119 | + } |
| 120 | + |
| 121 | + if ($customer->getConfirmation() |
| 122 | + && ($this->isConfirmationRequired($customer) || $this->isEmailChangedConfirmationRequired($customer))) { |
| 123 | + throw new EmailNotConfirmedException(__('This account isn\'t confirmed. Verify and try again.')); |
| 124 | + } |
| 125 | + |
| 126 | + $this->eventManager->dispatch( |
| 127 | + 'customer_customer_authenticated', |
| 128 | + ['model' => $customerModel, 'password' => $password] |
| 129 | + ); |
| 130 | + |
| 131 | + $this->eventManager->dispatch('customer_data_object_login', ['customer' => $customer]); |
| 132 | + |
| 133 | + return $customer; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Convert custom model to DTO |
| 138 | + * |
| 139 | + * @param Customer $customerModel |
| 140 | + * @return CustomerInterface |
| 141 | + */ |
| 142 | + private function getCustomerDataObject(Customer $customerModel): CustomerInterface |
| 143 | + { |
| 144 | + $customerDataObject = $this->customerFactory->create(); |
| 145 | + $this->dataObjectHelper->populateWithArray( |
| 146 | + $customerDataObject, |
| 147 | + $customerModel->getData(), |
| 148 | + CustomerInterface::class |
| 149 | + ); |
| 150 | + $customerDataObject->setId($customerModel->getId()); |
| 151 | + return $customerDataObject; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Check if accounts confirmation is required in config |
| 156 | + * |
| 157 | + * @param CustomerInterface $customer |
| 158 | + * @return bool |
| 159 | + */ |
| 160 | + private function isConfirmationRequired($customer) |
| 161 | + { |
| 162 | + return $this->accountConfirmation->isConfirmationRequired( |
| 163 | + $customer->getWebsiteId(), |
| 164 | + $customer->getId(), |
| 165 | + $customer->getEmail() |
| 166 | + ); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Checks if account confirmation is required if the email address has been changed |
| 171 | + * |
| 172 | + * @param CustomerInterface $customer |
| 173 | + * @return bool |
| 174 | + */ |
| 175 | + private function isEmailChangedConfirmationRequired(CustomerInterface $customer): bool |
| 176 | + { |
| 177 | + return $this->accountConfirmation->isEmailChangedConfirmationRequired( |
| 178 | + (int)$customer->getWebsiteId(), |
| 179 | + (int)$customer->getId(), |
| 180 | + $customer->getEmail() |
| 181 | + ); |
| 182 | + } |
| 183 | +} |
0 commit comments