Skip to content
Snippets Groups Projects
Commit 0d47a033 authored by fnuppy's avatar fnuppy Committed by Lachlan Ennis
Browse files

Issue #2025181 by fnuppy, ddrozdik: Add CAPTCHA to all forms by default

parent fddcc334
No related branches found
No related tags found
No related merge requests found
...@@ -165,6 +165,15 @@ function captcha_form_alter(array &$form, FormStateInterface $form_state, $form_ ...@@ -165,6 +165,15 @@ function captcha_form_alter(array &$form, FormStateInterface $form_state, $form_
->getStorage('captcha_point') ->getStorage('captcha_point')
->load($form_id); ->load($form_id);
if (!$captcha_point && $config->get('enabled_default')) {
// Create fake captcha point without saving.
$captcha_point = new CaptchaPoint([
'formId' => $form_id,
'captchaType' => $config->get('default_challenge'),
], 'captcha_point');
$captcha_point->enable();
}
if ($captcha_point && $captcha_point->status()) { if ($captcha_point && $captcha_point->status()) {
// Build CAPTCHA form element. // Build CAPTCHA form element.
$captcha_element = [ $captcha_element = [
......
enabled_default: 0
default_challenge: 'captcha/Math' default_challenge: 'captcha/Math'
description: 'This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.' description: 'This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.'
administration_mode: false administration_mode: false
......
...@@ -5,6 +5,9 @@ captcha.settings: ...@@ -5,6 +5,9 @@ captcha.settings:
type: config_object type: config_object
label: 'External Links Settings' label: 'External Links Settings'
mapping: mapping:
enabled_default:
type: integer
label: 'Add captcha to all forms.'
default_challenge: default_challenge:
type: string type: string
label: 'The default challenge for captcha.' label: 'The default challenge for captcha.'
......
...@@ -92,6 +92,13 @@ class CaptchaSettingsForm extends ConfigFormBase { ...@@ -92,6 +92,13 @@ class CaptchaSettingsForm extends ConfigFormBase {
'#default_value' => $config->get('default_challenge'), '#default_value' => $config->get('default_challenge'),
]; ];
// Option for enabling CAPTCHA for all forms.
$form['form_protection']['enabled_default'] = [
'#type' => 'checkbox',
'#title' => $this->t('Default challenge on non-listed forms.'),
'#description' => $this->t('Enable CAPTCHA for every form not listed in "CAPTCHA points"'),
'#default_value' => $config->get('enabled_default'),
];
// Field for the CAPTCHA administration mode. // Field for the CAPTCHA administration mode.
$form['form_protection']['administration_mode'] = [ $form['form_protection']['administration_mode'] = [
'#type' => 'checkbox', '#type' => 'checkbox',
...@@ -213,6 +220,7 @@ class CaptchaSettingsForm extends ConfigFormBase { ...@@ -213,6 +220,7 @@ class CaptchaSettingsForm extends ConfigFormBase {
$config->set('administration_mode', $form_state->getValue('administration_mode')); $config->set('administration_mode', $form_state->getValue('administration_mode'));
$config->set('allow_on_admin_pages', $form_state->getValue('allow_on_admin_pages')); $config->set('allow_on_admin_pages', $form_state->getValue('allow_on_admin_pages'));
$config->set('default_challenge', $form_state->getValue('default_challenge')); $config->set('default_challenge', $form_state->getValue('default_challenge'));
$config->set('enabled_default', $form_state->getValue('enabled_default'));
// CAPTCHA description stuff. // CAPTCHA description stuff.
$config->set('add_captcha_description', $form_state->getValue('add_captcha_description')); $config->set('add_captcha_description', $form_state->getValue('add_captcha_description'));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment