| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Captcha\Observer;
- use Magento\Customer\Model\AuthenticationInterface;
- use Magento\Framework\Event\ObserverInterface;
- use Magento\Framework\Exception\NoSuchEntityException;
- use Magento\Customer\Api\CustomerRepositoryInterface;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class CheckUserLoginObserver implements ObserverInterface
- {
- /**
- * @var \Magento\Captcha\Helper\Data
- */
- protected $_helper;
- /**
- * @var \Magento\Framework\App\ActionFlag
- */
- protected $_actionFlag;
- /**
- * @var \Magento\Framework\Message\ManagerInterface
- */
- protected $messageManager;
- /**
- * @var \Magento\Framework\Session\SessionManagerInterface
- */
- protected $_session;
- /**
- * @var CaptchaStringResolver
- */
- protected $captchaStringResolver;
- /**
- * Customer data
- *
- * @var \Magento\Customer\Model\Url
- */
- protected $_customerUrl;
- /**
- * @var CustomerRepositoryInterface
- */
- protected $customerRepository;
- /**
- * Authentication
- *
- * @var AuthenticationInterface
- */
- protected $authentication;
- /**
- * @param \Magento\Captcha\Helper\Data $helper
- * @param \Magento\Framework\App\ActionFlag $actionFlag
- * @param \Magento\Framework\Message\ManagerInterface $messageManager
- * @param \Magento\Framework\Session\SessionManagerInterface $customerSession
- * @param CaptchaStringResolver $captchaStringResolver
- * @param \Magento\Customer\Model\Url $customerUrl
- */
- public function __construct(
- \Magento\Captcha\Helper\Data $helper,
- \Magento\Framework\App\ActionFlag $actionFlag,
- \Magento\Framework\Message\ManagerInterface $messageManager,
- \Magento\Framework\Session\SessionManagerInterface $customerSession,
- CaptchaStringResolver $captchaStringResolver,
- \Magento\Customer\Model\Url $customerUrl
- ) {
- $this->_helper = $helper;
- $this->_actionFlag = $actionFlag;
- $this->messageManager = $messageManager;
- $this->_session = $customerSession;
- $this->captchaStringResolver = $captchaStringResolver;
- $this->_customerUrl = $customerUrl;
- }
- /**
- * Get customer repository
- *
- * @return \Magento\Customer\Api\CustomerRepositoryInterface
- */
- private function getCustomerRepository()
- {
- if (!($this->customerRepository instanceof \Magento\Customer\Api\CustomerRepositoryInterface)) {
- return \Magento\Framework\App\ObjectManager::getInstance()->get(
- \Magento\Customer\Api\CustomerRepositoryInterface::class
- );
- } else {
- return $this->customerRepository;
- }
- }
- /**
- * Get authentication
- *
- * @return AuthenticationInterface
- */
- private function getAuthentication()
- {
- if (!($this->authentication instanceof AuthenticationInterface)) {
- return \Magento\Framework\App\ObjectManager::getInstance()->get(
- AuthenticationInterface::class
- );
- } else {
- return $this->authentication;
- }
- }
- /**
- * Check captcha on user login page
- *
- * @param \Magento\Framework\Event\Observer $observer
- * @throws NoSuchEntityException
- * @return $this
- */
- public function execute(\Magento\Framework\Event\Observer $observer)
- {
- $formId = 'user_login';
- $captchaModel = $this->_helper->getCaptcha($formId);
- $controller = $observer->getControllerAction();
- $loginParams = $controller->getRequest()->getPost('login');
- $login = (is_array($loginParams) && array_key_exists('username', $loginParams))
- ? $loginParams['username']
- : null;
- if ($captchaModel->isRequired($login)) {
- $word = $this->captchaStringResolver->resolve($controller->getRequest(), $formId);
- if (!$captchaModel->isCorrect($word)) {
- try {
- $customer = $this->getCustomerRepository()->get($login);
- $this->getAuthentication()->processAuthenticationFailure($customer->getId());
- } catch (NoSuchEntityException $e) {
- //do nothing as customer existance is validated later in authenticate method
- }
- $this->messageManager->addError(__('Incorrect CAPTCHA'));
- $this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
- $this->_session->setUsername($login);
- $beforeUrl = $this->_session->getBeforeAuthUrl();
- $url = $beforeUrl ? $beforeUrl : $this->_customerUrl->getLoginUrl();
- $controller->getResponse()->setRedirect($url);
- }
- }
- $captchaModel->logAttempt($login);
- return $this;
- }
- }
|