Commit 07b9bd1c authored by Tino Goratsch's avatar Tino Goratsch

- added the first parts of the required site key and secret for the reCAPTCHA

- added proper form validation for reCAPTCHA
parent 047ffad2
......@@ -7,9 +7,13 @@
namespace ACP3\Modules\ACP3\Captcha\Extension;
use ACP3\Core\Http\RequestInterface;
use ACP3\Core\I18n\Translator;
use ACP3\Core\Settings\SettingsInterface;
use ACP3\Core\View;
use ACP3\Modules\ACP3\Captcha\Installer\Schema;
use ACP3\Modules\ACP3\Users\Model\UserModel;
use ReCaptcha\ReCaptcha;
class ReCaptchaCaptchaExtension implements CaptchaExtensionInterface
{
......@@ -25,18 +29,35 @@ class ReCaptchaCaptchaExtension implements CaptchaExtensionInterface
* @var UserModel
*/
private $user;
/**
* @var SettingsInterface
*/
private $settings;
/**
* @var RequestInterface
*/
private $request;
/**
* ReCaptchaCaptchaExtension constructor.
* @param Translator $translator
* @param RequestInterface $request
* @param SettingsInterface $settings
* @param View $view
* @param UserModel $user
*/
public function __construct(Translator $translator, View $view, UserModel $user)
public function __construct(
Translator $translator,
RequestInterface $request,
SettingsInterface $settings,
View $view,
UserModel $user)
{
$this->translator = $translator;
$this->view = $view;
$this->user = $user;
$this->settings = $settings;
$this->request = $request;
}
/**
......@@ -57,10 +78,12 @@ class ReCaptchaCaptchaExtension implements CaptchaExtensionInterface
$path = ''
) {
if (!$this->user->isAuthenticated()) {
$settings = $this->settings->getSettings(Schema::MODULE_NAME);
$this->view->assign('captcha', [
'id' => $formFieldId,
'input_only' => $inputOnly,
'sitekey' => '',
'sitekey' => $settings['recaptcha_sitekey'],
'language' => $this->translator->getShortIsoCode()
]);
......@@ -75,6 +98,14 @@ class ReCaptchaCaptchaExtension implements CaptchaExtensionInterface
*/
public function isCaptchaValid($formData, $formFieldName, array $extra = [])
{
return true;
$settings = $this->settings->getSettings(Schema::MODULE_NAME);
$recaptcha = new ReCaptcha($settings['recaptcha_secret']);
$response = $recaptcha->verify(
$formData['g-recaptcha-response'],
$this->request->getSymfonyRequest()->getClientIp()
);
return $response->isSuccess();
}
}
......@@ -25,6 +25,10 @@ class Migration implements MigrationInterface
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);",
],
34 => [
"INSERT INTO `{pre}settings` (`id`, `module_id`, `name`, `value`) VALUES ('', '{moduleId}', 'recaptcha_sitekey', '');",
"INSERT INTO `{pre}settings` (`id`, `module_id`, `name`, `value`) VALUES ('', '{moduleId}', 'recaptcha_secret', '');",
]
];
}
......
......@@ -29,7 +29,9 @@ class Schema implements Modules\Installer\SchemaInterface
public function settings()
{
return [
'captcha' => 'captcha.extension.native_captcha_extension'
'captcha' => 'captcha.extension.native_captcha_extension',
'recaptcha_sitekey' => '',
'recaptcha_secret' => ''
];
}
......@@ -54,6 +56,6 @@ class Schema implements Modules\Installer\SchemaInterface
*/
public function getSchemaVersion()
{
return 33;
return 34;
}
}
......@@ -27,6 +27,8 @@ services:
class: ACP3\Modules\ACP3\Captcha\Extension\ReCaptchaCaptchaExtension
arguments:
- '@core.lang'
- '@core.http.request'
- '@core.config'
- '@core.view'
- '@users.model.user_model'
tags:
......
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