On déplace la gestion du contrôle du Captcha dans un controller spécifique --> c'est plus propre (merci Franck :-)

This commit is contained in:
2019-12-03 16:46:46 +01:00
parent 5d3943cd4b
commit 66758f160a
5 changed files with 45 additions and 21 deletions

View File

@@ -50,15 +50,6 @@ class ContactController extends BaseFrontController
try { try {
$form = $this->validateForm($contactForm); $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( $this->getMailer()->sendSimpleEmailMessage(
[ ConfigQuery::getStoreEmail() => $form->get('name')->getData() ], [ ConfigQuery::getStoreEmail() => $form->get('name')->getData() ],
[ ConfigQuery::getStoreEmail() => ConfigQuery::getStoreName() ], [ ConfigQuery::getStoreEmail() => ConfigQuery::getStoreName() ],

View File

@@ -164,18 +164,7 @@ class CustomerController extends BaseFrontController
try { try {
$form = $this->validateForm($customerCreation, "post"); $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()); $customerCreateEvent = $this->createEventInstance($form->getData());
$this->dispatch(TheliaEvents::CUSTOMER_CREATEACCOUNT, $customerCreateEvent); $this->dispatch(TheliaEvents::CUSTOMER_CREATEACCOUNT, $customerCreateEvent);
$newCustomer = $customerCreateEvent->getCustomer(); $newCustomer = $customerCreateEvent->getCustomer();

View File

@@ -25,6 +25,11 @@
<tag name="kernel.event_subscriber" /> <tag name="kernel.event_subscriber" />
<argument type="service" id="request"/> <argument type="service" id="request"/>
</service> </service>
<service id="thelia.form_validator" class="ReCaptcha\Form\MyTheliaFormValidator">
<argument type="service" id="thelia.translator" />
<argument>%kernel.environment%</argument>
<argument type="service" id="event_dispatcher" />
</service>
</services> </services>
<hooks> <hooks>

View File

@@ -0,0 +1,39 @@
<?php
namespace ReCaptcha\Form;
use ReCaptcha\Event\ReCaptchaCheckEvent;
use ReCaptcha\Event\ReCaptchaEvents;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Thelia\Form\BaseForm;
use Thelia\Form\Exception\FormValidationException;
class MyTheliaFormValidator extends \Thelia\Core\Form\TheliaFormValidator
{
/** @var EventDispatcherInterface */
protected $dispatcher;
public function __construct(TranslatorInterface $translator, $environment, EventDispatcherInterface $dispatcher)
{
$this->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
}
}

View File

@@ -22,6 +22,6 @@ class FrontHook extends BaseHook
$captchaId = $captchaId.'-invisible'; $captchaId = $captchaId.'-invisible';
} }
$event->add("<div id='$captchaId' class='g-recaptcha' data-sitekey='$siteKey' $captchaCallback data-size='$captchaStyle'></div>"); $event->add("<div id='$captchaId' class='g-recaptcha' data-sitekey='$siteKey' $captchaCallback data-size='$captchaStyle'></div><input type='hidden' name='captcha' value='1'>");
} }
} }