Skip to content

Commit

Permalink
Refactored multiples conditions which could be grouped in a single on…
Browse files Browse the repository at this point in the history
…e in captcha observers
  • Loading branch information
jochhop authored and mage2pratik committed Jul 27, 2018
1 parent 327d9d5 commit e54a63e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 35 deletions.
17 changes: 8 additions & 9 deletions app/code/Magento/Captcha/Observer/CheckGuestCheckoutObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,14 @@ public function execute(\Magento\Framework\Event\Observer $observer)
$formId = 'guest_checkout';
$captchaModel = $this->_helper->getCaptcha($formId);
$checkoutMethod = $this->_typeOnepage->getQuote()->getCheckoutMethod();
if ($checkoutMethod == \Magento\Checkout\Model\Type\Onepage::METHOD_GUEST) {
if ($captchaModel->isRequired()) {
$controller = $observer->getControllerAction();
if (!$captchaModel->isCorrect($this->captchaStringResolver->resolve($controller->getRequest(), $formId))
) {
$this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
$result = ['error' => 1, 'message' => __('Incorrect CAPTCHA')];
$controller->getResponse()->representJson($this->jsonHelper->jsonEncode($result));
}
if ($checkoutMethod == \Magento\Checkout\Model\Type\Onepage::METHOD_GUEST
&& $captchaModel->isRequired()
) {
$controller = $observer->getControllerAction();
if (!$captchaModel->isCorrect($this->captchaStringResolver->resolve($controller->getRequest(), $formId))) {
$this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
$result = ['error' => 1, 'message' => __('Incorrect CAPTCHA')];
$controller->getResponse()->representJson($this->jsonHelper->jsonEncode($result));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,14 @@ public function execute(\Magento\Framework\Event\Observer $observer)
$formId = 'register_during_checkout';
$captchaModel = $this->_helper->getCaptcha($formId);
$checkoutMethod = $this->_typeOnepage->getQuote()->getCheckoutMethod();
if ($checkoutMethod == \Magento\Checkout\Model\Type\Onepage::METHOD_REGISTER) {
if ($captchaModel->isRequired()) {
$controller = $observer->getControllerAction();
if (!$captchaModel->isCorrect($this->captchaStringResolver->resolve($controller->getRequest(), $formId))
) {
$this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
$result = ['error' => 1, 'message' => __('Incorrect CAPTCHA')];
$controller->getResponse()->representJson($this->jsonHelper->jsonEncode($result));
}
if ($checkoutMethod == \Magento\Checkout\Model\Type\Onepage::METHOD_REGISTER
&& $captchaModel->isRequired()
) {
$controller = $observer->getControllerAction();
if (!$captchaModel->isCorrect($this->captchaStringResolver->resolve($controller->getRequest(), $formId))) {
$this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
$result = ['error' => 1, 'message' => __('Incorrect CAPTCHA')];
$controller->getResponse()->representJson($this->jsonHelper->jsonEncode($result));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,17 @@ public function execute(\Magento\Framework\Event\Observer $observer)
$controller = $observer->getControllerAction();
$email = (string)$observer->getControllerAction()->getRequest()->getParam('email');
$params = $observer->getControllerAction()->getRequest()->getParams();
if (!empty($email) && !empty($params)) {
if ($captchaModel->isRequired()) {
if (!$captchaModel->isCorrect($this->captchaStringResolver->resolve($controller->getRequest(), $formId))
) {
$this->_session->setEmail((string)$controller->getRequest()->getPost('email'));
$this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
$this->messageManager->addError(__('Incorrect CAPTCHA'));
$controller->getResponse()->setRedirect(
$controller->getUrl('*/*/forgotpassword', ['_nosecret' => true])
);
}
}
if (!empty($email)
&& !empty($params)
&& $captchaModel->isRequired()
&& !$captchaModel->isCorrect($this->captchaStringResolver->resolve($controller->getRequest(), $formId))
) {
$this->_session->setEmail((string)$controller->getRequest()->getPost('email'));
$this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
$this->messageManager->addError(__('Incorrect CAPTCHA'));
$controller->getResponse()->setRedirect(
$controller->getUrl('*/*/forgotpassword', ['_nosecret' => true])
);
}

return $this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ public function execute(\Magento\Framework\Event\Observer $observer)
$formId = 'backend_login';
$captchaModel = $this->_helper->getCaptcha($formId);
$login = $observer->getEvent()->getUsername();
if ($captchaModel->isRequired($login)) {
if (!$captchaModel->isCorrect($this->captchaStringResolver->resolve($this->_request, $formId))) {
$captchaModel->logAttempt($login);
throw new PluginAuthenticationException(__('Incorrect CAPTCHA.'));
}
if ($captchaModel->isRequired($login)
&& !$captchaModel->isCorrect($this->captchaStringResolver->resolve($this->_request, $formId))
) {
$captchaModel->logAttempt($login);
throw new PluginAuthenticationException(__('Incorrect CAPTCHA.'));
}
$captchaModel->logAttempt($login);

Expand Down

0 comments on commit e54a63e

Please sign in to comment.