Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to disable external loading resources #308

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,16 @@ ewz_recaptcha:
score_threshold: 0.6
```

To disable loading recaptcha script from external source (most often for GDPR)
``` yaml
# app/config/config.yml

ewz_recaptcha:
// ...
external_recaptcha_assets: true
external_recaptcha_assets_missing_message: ~
```

Congratulations! You're ready!

## Basic Usage
Expand Down
2 changes: 2 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public function getConfigTreeBuilder(): TreeBuilder

->integerNode('timeout')->min(0)->defaultNull()->end()
->arrayNode('trusted_roles')->prototype('scalar')->treatNullLike(array())->end()
->booleanNode('external_recaptcha_assets')->defaultTrue()->end()
->scalarNode('external_recaptcha_assets_missing_message')->defaultNull()->end()
->end()
;

Expand Down
12 changes: 11 additions & 1 deletion src/Form/Type/EWZRecaptchaV3Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,28 @@ class EWZRecaptchaV3Type extends AbstractEWZRecaptchaType

/** @var bool */
private $hideBadge;
/** @var bool */
private $externalRecaptcha;

/** @var string|null */
private $externalRecaptchaMissingMessage;

/**
* EWZRecaptchaV3Type constructor.
*
* @param string $publicKey
* @param bool $enabled
* @param bool $hideBadge
* @param bool $externalRecaptcha
* @param string $apiHost
*/
public function __construct(string $publicKey, bool $enabled, bool $hideBadge, string $apiHost = 'www.google.com')
public function __construct(string $publicKey, bool $enabled, bool $hideBadge, bool $externalRecaptcha, ?string $externalRecaptchaMissingMessage, string $apiHost = 'www.google.com')
{
parent::__construct($publicKey, $enabled, $apiHost);

$this->hideBadge = $hideBadge;
$this->externalRecaptcha = $externalRecaptcha;
$this->externalRecaptchaMissingMessage = $externalRecaptchaMissingMessage;
}

/**
Expand Down Expand Up @@ -61,6 +69,8 @@ protected function addCustomVars(FormView $view, FormInterface $form, array $opt
{
$view->vars = array_replace($view->vars, [
'ewz_recaptcha_hide_badge' => $this->hideBadge,
'ewz_external_recaptcha_assets' => $this->externalRecaptcha,
'external_recaptcha_assets_missing_message' => $this->externalRecaptchaMissingMessage,
'script_nonce_csp' => $options['script_nonce_csp'] ?? '',
'action_name' => $options['action_name'] ?? '',
]);
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ services:
- '%ewz_recaptcha.public_key%'
- '%ewz_recaptcha.enabled%'
- '%ewz_recaptcha.hide_badge%'
- '%ewz_recaptcha.external_recaptcha_assets%'
- '%ewz_recaptcha.api_host%'
tags:
- { name: form.type }
Expand Down
16 changes: 15 additions & 1 deletion src/Resources/views/Form/v3/ewz_recaptcha_widget.html.twig
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{% block ewz_recaptcha_widget %}
{% apply spaceless %}
{% if form.vars.ewz_recaptcha_enabled %}
<script src="{{ form.vars.ewz_recaptcha_api_uri }}?render={{ form.vars.public_key }}"></script>
{% if form.vars.ewz_external_recaptcha_assets %}
<script src="{{ form.vars.ewz_recaptcha_api_uri }}?render={{ form.vars.public_key }}"></script>
{% endif %}

{% if form.vars.ewz_recaptcha_hide_badge %}
<link rel="stylesheet" href="{{ asset('/bundles/ewzrecaptcha/css/recaptcha.css') }}">
Expand All @@ -16,6 +18,18 @@
grecaptchaForm.addEventListener('submit', function (e) {
e.preventDefault();

{% if form.vars.ewz_external_recaptcha_assets %}
if (!Object.hasOwn(window, "grecaptcha")) {
console.error('Recaptcha not found');

{% if form.vars.external_recaptcha_assets_missing_message is not null %}
alert({{ form.vars.external_recaptcha_assets_missing_message|trans }});

{% endif %}
return;
}
{% endif %}

grecaptcha.ready(function () {
grecaptcha.execute('{{ form.vars.public_key }}', { action: '{{ form.vars.action_name|default(constant('EWZ\\Bundle\\RecaptchaBundle\\Form\\Type\\EWZRecaptchaV3Type::DEFAULT_ACTION_NAME')) }}' }).then(function (token) {
grecaptchaInput.value = token;
Expand Down
2 changes: 1 addition & 1 deletion tests/Form/Type/EWZRecaptchaV3TypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class EWZRecaptchaV3TypeTest extends TestCase
protected function setUp(): void
{
$requestStack = $this->createMock(RequestStack::class);
$this->type = new EWZRecaptchaV3Type('key', true, true, 'www.google.com');
$this->type = new EWZRecaptchaV3Type('key', true, true, true, null, 'www.google.com');
}

/**
Expand Down