Commit ecd4a284 authored by Tino Goratsch's avatar Tino Goratsch

inject the CaptchaValidationRule via the new event captcha.validation.validate_captcha

parent fda721a3
<?php
/**
* Copyright (c) 2016 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Modules\ACP3\Captcha\Event\Listener;
use ACP3\Core\I18n\Translator;
use ACP3\Core\Validation\Event\FormValidationEvent;
use ACP3\Modules\ACP3\Captcha\Validation\ValidationRules\CaptchaValidationRule;
class OnCaptchaValidationValidateCaptchaListener
{
/**
* @var Translator
*/
private $translator;
/**
* OnCaptchaValidationValidateCaptcha constructor.
* @param Translator $translator
*/
public function __construct(Translator $translator)
{
$this->translator = $translator;
}
/**
* @param FormValidationEvent $event
*/
public function validateCaptcha(FormValidationEvent $event)
{
$event
->getValidator()
->addConstraint(
CaptchaValidationRule::class,
[
'data' => $event->getFormData(),
'field' => 'captcha',
'message' => $this->translator->t('captcha', 'invalid_captcha_entered')
]
);
}
}
......@@ -5,4 +5,11 @@ services:
- '@core.acl'
- '@captcha.helpers'
tags:
- { name: core.eventListener, event: captcha.event.display_captcha, method: onDisplayCaptcha }
\ No newline at end of file
- { name: core.eventListener, event: captcha.event.display_captcha, method: onDisplayCaptcha }
captcha.event.on_captcha_validation_validate_captcha_listener:
class: ACP3\Modules\ACP3\Captcha\Event\Listener\OnCaptchaValidationValidateCaptchaListener
arguments:
- '@core.lang'
tags:
- { name: core.eventListener, event: captcha.validation.validate_captcha, method: validateCaptcha }
......@@ -81,11 +81,16 @@ class CaptchaValidationRule extends AbstractValidationRule
return true;
}
protected function checkCaptcha($input, $path)
/**
* @param string $value
* @param string $path
* @return bool
*/
protected function checkCaptcha($value, $path)
{
$index = 'captcha_' . sha1($this->router->route(empty($path) === true ? $this->request->getQuery() : $path));
$indexName = 'captcha_' . sha1($this->router->route(empty($path) === true ? $this->request->getQuery() : $path));
return preg_match('/^[a-zA-Z0-9]+$/', $input)
&& strtolower($input) === strtolower($this->sessionHandler->get($index, ''));
return preg_match('/^[a-zA-Z0-9]+$/', $value)
&& strtolower($value) === strtolower($this->sessionHandler->get($indexName, ''));
}
}
......@@ -2,7 +2,6 @@
namespace ACP3\Modules\ACP3\Comments\Validation;
use ACP3\Core;
use ACP3\Modules\ACP3\Captcha\Validation\ValidationRules\CaptchaValidationRule;
use ACP3\Modules\ACP3\Comments\Validation\ValidationRules\FloodBarrierValidationRule;
/**
......@@ -53,15 +52,9 @@ class FormValidation extends Core\Validation\AbstractFormValidation
'data' => $formData,
'field' => 'message',
'message' => $this->translator->t('system', 'message_to_short')
])
->addConstraint(
CaptchaValidationRule::class,
[
'data' => $formData,
'field' => 'captcha',
'message' => $this->translator->t('captcha', 'invalid_captcha_entered')
]
);
]);
$this->validator->dispatchValidationEvent('captcha.validation.validate_captcha', $formData);
$this->validator->validate();
}
......
......@@ -3,7 +3,6 @@
namespace ACP3\Modules\ACP3\Contact\Validation;
use ACP3\Core;
use ACP3\Modules\ACP3\Captcha\Validation\ValidationRules\CaptchaValidationRule;
/**
* Class FormValidation
......@@ -38,15 +37,10 @@ class FormValidation extends Core\Validation\AbstractFormValidation
'data' => $formData,
'field' => 'message',
'message' => $this->translator->t('system', 'message_to_short')
])
->addConstraint(
CaptchaValidationRule::class,
[
'data' => $formData,
'field' => 'captcha',
'message' => $this->translator->t('captcha', 'invalid_captcha_entered')
]);
$this->validator->dispatchValidationEvent('captcha.validation.validate_captcha', $formData);
$this->validator->validate();
}
}
......@@ -2,7 +2,6 @@
namespace ACP3\Modules\ACP3\Guestbook\Validation;
use ACP3\Core;
use ACP3\Modules\ACP3\Captcha\Validation\ValidationRules\CaptchaValidationRule;
use ACP3\Modules\ACP3\Guestbook\Validation\ValidationRules\FloodBarrierValidationRule;
use ACP3\Modules\ACP3\Newsletter;
......@@ -71,13 +70,6 @@ class FormValidation extends Core\Validation\AbstractFormValidation
'data' => $formData,
'field' => 'message',
'message' => $this->translator->t('system', 'message_to_short')
])
->addConstraint(
CaptchaValidationRule::class,
[
'data' => $formData,
'field' => 'captcha',
'message' => $this->translator->t('captcha', 'invalid_captcha_entered')
]);
if (!empty($formData['mail'])) {
......@@ -111,6 +103,8 @@ class FormValidation extends Core\Validation\AbstractFormValidation
);
}
$this->validator->dispatchValidationEvent('captcha.validation.validate_captcha', $formData);
$this->validator->validate();
}
}
......@@ -7,45 +7,20 @@
namespace ACP3\Modules\ACP3\Newsletter\Test\Validation;
use ACP3\Core\ACL;
use ACP3\Core\Http\Request;
use ACP3\Core\Router\Router;
use ACP3\Core\Session\SessionHandler;
use ACP3\Core\Session\SessionHandlerInterface;
use ACP3\Core\Test\Validation\AbstractFormValidationTest;
use ACP3\Core\Validation\ValidationRules\EmailValidationRule;
use ACP3\Core\Validation\ValidationRules\InArrayValidationRule;
use ACP3\Modules\ACP3\Captcha\Validation\ValidationRules\CaptchaValidationRule;
use ACP3\Modules\ACP3\Newsletter\Model\Repository\AccountRepository;
use ACP3\Modules\ACP3\Newsletter\Validation\SubscribeFormValidation;
use ACP3\Modules\ACP3\Newsletter\Validation\ValidationRules\AccountNotExistsValidationRule;
use ACP3\Modules\ACP3\Users\Model\UserModel;
class SubscribeFormValidationTest extends AbstractFormValidationTest
{
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $aclMock;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $accountRepositoryMock;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $requestMock;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $routerMock;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $sessionHandlerMock;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $userModelMock;
/**
* @return void
......@@ -76,31 +51,6 @@ class SubscribeFormValidationTest extends AbstractFormValidationTest
->getMock();
$accountNotExistsRule = new AccountNotExistsValidationRule($this->accountRepositoryMock);
$this->validator->registerValidationRule($accountNotExistsRule);
$this->aclMock = $this->getMockBuilder(ACL::class)
->disableOriginalConstructor()
->getMock();
$this->requestMock = $this->getMockBuilder(Request::class)
->disableOriginalConstructor()
->getMock();
$this->routerMock = $this->getMockBuilder(Router::class)
->disableOriginalConstructor()
->getMock();
$this->sessionHandlerMock = $this->getMockBuilder(SessionHandler::class)
->disableOriginalConstructor()
->getMock();
$this->userModelMock = $this->getMockBuilder(UserModel::class)
->disableOriginalConstructor()
->getMock();
$captchaRule = new CaptchaValidationRule(
$this->aclMock,
$this->requestMock,
$this->routerMock,
$this->sessionHandlerMock,
$this->userModelMock
);
$this->validator->registerValidationRule($captchaRule);
}
/**
......@@ -110,21 +60,21 @@ class SubscribeFormValidationTest extends AbstractFormValidationTest
{
return [
[
\ACP3\Core\Session\SessionHandlerInterface::XSRF_TOKEN_NAME => self::XSRF_FORM_TOKEN,
SessionHandlerInterface::XSRF_TOKEN_NAME => self::XSRF_FORM_TOKEN,
'mail' => 'test@example.com',
'salutation' => '',
'first_name' => 'Foo',
'last_name' => 'bar'
],
[
\ACP3\Core\Session\SessionHandlerInterface::XSRF_TOKEN_NAME => self::XSRF_FORM_TOKEN,
SessionHandlerInterface::XSRF_TOKEN_NAME => self::XSRF_FORM_TOKEN,
'mail' => 'test@example.com',
'salutation' => '',
'first_name' => '',
'last_name' => ''
],
[
\ACP3\Core\Session\SessionHandlerInterface::XSRF_TOKEN_NAME => self::XSRF_FORM_TOKEN,
SessionHandlerInterface::XSRF_TOKEN_NAME => self::XSRF_FORM_TOKEN,
'mail' => 'test@example.com',
'salutation' => '',
'first_name' => '',
......@@ -141,14 +91,14 @@ class SubscribeFormValidationTest extends AbstractFormValidationTest
{
return [
[
\ACP3\Core\Session\SessionHandlerInterface::XSRF_TOKEN_NAME => self::XSRF_FORM_TOKEN,
SessionHandlerInterface::XSRF_TOKEN_NAME => self::XSRF_FORM_TOKEN,
'mail' => 'testexample.com',
'salutation' => '',
'first_name' => 'Foo',
'last_name' => 'bar'
],
[
\ACP3\Core\Session\SessionHandlerInterface::XSRF_TOKEN_NAME => self::XSRF_FORM_TOKEN,
SessionHandlerInterface::XSRF_TOKEN_NAME => self::XSRF_FORM_TOKEN,
'mail' => 'test@example.com',
'salutation' => 3,
'first_name' => '',
......
......@@ -3,7 +3,6 @@ namespace ACP3\Modules\ACP3\Newsletter\Validation;
use ACP3\Core;
use ACP3\Core\Validation\AbstractFormValidation;
use ACP3\Modules\ACP3\Captcha\Validation\ValidationRules\CaptchaValidationRule;
use ACP3\Modules\ACP3\Newsletter\Validation\ValidationRules\AccountNotExistsValidationRule;
/**
......@@ -42,15 +41,10 @@ class SubscribeFormValidation extends AbstractFormValidation
'data' => $formData,
'field' => 'mail',
'message' => $this->translator->t('newsletter', 'account_exists')
])
->addConstraint(
CaptchaValidationRule::class,
[
'data' => $formData,
'field' => 'captcha',
'message' => $this->translator->t('captcha', 'invalid_captcha_entered')
]);
$this->validator->dispatchValidationEvent('captcha.validation.validate_captcha', $formData);
$this->validator->validate();
}
}
......@@ -3,7 +3,6 @@ namespace ACP3\Modules\ACP3\Newsletter\Validation;
use ACP3\Core;
use ACP3\Core\Validation\AbstractFormValidation;
use ACP3\Modules\ACP3\Captcha\Validation\ValidationRules\CaptchaValidationRule;
use ACP3\Modules\ACP3\Newsletter\Validation\ValidationRules\AccountExistsValidationRule;
/**
......@@ -33,15 +32,9 @@ class UnsubscribeFormValidation extends AbstractFormValidation
'data' => $formData,
'field' => 'mail',
'message' => $this->translator->t('newsletter', 'account_not_exists')
])
->addConstraint(
CaptchaValidationRule::class,
[
'data' => $formData,
'field' => 'captcha',
'message' => $this->translator->t('captcha', 'invalid_captcha_entered')
]);
$this->validator->dispatchValidationEvent('captcha.validation.validate_captcha', $formData);
$this->validator->validate();
}
......
......@@ -3,7 +3,6 @@ namespace ACP3\Modules\ACP3\Users\Validation;
use ACP3\Core;
use ACP3\Core\Validation\AbstractFormValidation;
use ACP3\Modules\ACP3\Captcha\Validation\ValidationRules\CaptchaValidationRule;
use ACP3\Modules\ACP3\Users\Validation\ValidationRules\AccountNotExistsByEmailValidationRule;
use ACP3\Modules\ACP3\Users\Validation\ValidationRules\AccountNotExistsByNameValidationRule;
......@@ -33,13 +32,6 @@ class AccountForgotPasswordFormValidation extends AbstractFormValidation
'field' => 'nick_mail',
'message' => $this->translator->t('users', 'type_in_nickname_or_email')
])
->addConstraint(
CaptchaValidationRule::class,
[
'data' => $formData,
'field' => 'captcha',
'message' => $this->translator->t('captcha', 'invalid_captcha_entered')
])
->addConstraint(
$ruleName,
[
......@@ -48,6 +40,8 @@ class AccountForgotPasswordFormValidation extends AbstractFormValidation
'message' => $this->translator->t('users', 'user_not_exists')
]);
$this->validator->dispatchValidationEvent('captcha.validation.validate_captcha', $formData);
$this->validator->validate();
}
}
......@@ -2,7 +2,6 @@
namespace ACP3\Modules\ACP3\Users\Validation;
use ACP3\Core;
use ACP3\Modules\ACP3\Captcha\Validation\ValidationRules\CaptchaValidationRule;
use ACP3\Modules\ACP3\Users\Validation\ValidationRules\AccountNotExistsByEmailValidationRule;
use ACP3\Modules\ACP3\Users\Validation\ValidationRules\AccountNotExistsByNameValidationRule;
......@@ -46,17 +45,12 @@ class RegistrationFormValidation extends AbstractUserFormValidation
'data' => $formData,
'field' => 'mail',
'message' => $this->translator->t('users', 'user_email_already_exists')
])
->addConstraint(
CaptchaValidationRule::class,
[
'data' => $formData,
'field' => 'captcha',
'message' => $this->translator->t('captcha', 'invalid_captcha_entered')
]);
$this->validatePassword($formData, 'pwd', 'pwd_repeat');
$this->validator->dispatchValidationEvent('captcha.validation.validate_captcha', $formData);
$this->validator->validate();
}
}
......@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added
### Changed
- The captcha validation is now handled by the new event `captcha.validation.validate_captcha`
### Fixed
- fixed the PrefixUri class when there is an URL given with a valid protocol
......
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