Merge pull request #27 from jeremylivingston/bypass-code
Add bypass_code configuration parameter
This commit is contained in:
commit
589e8f0bcb
@ -32,6 +32,7 @@ class Configuration implements ConfigurationInterface
|
||||
->scalarNode('expiration')->defaultValue(60)->end()
|
||||
->scalarNode('quality')->defaultValue(15)->end()
|
||||
->scalarNode('invalid_message')->defaultValue('Bad code value')->end()
|
||||
->scalarNode('bypass_code')->defaultValue(null)->end()
|
||||
->end()
|
||||
;
|
||||
return $treeBuilder;
|
||||
|
@ -127,6 +127,7 @@ You can define the following type option :
|
||||
* **gc_freq**: frequency of garbage collection in fractions of 1 (default=100)
|
||||
* **expiration**: maximum lifetime of captcha image files in minutes (default=60)
|
||||
* **invalid_message**: error message displayed when an non-matching code is submitted (default="Bad code value")
|
||||
* **bypass_code**: code that will always validate the captcha (default=null)
|
||||
|
||||
Example :
|
||||
|
||||
|
@ -47,7 +47,10 @@ class CaptchaType extends AbstractType
|
||||
$this->key = $builder->getForm()->getName();
|
||||
|
||||
$builder->addValidator(
|
||||
new CaptchaValidator($this->session, $this->key, $options['invalid_message'])
|
||||
new CaptchaValidator($this->session,
|
||||
$this->key,
|
||||
$options['invalid_message'],
|
||||
$options['bypass_code'])
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -29,20 +29,26 @@ class CaptchaValidator implements FormValidatorInterface
|
||||
*/
|
||||
private $invalidMessage;
|
||||
|
||||
public function __construct(Session $session, $key, $invalidMessage)
|
||||
/**
|
||||
* Configuration parameter used to bypass a required code match
|
||||
*/
|
||||
private $bypassCode;
|
||||
|
||||
public function __construct(Session $session, $key, $invalidMessage, $bypassCode)
|
||||
{
|
||||
$this->session = $session;
|
||||
$this->key = $key;
|
||||
$this->invalidMessage = $invalidMessage;
|
||||
$this->bypassCode = $bypassCode;
|
||||
}
|
||||
|
||||
public function validate(FormInterface $form)
|
||||
{
|
||||
$code = $form->getData();
|
||||
$excepted_code = $this->getExceptedCode();
|
||||
$expectedCode = $this->getExpectedCode();
|
||||
|
||||
if (!($code && $excepted_code && is_string($code) && is_string($excepted_code)
|
||||
&& $this->niceize($code) == $this->niceize($excepted_code))) {
|
||||
if (!($code && is_string($code)
|
||||
&& ($this->compare($code, $expectedCode) || $this->compare($code, $this->bypassCode)))) {
|
||||
$form->addError(new FormError($this->invalidMessage));
|
||||
}
|
||||
|
||||
@ -54,9 +60,9 @@ class CaptchaValidator implements FormValidatorInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the excepted CAPTCHA code
|
||||
* Retrieve the expected CAPTCHA code
|
||||
*/
|
||||
private function getExceptedCode()
|
||||
private function getExpectedCode()
|
||||
{
|
||||
if ($this->session->has($this->key)) {
|
||||
return $this->session->get($this->key);
|
||||
@ -71,4 +77,12 @@ class CaptchaValidator implements FormValidatorInterface
|
||||
{
|
||||
return strtr(strtolower($code), 'oil', '01l');
|
||||
}
|
||||
|
||||
/**
|
||||
* Run a match comparison on the provided code and the expected code
|
||||
*/
|
||||
private function compare($code, $expectedCode)
|
||||
{
|
||||
return ($expectedCode && is_string($expectedCode) && $this->niceize($code) == $this->niceize($expectedCode));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user