Created
May 19, 2022 22:33
-
-
Save jasonevans1/d8f54a647d597c55ade10d9f5483ac3a to your computer and use it in GitHub Desktop.
Magento Customer Authenticated Plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace Abc\Customer\Plugin; | |
use Magento\Customer\Api\AccountManagementInterface; | |
use Magento\Customer\Api\CustomerRepositoryInterface; | |
use Magento\Framework\Exception\NoSuchEntityException; | |
class CustomerAuthenticated | |
{ | |
private CustomerRepositoryInterface $customerRepository; | |
public function __construct(CustomerRepositoryInterface $customerRepository) | |
{ | |
$this->customerRepository = $customerRepository; | |
} | |
public function beforeAuthenticate(AccountManagementInterface $subject, $username, $password): array | |
{ | |
try { | |
$customer = $this->customerRepository->get($username); | |
} catch (NoSuchEntityException $e) { | |
throw new \Magento\Framework\Exception\InvalidEmailOrPasswordException(__('Invalid login or password.')); | |
} | |
if ($customer->getExtensionAttributes() | |
&& $customer->getExtensionAttributes()->getCompanyAttributes() | |
&& $customer->getExtensionAttributes()->getCompanyAttributes()->getActivated() == 0 | |
) { | |
throw new \Magento\Framework\Exception\LocalizedException(__('This account is locked.')); | |
} | |
return [$username, $password]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment