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:
@@ -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() ],
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -25,6 +25,11 @@
|
||||
<tag name="kernel.event_subscriber" />
|
||||
<argument type="service" id="request"/>
|
||||
</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>
|
||||
|
||||
<hooks>
|
||||
|
||||
39
local/modules/ReCaptcha/Form/MyTheliaFormValidator.php
Normal file
39
local/modules/ReCaptcha/Form/MyTheliaFormValidator.php
Normal 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
|
||||
}
|
||||
|
||||
}
|
||||
@@ -22,6 +22,6 @@ class FrontHook extends BaseHook
|
||||
$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'>");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user