Rajout du module ReCaptcha

This commit is contained in:
2021-02-09 15:09:14 +01:00
parent de56924f04
commit aa4a44aea7
22 changed files with 713 additions and 15 deletions

View File

@@ -0,0 +1,55 @@
<?php
namespace ReCaptcha\Form;
use ReCaptcha\ReCaptcha;
use Thelia\Core\Translation\Translator;
use Thelia\Form\BaseForm;
class ConfigurationForm extends BaseForm
{
protected function buildForm()
{
$this->formBuilder
->add(
"site_key",
"text",
[
"data" => ReCaptcha::getConfigValue("site_key"),
"label"=>Translator::getInstance()->trans("Site key", array(), ReCaptcha::DOMAIN_NAME),
"label_attr" => ["for" => "site_key"],
"required" => true
]
)
->add(
"secret_key",
"text",
[
"data" => ReCaptcha::getConfigValue("secret_key"),
"label"=>Translator::getInstance()->trans("Secret key", array(), ReCaptcha::DOMAIN_NAME),
"label_attr" => ["for" => "secret_key"],
"required" => true
]
)
->add(
"captcha_style",
"choice",
[
"data" => ReCaptcha::getConfigValue("captcha_style"),
"label"=>Translator::getInstance()->trans("ReCaptcha style", array(), ReCaptcha::DOMAIN_NAME),
"label_attr" => ["for" => "captcha_style"],
"required" => true,
'choices' => [
'normal'=>'Normal',
'compact'=>'Compact',
'invisible'=>'Invisible'
]
]
);
}
public function getName()
{
return "recaptcha_configuration_form";
}
}

View File

@@ -0,0 +1,37 @@
<?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
}
}