Add bypass_code configuration parameter to force captcha validation for development/automation.

This commit is contained in:
Jeremy Livingston 2012-06-30 12:18:05 -04:00
parent 9980108880
commit f9885acde9
4 changed files with 26 additions and 7 deletions

View File

@ -32,6 +32,7 @@ class Configuration implements ConfigurationInterface
->scalarNode('expiration')->defaultValue(60)->end() ->scalarNode('expiration')->defaultValue(60)->end()
->scalarNode('quality')->defaultValue(15)->end() ->scalarNode('quality')->defaultValue(15)->end()
->scalarNode('invalid_message')->defaultValue('Bad code value')->end() ->scalarNode('invalid_message')->defaultValue('Bad code value')->end()
->scalarNode('bypass_code')->defaultValue(null)->end()
->end() ->end()
; ;
return $treeBuilder; return $treeBuilder;

View File

@ -127,6 +127,7 @@ You can define the following type option :
* **gc_freq**: frequency of garbage collection in fractions of 1 (default=100) * **gc_freq**: frequency of garbage collection in fractions of 1 (default=100)
* **expiration**: maximum lifetime of captcha image files in minutes (default=60) * **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") * **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 : Example :

View File

@ -47,7 +47,10 @@ class CaptchaType extends AbstractType
$this->key = $builder->getForm()->getName(); $this->key = $builder->getForm()->getName();
$builder->addValidator( $builder->addValidator(
new CaptchaValidator($this->session, $this->key, $options['invalid_message']) new CaptchaValidator($this->session,
$this->key,
$options['invalid_message'],
$options['bypass_code'])
); );
} }

View File

@ -29,20 +29,26 @@ class CaptchaValidator implements FormValidatorInterface
*/ */
private $invalidMessage; 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->session = $session;
$this->key = $key; $this->key = $key;
$this->invalidMessage = $invalidMessage; $this->invalidMessage = $invalidMessage;
$this->bypassCode = $bypassCode;
} }
public function validate(FormInterface $form) public function validate(FormInterface $form)
{ {
$code = $form->getData(); $code = $form->getData();
$excepted_code = $this->getExceptedCode(); $expectedCode = $this->getExpectedCode();
if (!($code && $excepted_code && is_string($code) && is_string($excepted_code) if (!($code && is_string($code)
&& $this->niceize($code) == $this->niceize($excepted_code))) { && ($this->compare($code, $expectedCode) || $this->compare($code, $this->bypassCode)))) {
$form->addError(new FormError($this->invalidMessage)); $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)) { if ($this->session->has($this->key)) {
return $this->session->get($this->key); return $this->session->get($this->key);
@ -71,4 +77,12 @@ class CaptchaValidator implements FormValidatorInterface
{ {
return strtr(strtolower($code), 'oil', '01l'); 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));
}
} }