From 66758f160a9b54796bf24837adc5f9b26de4e2a7 Mon Sep 17 00:00:00 2001 From: Laurent LE CORRE Date: Tue, 3 Dec 2019 16:46:46 +0100 Subject: [PATCH] =?UTF-8?q?On=20d=C3=A9place=20la=20gestion=20du=20contr?= =?UTF-8?q?=C3=B4le=20du=20Captcha=20dans=20un=20controller=20sp=C3=A9cifi?= =?UTF-8?q?que=20-->=20c'est=20plus=20propre=20(merci=20Franck=20:-)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Front/Controller/ContactController.php | 9 ----- .../Front/Controller/CustomerController.php | 11 ------ local/modules/ReCaptcha/Config/config.xml | 5 +++ .../ReCaptcha/Form/MyTheliaFormValidator.php | 39 +++++++++++++++++++ local/modules/ReCaptcha/Hook/FrontHook.php | 2 +- 5 files changed, 45 insertions(+), 21 deletions(-) create mode 100644 local/modules/ReCaptcha/Form/MyTheliaFormValidator.php diff --git a/local/modules/Front/Controller/ContactController.php b/local/modules/Front/Controller/ContactController.php index fa398328..acb8806a 100644 --- a/local/modules/Front/Controller/ContactController.php +++ b/local/modules/Front/Controller/ContactController.php @@ -50,15 +50,6 @@ class ContactController extends BaseFrontController try { $form = $this->validateForm($contactForm); - $checkModule = ModuleQuery::create() - ->findOneByCode('ReCaptcha'); - - if($checkModule && $checkModule->getActivate()){ - $checkCaptchaEvent = new ReCaptchaCheckEvent(); - $this->dispatch(ReCaptchaEvents::CHECK_CAPTCHA_EVENT, $checkCaptchaEvent); - if ($checkCaptchaEvent->isHuman() == false) { throw new FormValidationException('Veuillez confirmer que vous n\'êtes pas un robot.'); } - } - $this->getMailer()->sendSimpleEmailMessage( [ ConfigQuery::getStoreEmail() => $form->get('name')->getData() ], [ ConfigQuery::getStoreEmail() => ConfigQuery::getStoreName() ], diff --git a/local/modules/Front/Controller/CustomerController.php b/local/modules/Front/Controller/CustomerController.php index 56b9f5d9..9e414458 100644 --- a/local/modules/Front/Controller/CustomerController.php +++ b/local/modules/Front/Controller/CustomerController.php @@ -164,18 +164,7 @@ class CustomerController extends BaseFrontController try { $form = $this->validateForm($customerCreation, "post"); - - $checkModule = ModuleQuery::create() - ->findOneByCode('ReCaptcha'); - - if($checkModule && $checkModule->getActivate()){ - $checkCaptchaEvent = new ReCaptchaCheckEvent(); - $this->dispatch(ReCaptchaEvents::CHECK_CAPTCHA_EVENT, $checkCaptchaEvent); - if ($checkCaptchaEvent->isHuman() == false) { throw new \Exception('Veuillez confirmer que vous n\'êtes pas un robot.'); } - } - $customerCreateEvent = $this->createEventInstance($form->getData()); - $this->dispatch(TheliaEvents::CUSTOMER_CREATEACCOUNT, $customerCreateEvent); $newCustomer = $customerCreateEvent->getCustomer(); diff --git a/local/modules/ReCaptcha/Config/config.xml b/local/modules/ReCaptcha/Config/config.xml index 2bcf4598..cd1a76d4 100644 --- a/local/modules/ReCaptcha/Config/config.xml +++ b/local/modules/ReCaptcha/Config/config.xml @@ -25,6 +25,11 @@ + + + %kernel.environment% + + diff --git a/local/modules/ReCaptcha/Form/MyTheliaFormValidator.php b/local/modules/ReCaptcha/Form/MyTheliaFormValidator.php new file mode 100644 index 00000000..ff852e88 --- /dev/null +++ b/local/modules/ReCaptcha/Form/MyTheliaFormValidator.php @@ -0,0 +1,39 @@ +dispatcher = $dispatcher; + + parent::__construct($translator, $environment); + } + + public function validateForm(BaseForm $aBaseForm, $expectedMethod = null) + { + if ($aBaseForm->getRequest()->get('captcha')) { + $checkCaptchaEvent = new ReCaptchaCheckEvent(); + $this->dispatcher->dispatch(ReCaptchaEvents::CHECK_CAPTCHA_EVENT, $checkCaptchaEvent); + if ($checkCaptchaEvent->isHuman() == false) { + throw new FormValidationException('Veuillez confirmer que vous n\'êtes pas un robot.'); + } + } + + return parent::validateForm($aBaseForm, $expectedMethod); // TODO: Change the autogenerated stub + } + +} \ No newline at end of file diff --git a/local/modules/ReCaptcha/Hook/FrontHook.php b/local/modules/ReCaptcha/Hook/FrontHook.php index cbcd3cef..37d3fd5e 100644 --- a/local/modules/ReCaptcha/Hook/FrontHook.php +++ b/local/modules/ReCaptcha/Hook/FrontHook.php @@ -22,6 +22,6 @@ class FrontHook extends BaseHook $captchaId = $captchaId.'-invisible'; } - $event->add("
"); + $event->add("
"); } }