Skip to content

Commit

Permalink
Merge branch '2.4-develop' into Sortby-component
Browse files Browse the repository at this point in the history
  • Loading branch information
konarshankar07 authored Feb 20, 2020
2 parents 9404f53 + a1fad2b commit 858756d
Show file tree
Hide file tree
Showing 29 changed files with 1,593 additions and 328 deletions.
65 changes: 30 additions & 35 deletions app/code/Magento/Captcha/Observer/CheckContactUsFormObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,41 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Captcha\Observer;

use Magento\Captcha\Helper\Data;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\ActionFlag;
use Magento\Framework\App\Response\RedirectInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Message\ManagerInterface;

/**
* Class CheckContactUsFormObserver
* Check captcha on contact us form submit observer.
*/
class CheckContactUsFormObserver implements ObserverInterface
{
/**
* @var \Magento\Captcha\Helper\Data
* @var Data
*/
protected $_helper;

/**
* @var \Magento\Framework\App\ActionFlag
* @var ActionFlag
*/
protected $_actionFlag;

/**
* @var \Magento\Framework\Message\ManagerInterface
* @var ManagerInterface
*/
protected $messageManager;

/**
* @var \Magento\Framework\App\Response\RedirectInterface
* @var RedirectInterface
*/
protected $redirect;

Expand All @@ -45,60 +52,48 @@ class CheckContactUsFormObserver implements ObserverInterface
private $dataPersistor;

/**
* @param \Magento\Captcha\Helper\Data $helper
* @param \Magento\Framework\App\ActionFlag $actionFlag
* @param \Magento\Framework\Message\ManagerInterface $messageManager
* @param \Magento\Framework\App\Response\RedirectInterface $redirect
* @param Data $helper
* @param ActionFlag $actionFlag
* @param ManagerInterface $messageManager
* @param RedirectInterface $redirect
* @param CaptchaStringResolver $captchaStringResolver
* @param DataPersistorInterface $dataPersistor
*/
public function __construct(
\Magento\Captcha\Helper\Data $helper,
\Magento\Framework\App\ActionFlag $actionFlag,
\Magento\Framework\Message\ManagerInterface $messageManager,
\Magento\Framework\App\Response\RedirectInterface $redirect,
CaptchaStringResolver $captchaStringResolver
Data $helper,
ActionFlag $actionFlag,
ManagerInterface $messageManager,
RedirectInterface $redirect,
CaptchaStringResolver $captchaStringResolver,
DataPersistorInterface $dataPersistor
) {
$this->_helper = $helper;
$this->_actionFlag = $actionFlag;
$this->messageManager = $messageManager;
$this->redirect = $redirect;
$this->captchaStringResolver = $captchaStringResolver;
$this->dataPersistor = $dataPersistor;
}

/**
* Check CAPTCHA on Contact Us page
*
* @param \Magento\Framework\Event\Observer $observer
* @param Observer $observer
* @return void
*/
public function execute(\Magento\Framework\Event\Observer $observer)
public function execute(Observer $observer)
{
$formId = 'contact_us';
$captcha = $this->_helper->getCaptcha($formId);
if ($captcha->isRequired()) {
/** @var \Magento\Framework\App\Action\Action $controller */
/** @var Action $controller */
$controller = $observer->getControllerAction();
if (!$captcha->isCorrect($this->captchaStringResolver->resolve($controller->getRequest(), $formId))) {
$this->messageManager->addErrorMessage(__('Incorrect CAPTCHA.'));
$this->getDataPersistor()->set($formId, $controller->getRequest()->getPostValue());
$this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
$this->dataPersistor->set($formId, $controller->getRequest()->getPostValue());
$this->_actionFlag->set('', Action::FLAG_NO_DISPATCH, true);
$this->redirect->redirect($controller->getResponse(), 'contact/index/index');
}
}
}

/**
* Get Data Persistor
*
* @return DataPersistorInterface
*/
private function getDataPersistor()
{
if ($this->dataPersistor === null) {
$this->dataPersistor = ObjectManager::getInstance()
->get(DataPersistorInterface::class);
}

return $this->dataPersistor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,39 @@
*/
namespace Magento\Captcha\Observer;

use Magento\Captcha\Model\ResourceModel\Log;
use Magento\Captcha\Model\ResourceModel\LogFactory;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\LocalizedException;

/**
* Reset captcha attempts for Backend
*/
class ResetAttemptForBackendObserver implements ObserverInterface
{
/**
* @var \Magento\Captcha\Model\ResourceModel\LogFactory
* @var LogFactory
*/
public $resLogFactory;

/**
* @param \Magento\Captcha\Model\ResourceModel\LogFactory $resLogFactory
* @param LogFactory $resLogFactory
*/
public function __construct(
\Magento\Captcha\Model\ResourceModel\LogFactory $resLogFactory
LogFactory $resLogFactory
) {
$this->resLogFactory = $resLogFactory;
}

/**
* Reset Attempts For Backend
*
* @param \Magento\Framework\Event\Observer $observer
* @return \Magento\Captcha\Observer\ResetAttemptForBackendObserver
* @param Observer $observer
* @return Log
* @throws LocalizedException
*/
public function execute(\Magento\Framework\Event\Observer $observer)
public function execute(Observer $observer)
{
return $this->resLogFactory->create()->deleteUserAttempts($observer->getUser()->getUsername());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,42 @@
*/
namespace Magento\Captcha\Observer;

use Magento\Captcha\Model\ResourceModel\Log;
use Magento\Captcha\Model\ResourceModel\LogFactory;
use Magento\Customer\Model\Customer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\LocalizedException;

/**
* Reset captcha attempts for Frontend
*/
class ResetAttemptForFrontendObserver implements ObserverInterface
{
/**
* @var \Magento\Captcha\Model\ResourceModel\LogFactory
* @var LogFactory
*/
public $resLogFactory;

/**
* @param \Magento\Captcha\Model\ResourceModel\LogFactory $resLogFactory
* @param LogFactory $resLogFactory
*/
public function __construct(
\Magento\Captcha\Model\ResourceModel\LogFactory $resLogFactory
LogFactory $resLogFactory
) {
$this->resLogFactory = $resLogFactory;
}

/**
* Reset Attempts For Frontend
*
* @param \Magento\Framework\Event\Observer $observer
* @return \Magento\Captcha\Observer\ResetAttemptForFrontendObserver
* @param Observer $observer
* @return Log
* @throws LocalizedException
*/
public function execute(\Magento\Framework\Event\Observer $observer)
public function execute(Observer $observer)
{
/** @var \Magento\Customer\Model\Customer $model */
/** @var Customer $model */
$model = $observer->getModel();

return $this->resLogFactory->create()->deleteUserAttempts($model->getEmail());
Expand Down
Loading

0 comments on commit 858756d

Please sign in to comment.