Commit 0c248b10 authored by Tino Goratsch's avatar Tino Goratsch

made the captcha type configurable via the new captcha settings page

parent 510b7bfa
......@@ -51,19 +51,6 @@ class Validator
return $this;
}
/**
* @return array
*/
protected function getDefaultConstraintParams()
{
return [
'data' => null,
'field' => '',
'message' => '',
'extra' => []
];
}
/**
* @param string $validationRule
* @param array $params
......@@ -80,6 +67,19 @@ class Validator
return $this;
}
/**
* @return array
*/
protected function getDefaultConstraintParams()
{
return [
'data' => null,
'field' => '',
'message' => '',
'extra' => []
];
}
/**
* @param string $message
* @param string|array $field
......@@ -148,9 +148,7 @@ class Validator
$params['extra']
);
} else {
throw new ValidationRuleNotFoundException(
'Can not find the validation rule with the name ' . $constraint['rule'] . '.'
);
throw new ValidationRuleNotFoundException(sprintf($this->getExceptionMessage(), $constraint['rule']));
}
}
......@@ -159,6 +157,22 @@ class Validator
}
}
/**
* @return string
*/
private function getExceptionMessage()
{
return 'Can not find the validation rule with the name "%s".';
}
/**
* @return bool
*/
protected function hasErrors()
{
return !empty($this->errors);
}
/**
* @param string $validationRule
* @param mixed $field
......@@ -172,14 +186,6 @@ class Validator
return $this->validationRules[$validationRule]->isValid($field);
}
throw new ValidationRuleNotFoundException('Can not find the validation rule with the name ' . $validationRule . '.');
}
/**
* @return bool
*/
protected function hasErrors()
{
return !empty($this->errors);
throw new ValidationRuleNotFoundException(sprintf($this->getExceptionMessage(), $validationRule));
}
}
......@@ -8,10 +8,57 @@ namespace ACP3\Modules\ACP3\Captcha\Controller\Admin\Index;
use ACP3\Core\Controller\AbstractAdminAction;
use ACP3\Core\Controller\Context\FrontendContext;
use ACP3\Core\Helpers\Forms;
use ACP3\Core\Helpers\FormToken;
use ACP3\Modules\ACP3\Captcha\Extension\CaptchaExtensionInterface;
use ACP3\Modules\ACP3\Captcha\Installer\Schema;
use ACP3\Modules\ACP3\Captcha\Utility\CaptchaRegistrar;
use ACP3\Modules\ACP3\Captcha\Validation\AdminSettingsFormValidation;
class Settings extends AbstractAdminAction
{
/**
* @var Forms
*/
private $forms;
/**
* @var FormToken
*/
private $formToken;
/**
* @var CaptchaRegistrar
*/
private $captchaRegistrar;
/**
* @var AdminSettingsFormValidation
*/
private $formValidation;
/**
* Settings constructor.
*
* @param FrontendContext $context
* @param Forms $forms
* @param FormToken $formToken
* @param CaptchaRegistrar $captchaRegistrar
* @param AdminSettingsFormValidation $formValidation
*/
public function __construct(
FrontendContext $context,
Forms $forms,
FormToken $formToken,
CaptchaRegistrar $captchaRegistrar,
AdminSettingsFormValidation $formValidation
) {
parent::__construct($context);
$this->forms = $forms;
$this->formToken = $formToken;
$this->captchaRegistrar = $captchaRegistrar;
$this->formValidation = $formValidation;
}
/**
* @return array
*/
......@@ -19,8 +66,16 @@ class Settings extends AbstractAdminAction
{
$settings = $this->config->getSettings(Schema::MODULE_NAME);
$captchas = [];
foreach ($this->captchaRegistrar->getAvailableCaptchas() as $serviceId => $captcha) {
/** @var CaptchaExtensionInterface $captcha */
$captchas[$serviceId] = $captcha->getCaptchaName();
}
return [
'form' => array_merge($settings, $this->request->getPost()->all())
'captchas' => $this->forms->choicesGenerator('captcha', $captchas, $settings['captcha']),
'form' => array_merge($settings, $this->request->getPost()->all()),
'form_token' => $this->formToken->renderFormToken()
];
}
......@@ -32,6 +87,8 @@ class Settings extends AbstractAdminAction
return $this->actionHelper->handleSettingsPostAction(function () {
$formData = $this->request->getPost()->all();
$this->formValidation->validate($formData);
return $this->config->saveSettings($formData, Schema::MODULE_NAME);
});
}
......
......@@ -24,7 +24,7 @@ class CaptchaCompilerPass implements CompilerPassInterface
foreach ($plugins as $serviceId => $tags) {
$definition->addMethodCall(
'registerCaptcha',
[new Reference($serviceId)]
[$serviceId, new Reference($serviceId)]
);
}
}
......
......@@ -12,8 +12,6 @@ class Migration implements MigrationInterface
{
/**
* @inheritdoc
*
* @return array
*/
public function schemaUpdates()
{
......@@ -23,14 +21,16 @@ class Migration implements MigrationInterface
],
32 => [
"INSERT INTO `{pre}settings` (`id`, `module_id`, `name`, `value`) VALUES ('', '{moduleId}', 'captcha', 'captcha.extension.native_captcha_extension');",
],
33 => [
"INSERT INTO `{pre}acl_resources` (`module_id`, `area`, `controller`, `page`, `params`, `privilege_id`) VALUES ('{moduleId}', 'admin', 'index', 'index', '', 3);",
"INSERT INTO `{pre}acl_resources` (`module_id`, `area`, `controller`, `page`, `params`, `privilege_id`) VALUES ('{moduleId}', 'admin', 'index', 'settings', '', 7);",
]
];
}
/**
* @inheritdoc
*
* @return array
*/
public function renameModule()
{
......
......@@ -54,6 +54,6 @@ class Schema implements Modules\Installer\SchemaInterface
*/
public function getSchemaVersion()
{
return 32;
return 33;
}
}
{extends file="asset:System/layout.header-bar.tpl"}
{block HEADER_BAR_OPTIONS}
{check_access mode="link" path="acp/captcha/index/settings" class="glyphicon glyphicon-cog"}
{/block}
{block CONTENT_AFTER_HEADER_BAR}
{redirect_message}
{include file="asset:System/Partials/no_results.tpl" no_results_text={lang t="system|select_menu_item"}}
{/block}
{extends file="asset:System/layout.ajax-form.tpl"}
{block CONTENT_AJAX_FORM}
{include file="asset:System/Partials/form_group.select.tpl" options=$captchas required=true label={lang t="captcha|captcha_type"}}
{include file="asset:System/Partials/form_group.submit.tpl" form_token=$form_token back_url={uri args="acp/captcha"}}
{/block}
services:
captcha.controller.admin.index.index:
class: ACP3\Modules\ACP3\Captcha\Controller\Admin\Index\Index
arguments:
- '@core.context.frontend'
captcha.controller.admin.index.settings:
class: ACP3\Modules\ACP3\Captcha\Controller\Admin\Index\Settings
arguments:
- '@core.context.frontend'
- '@core.helpers.forms'
- '@core.helpers.formToken'
- '@captcha.utility.captcha_registrar'
- '@cpatcha.validation.admin_settings_form_validation'
captcha.controller.frontend.index.image:
class: ACP3\Modules\ACP3\Captcha\Controller\Frontend\Index\Image
arguments:
......
......@@ -9,3 +9,14 @@ services:
- '@users.model.user_model'
tags:
- { name: core.validation.validation_rule }
captcha.validation.validation_rules.captcha_type_validation_rule:
class: ACP3\Modules\ACP3\Captcha\Validation\ValidationRules\CaptchaTypeValidationRule
arguments:
- '@captcha.utility.captcha_registrar'
tags:
- { name: core.validation.validation_rule }
cpatcha.validation.admin_settings_form_validation:
class: ACP3\Modules\ACP3\Captcha\Validation\AdminSettingsFormValidation
parent: core.validator.abstract
......@@ -5,9 +5,12 @@
<direction>ltr</direction>
</info>
<keys>
<item key="admin_index_settings">Einstellungen</item>
<item key="captcha">Captcha</item>
<item key="invalid_captcha_entered">Entweder wurde das Captcha falsch eingegeben oder es ist nicht mehr gültig.
</item>
<item key="captcha_type">Captcha-Typ</item>
<item key="invalid_captcha_entered">Entweder wurde das Captcha falsch eingegeben oder es ist nicht mehr gültig.</item>
<item key="mod_description">Captchas bieten wirkungsvollen Schutz gegen Spambots.</item>
<item key="native">ACP3-Nativ</item>
<item key="select_captcha_type">Bitte wählen Sie den zu verwendenten Captcha-Typen aus.</item>
</keys>
</language>
......@@ -5,8 +5,12 @@
<direction>ltr</direction>
</info>
<keys>
<item key="admin_index_settings">Settings</item>
<item key="captcha">Captcha</item>
<item key="captcha_type">Captcha type</item>
<item key="invalid_captcha_entered">Either the captcha is incorrect, or it is no longer valid.</item>
<item key="mod_description">Captchas provide effective protection against spambots.</item>
<item key="native">ACP3 native</item>
<item key="select_captcha_type">Please select the captcha type which should be used.</item>
</keys>
</language>
......@@ -17,11 +17,12 @@ class CaptchaRegistrar
protected $availableCaptchas = [];
/**
* @param string $serviceId
* @param CaptchaExtensionInterface $captchaExtension
*/
public function registerCaptcha(CaptchaExtensionInterface $captchaExtension)
public function registerCaptcha($serviceId, CaptchaExtensionInterface $captchaExtension)
{
$this->availableCaptchas[$captchaExtension->getCaptchaName()] = $captchaExtension;
$this->availableCaptchas[$serviceId] = $captchaExtension;
}
/**
......@@ -31,4 +32,13 @@ class CaptchaRegistrar
{
return $this->availableCaptchas;
}
/**
* @param string $serviceId
* @return bool
*/
public function hasCaptcha($serviceId)
{
return isset($this->availableCaptchas[$serviceId]);
}
}
<?php
/**
* Copyright (c) 2017 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Modules\ACP3\Captcha\Validation;
use ACP3\Core\Validation\AbstractFormValidation;
use ACP3\Core\Validation\ValidationRules\FormTokenValidationRule;
use ACP3\Modules\ACP3\Captcha\Validation\ValidationRules\CaptchaTypeValidationRule;
class AdminSettingsFormValidation extends AbstractFormValidation
{
/**
* @param array $formData
*
* @throws \ACP3\Core\Validation\Exceptions\InvalidFormTokenException
* @throws \ACP3\Core\Validation\Exceptions\ValidationFailedException
*/
public function validate(array $formData)
{
$this->validator
->addConstraint(FormTokenValidationRule::class)
->addConstraint(CaptchaTypeValidationRule::class, [
'data' => $formData,
'field' => 'captcha',
'message' => $this->translator->t('captcha', 'select_captcha_type')
]);
$this->validator->validate();
}
}
<?php
/**
* Copyright (c) 2017 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Modules\ACP3\Captcha\Validation\ValidationRules;
use ACP3\Core\Validation\ValidationRules\AbstractValidationRule;
use ACP3\Modules\ACP3\Captcha\Utility\CaptchaRegistrar;
class CaptchaTypeValidationRule extends AbstractValidationRule
{
/**
* @var CaptchaRegistrar
*/
private $captchaRegistrar;
/**
* CaptchaTypeValidationRule constructor.
* @param CaptchaRegistrar $captchaRegistrar
*/
public function __construct(CaptchaRegistrar $captchaRegistrar)
{
$this->captchaRegistrar = $captchaRegistrar;
}
/**
* @inheritdoc
*/
public function isValid($data, $field = '', array $extra = [])
{
if (is_array($data) && array_key_exists($field, $data)) {
return $this->isValid($data[$field], $field, $extra);
}
return $this->captchaRegistrar->hasCaptcha($data);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment