Skip to content
This repository was archived by the owner on Nov 24, 2023. It is now read-only.

Commit af3bb6e

Browse files
committed
Add captcha
1 parent 397f831 commit af3bb6e

22 files changed

Lines changed: 787 additions & 15 deletions

Constraints/Recaptcha.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Mdespeuilles\WebformBundle\Constraints;
4+
5+
use Symfony\Component\Validator\Constraint;
6+
7+
class Recaptcha extends Constraint
8+
{
9+
public $message = 'Invalid captcha';
10+
}

Constraints/RecaptchaValidator.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
4+
namespace Mdespeuilles\WebformBundle\Constraints;
5+
6+
7+
use Symfony\Component\HttpFoundation\RequestStack;
8+
use Symfony\Component\Validator\Constraint;
9+
use Symfony\Component\Validator\ConstraintValidator;
10+
11+
class RecaptchaValidator extends ConstraintValidator
12+
{
13+
private $requestStack;
14+
15+
private $recaptcha;
16+
17+
public function __construct(RequestStack $requestStack, \ReCaptcha\ReCaptcha $reCaptcha)
18+
{
19+
$this->requestStack = $requestStack;
20+
$this->recaptcha = $reCaptcha;
21+
}
22+
23+
public function validate($value, Constraint $constraint)
24+
{
25+
$request = $this->requestStack->getCurrentRequest();
26+
$recaptchaResponse = $request->request->get('g-recaptcha-response');
27+
if (empty($recaptchaResponse)) {
28+
$this->context->buildViolation($constraint->message)->addViolation();
29+
return;
30+
}
31+
32+
$response = $this->recaptcha->setExpectedHostname($request->getHost())->verify($recaptchaResponse, $request->getClientIp());
33+
34+
if (!$response->isSuccess()) {
35+
$this->context->buildViolation($constraint->message)->addViolation();
36+
return;
37+
}
38+
}
39+
}

Controller/WebformController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function submitAction(Request $request, $webformId) {
4444

4545
if ($request->isMethod('POST')) {
4646
$form->handleRequest($request);
47-
//if ($form->isValid()) {
47+
if ($form->isValid()) {
4848
$webformSubmission = new WebformSubmission();
4949
$webformSubmission->setWebform($webform);
5050

@@ -74,7 +74,7 @@ public function submitAction(Request $request, $webformId) {
7474
$response->setData($webform->getConfirmationMessage());
7575
return $response;
7676
}
77-
/*}
77+
}
7878
else {
7979
if ($webform->isUseAjax()) {
8080
$errors = FormError::getErrors($form);
@@ -85,7 +85,7 @@ public function submitAction(Request $request, $webformId) {
8585
]);
8686
return $response;
8787
}
88-
}*/
88+
}
8989
}
9090
}
9191
}

DependencyInjection/Configuration.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ class Configuration implements ConfigurationInterface
1818
public function getConfigTreeBuilder()
1919
{
2020
$treeBuilder = new TreeBuilder();
21-
$rootNode = $treeBuilder->root('open_access_webform');
21+
$rootNode = $treeBuilder->root('mdespeuilles_webform');
2222

23-
// Here you should define the parameters that are allowed to
24-
// configure your bundle. See the documentation linked above for
25-
// more information on that topic.
23+
$rootNode
24+
->children()
25+
->scalarNode('key')->defaultValue(null)->end()
26+
->scalarNode('secret')->defaultValue(null)->end()
27+
->end();
2628

2729
return $treeBuilder;
2830
}

DependencyInjection/MdespeuillesWebformExtension.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,8 @@ public function load(array $configs, ContainerBuilder $container)
2424

2525
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
2626
$loader->load('services.yml');
27+
28+
$container->setParameter('mdespeuilles_webform.key', $config['key']);
29+
$container->setParameter('mdespeuilles_webform.secret', $config['secret']);
2730
}
2831
}

Form/Type/RecaptchaSubmitType.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Mdespeuilles\WebformBundle\Form\Type;
4+
5+
use Mdespeuilles\WebformBundle\Constraints\Recaptcha;
6+
use Symfony\Component\Form\Extension\Core\Type\TextType;
7+
use Symfony\Component\Form\FormInterface;
8+
use Symfony\Component\Form\FormView;
9+
use \Symfony\Component\OptionsResolver\OptionsResolver;
10+
11+
class RecaptchaSubmitType extends \Symfony\Component\Form\AbstractType
12+
{
13+
14+
private $key;
15+
16+
public function __construct($key)
17+
{
18+
$this->key = $key;
19+
}
20+
21+
public function configureOptions(OptionsResolver $resolver)
22+
{
23+
$resolver->setDefaults([
24+
'mapped' => false,
25+
'constraints' => new Recaptcha()
26+
]);
27+
}
28+
29+
public function getBlockPrefix()
30+
{
31+
return 'recaptcha_submit';
32+
}
33+
34+
public function buildView(FormView $view, FormInterface $form, array $options)
35+
{
36+
$view->vars['label'] = false;
37+
$view->vars['key'] = $this->key;
38+
$view->vars['button'] = $options['label'];
39+
}
40+
41+
public function getParent()
42+
{
43+
return TextType::class;
44+
}
45+
}

Form/WebformType.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use AppBundle\Entity\WebformComponent;
77
use EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaType;
88
use Mdespeuilles\MarkupFieldBundle\Form\Type\MarkupType;
9+
use Mdespeuilles\WebformBundle\Form\Type\RecaptchaSubmitType;
910
use Sonata\CoreBundle\Form\Type\BooleanType;
1011
use Symfony\Component\Form\AbstractType;
1112
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
@@ -40,7 +41,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
4041
],
4142
'label' => $component->getName()
4243
];
43-
44+
4445
if ($component->getType() == MarkupType::class) {
4546
$componentOptions['label'] = false;
4647
$componentOptions['markup'] = $component->getName();
@@ -54,16 +55,21 @@ public function buildForm(FormBuilderInterface $builder, array $options)
5455
}
5556

5657
if ($webform->isUseCaptcha()) {
57-
$builder->add('recaptcha', EWZRecaptchaType::class, [
58+
/*$builder->add('recaptcha', EWZRecaptchaType::class, [
5859
"required" => true,
5960
"label" => false,
6061
"mapped" => false
62+
]);*/
63+
64+
$builder->add('captcha', RecaptchaSubmitType::class, [
65+
'label' => $webform->getSubmitString(),
66+
]);
67+
}
68+
else {
69+
$builder->add('submit', SubmitType::class, [
70+
'label' => $webform->getSubmitString()
6171
]);
6272
}
63-
64-
$builder->add('submit', SubmitType::class, [
65-
'label' => $webform->getSubmitString()
66-
]);
6773
}
6874

6975
/**
@@ -73,7 +79,8 @@ public function configureOptions(OptionsResolver $resolver)
7379
{
7480
$resolver->setDefaults(array(
7581
'data_class' => 'Mdespeuilles\WebformBundle\Entity\WebformComponent',
76-
'webform' => null
82+
'webform' => null,
83+
"allow_extra_fields" => true
7784
));
7885
}
7986

MdespeuillesWebformBundle.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22

33
namespace Mdespeuilles\WebformBundle;
44

5+
use Symfony\Component\DependencyInjection\ContainerBuilder;
56
use Symfony\Component\HttpKernel\Bundle\Bundle;
67

78
class MdespeuillesWebformBundle extends Bundle
89
{
10+
public function build(ContainerBuilder $container)
11+
{
12+
parent::build($container); // TODO: Change the autogenerated stub
13+
//$container->addCompilerPass(new RecaptchaComplierPass());
14+
}
915
}

RecaptchaComplierPass.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
4+
namespace Mdespeuilles\WebformBundle;
5+
6+
7+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
8+
use Symfony\Component\DependencyInjection\ContainerBuilder;
9+
10+
class RecaptchaComplierPass implements CompilerPassInterface
11+
{
12+
public function process(ContainerBuilder $container)
13+
{
14+
if ($container->hasParameter('twig.form.resources')) {
15+
$ressources = $container->getParameter('twig.form.resources') ?: [];
16+
array_unshift($ressources, 'fields.html.twig');
17+
$container->setParameter('twig.form.resources', $ressources);
18+
}
19+
}
20+
}

Resources/config/services.yml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,23 @@ services:
1919

2020
mdespeuilles.webform:
2121
class: Mdespeuilles\WebformBundle\Services\Webform
22-
arguments: [ '@service_container', '@request_stack' ]
22+
arguments: [ '@service_container', '@request_stack' ]
23+
24+
webformrecaptcha.type:
25+
class: Mdespeuilles\WebformBundle\Form\Type\RecaptchaSubmitType
26+
tags:
27+
- { name: form.type }
28+
arguments:
29+
$key: '%mdespeuilles_webform.key%'
30+
31+
recaptcha.validator:
32+
class: Mdespeuilles\WebformBundle\Constraints\RecaptchaValidator
33+
tags:
34+
- { name: validator.constraint_validator }
35+
arguments: ['@request_stack', '@Recaptcha\ReCaptcha']
36+
autowire: true
37+
38+
Recaptcha\ReCaptcha:
39+
class: ReCaptcha\ReCaptcha
40+
arguments:
41+
$secret: '%mdespeuilles_webform.secret%'

0 commit comments

Comments
 (0)