Skip to content

Commit

Permalink
fix: Improved Recaptcha fields Contraint and naming
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroisemaupate committed May 19, 2022
1 parent d9842ac commit 4121345
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 38 deletions.
40 changes: 5 additions & 35 deletions src/Form/Constraint/Recaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,15 @@

namespace RZ\Roadiz\CoreBundle\Form\Constraint;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\Constraint;

class Recaptcha extends Constraint
{
/**
* @var Request
*/
public $request;
/**
* @var string
*/
public $emptyMessage = 'you_must_show_youre_not_robot';
/**
* @var string
*/
public $invalidMessage = 'recaptcha_is_invalid.try_again';
/**
* @var string
*/
public $privateKey;
/**
* @var string
*/
public $fieldName = 'g-recaptcha-response';
/**
* @var string
*/
public $verifyUrl;

/**
* @param Request $request
* @param array $options
*/
public function __construct(Request $request, array $options)
{
parent::__construct($options);
$this->request = $request;
}
public string $emptyMessage = 'you_must_show_youre_not_robot';
public string $invalidMessage = 'recaptcha_is_invalid.try_again';
public string $fieldName = 'g-recaptcha-response';
public ?string $privateKey = null;
public ?string $verifyUrl = null;

/**
* @return string[]
Expand Down
22 changes: 20 additions & 2 deletions src/Form/Constraint/RecaptchaValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace RZ\Roadiz\CoreBundle\Form\Constraint;

use GuzzleHttp\Client;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

Expand All @@ -13,6 +14,16 @@
*/
class RecaptchaValidator extends ConstraintValidator
{
protected RequestStack $requestStack;

/**
* @param RequestStack $requestStack
*/
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}

/**
*
* @see \Symfony\Component\Validator\ConstraintValidator::validate()
Expand All @@ -23,7 +34,14 @@ public function validate($data, Constraint $constraint)
{
if ($constraint instanceof Recaptcha) {
$propertyPath = $this->context->getPropertyPath();
$responseField = $constraint->request->request->get($constraint->fieldName);

if (null === $this->requestStack->getCurrentRequest()) {
$this->context->buildViolation('Request is not defined')
->atPath($propertyPath)
->addViolation();
}

$responseField = $this->requestStack->getCurrentRequest()->get($constraint->fieldName);

if (empty($responseField)) {
$this->context->buildViolation($constraint->emptyMessage)
Expand Down Expand Up @@ -58,7 +76,7 @@ public function validate($data, Constraint $constraint)
*
* @return bool|string|array
*/
protected function check(Recaptcha $constraint, $responseField)
protected function check(Recaptcha $constraint, string $responseField)
{
$data = [
'secret' => $constraint->privateKey,
Expand Down
5 changes: 4 additions & 1 deletion src/Form/CustomFormsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$options['recaptcha_verifyurl'] :
'https://www.google.com/recaptcha/api/siteverify';

$builder->add('recaptcha', RecaptchaType::class, [
$builder->add($options['recaptcha_name'], RecaptchaType::class, [
'label' => false,
'configs' => [
'publicKey' => $options['recaptcha_public_key'],
Expand All @@ -85,6 +85,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
new Recaptcha($options['request'], [
'privateKey' => $options['recaptcha_private_key'],
'verifyUrl' => $verifyUrl,
'fieldName' => $options['recaptcha_name']
]),
],
]);
Expand Down Expand Up @@ -299,6 +300,7 @@ public function configureOptions(OptionsResolver $resolver)
'recaptcha_public_key' => null,
'recaptcha_private_key' => null,
'recaptcha_verifyurl' => null,
'recaptcha_name' => 'g-recaptcha-response',
'request' => null,
'forceExpanded' => false,
'csrf_protection' => false,
Expand All @@ -312,6 +314,7 @@ public function configureOptions(OptionsResolver $resolver)
$resolver->setAllowedTypes('recaptcha_public_key', ['string', 'null', 'boolean']);
$resolver->setAllowedTypes('recaptcha_private_key', ['string', 'null', 'boolean']);
$resolver->setAllowedTypes('recaptcha_verifyurl', ['string', 'null', 'boolean']);
$resolver->setAllowedTypes('recaptcha_name', ['string']);
}

/**
Expand Down

0 comments on commit 4121345

Please sign in to comment.