Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CheckUserLoginObserver.php
Go to the documentation of this file.
1 <?php
7 
12 
17 {
21  protected $_helper;
22 
26  protected $_actionFlag;
27 
31  protected $messageManager;
32 
36  protected $_session;
37 
42 
48  protected $_customerUrl;
49 
54 
60  protected $authentication;
61 
70  public function __construct(
71  \Magento\Captcha\Helper\Data $helper,
72  \Magento\Framework\App\ActionFlag $actionFlag,
73  \Magento\Framework\Message\ManagerInterface $messageManager,
74  \Magento\Framework\Session\SessionManagerInterface $customerSession,
76  \Magento\Customer\Model\Url $customerUrl
77  ) {
78  $this->_helper = $helper;
79  $this->_actionFlag = $actionFlag;
80  $this->messageManager = $messageManager;
81  $this->_session = $customerSession;
82  $this->captchaStringResolver = $captchaStringResolver;
83  $this->_customerUrl = $customerUrl;
84  }
85 
91  private function getCustomerRepository()
92  {
93 
94  if (!($this->customerRepository instanceof \Magento\Customer\Api\CustomerRepositoryInterface)) {
95  return \Magento\Framework\App\ObjectManager::getInstance()->get(
96  \Magento\Customer\Api\CustomerRepositoryInterface::class
97  );
98  } else {
100  }
101  }
102 
108  private function getAuthentication()
109  {
110 
111  if (!($this->authentication instanceof AuthenticationInterface)) {
112  return \Magento\Framework\App\ObjectManager::getInstance()->get(
113  AuthenticationInterface::class
114  );
115  } else {
116  return $this->authentication;
117  }
118  }
119 
127  public function execute(\Magento\Framework\Event\Observer $observer)
128  {
129  $formId = 'user_login';
130  $captchaModel = $this->_helper->getCaptcha($formId);
131  $controller = $observer->getControllerAction();
132  $loginParams = $controller->getRequest()->getPost('login');
133  $login = (is_array($loginParams) && array_key_exists('username', $loginParams))
134  ? $loginParams['username']
135  : null;
136  if ($captchaModel->isRequired($login)) {
137  $word = $this->captchaStringResolver->resolve($controller->getRequest(), $formId);
138  if (!$captchaModel->isCorrect($word)) {
139  try {
140  $customer = $this->getCustomerRepository()->get($login);
141  $this->getAuthentication()->processAuthenticationFailure($customer->getId());
142  } catch (NoSuchEntityException $e) {
143  //do nothing as customer existance is validated later in authenticate method
144  }
145  $this->messageManager->addError(__('Incorrect CAPTCHA'));
146  $this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
147  $this->_session->setUsername($login);
148  $beforeUrl = $this->_session->getBeforeAuthUrl();
149  $url = $beforeUrl ? $beforeUrl : $this->_customerUrl->getLoginUrl();
150  $controller->getResponse()->setRedirect($url);
151  }
152  }
153  $captchaModel->logAttempt($login);
154 
155  return $this;
156  }
157 }
$helper
Definition: iframe.phtml:13
__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)
$customer
Definition: customers.php:11
$customerUrl
Definition: info.phtml:28
__()
Definition: __.php:13
execute(\Magento\Framework\Event\Observer $observer)
$controller
Definition: info.phtml:14