API Enable spam protectors to receive usercode configuration

This commit is contained in:
Damian Mooyman 2015-12-11 14:29:09 +13:00
parent 078b0f51f9
commit 68e0a6d16a
5 changed files with 77 additions and 32 deletions

View File

@ -66,25 +66,26 @@ implementation client side or server side.
Options to configure are: Options to configure are:
*`protector`* a class name string or class instance which implements * `protector`: a class name string or class instance which implements `SpamProtector`. Defaults to your
`SpamProtector`. Defaults to your
`FormSpamProtectionExtension.default_spam_protector` value. `FormSpamProtectionExtension.default_spam_protector` value.
* `name`: The form field name argument for the Captcha. Defaults to `Catcha`.
* `title`: title of the Captcha form field. Defaults to `''`
* `insertBefore`: name of existing field to insert the spam protection field prior to
* `mapping`: an array mapping of the Form fields to the standardized list of field names.
The list of standardized fields to pass to the spam protector are:
* title
* body
* contextUrl
* contextTitle
* authorName
* authorMail
* authorUrl
* authorIp
* authorId
*`name`* the form field name argument for the Captcha. Defaults to `Catcha`. Additional options may be specified, which may be used to activate implementation specific
*`title`* title of the Captcha form field. Defaults to `''` features for the chosen spam protector. All of these options will be passed to the
*`insertBefore`* name of existing field to insert the spam protection field prior to protector constructor (see below).
*`mapping`* an array mapping of the Form fields to the standardized list of
field names. The list of standardized fields to pass to the spam protector are:
title
body
contextUrl
contextTitle
authorName
authorMail
authorUrl
authorIp
authorId
## Defining your own `SpamProtector` ## Defining your own `SpamProtector`
@ -93,16 +94,32 @@ be set as the spam protector. The `getFormField()` method returns the
`FormField` to be inserted into the `Form`. The `FormField` returned should be `FormField` to be inserted into the `Form`. The `FormField` returned should be
in charge of the validation process. in charge of the validation process.
<?php <?php
class CustomSpamProtector implements SpamProtector { class CustomSpamProtector implements SpamProtector
{
public function getFormField($name = null, $title = null, $value = null) { /**
// CaptchaField is a imagined class which has some functionality. * List of options passed to enableSpamProtection() used to generate this protector
// See silverstripe-mollom module for an example. * @var array
return new CaptchaField($name, $title, $value); */
} protected $options = array();
}
public function __construct($options = array())
{
$this->options = $options;
}
public function getFormField($name = null, $title = null, $value = null)
{
// CaptchaField is a imagined class which has some functionality.
// See silverstripe-mollom module for an example.
return new CaptchaField($name, $title, $value);
}
public function setFieldMapping($fieldMapping) {
// No-op
}
}
## Using Spam Protection with User Forms ## Using Spam Protection with User Forms

View File

@ -57,7 +57,7 @@ class FormSpamProtectionExtension extends Extension
} }
if ($protector && class_exists($protector)) { if ($protector && class_exists($protector)) {
return Injector::inst()->create($protector); return Injector::inst()->createWithArgs($protector, array($options));
} else { } else {
return null; return null;
} }

View File

@ -14,6 +14,12 @@
interface SpamProtector interface SpamProtector
{ {
/**
* SpamProtector constructor.
* @param array $options List of spam protection options
*/
public function __construct($options = array());
/** /**
* Return the {@link FormField} associated with this protector. * Return the {@link FormField} associated with this protector.
* *

View File

@ -18,7 +18,7 @@
}, },
"extra": { "extra": {
"branch-alias": { "branch-alias": {
"dev-master": "2.0.x-dev" "dev-master": "3.0.x-dev"
} }
}, },
"license": "BSD-3-Clause" "license": "BSD-3-Clause"

View File

@ -61,10 +61,13 @@ class FormSpamProtectionExtensionTest extends SapphireTest
$form = $this->form->enableSpamProtection(array( $form = $this->form->enableSpamProtection(array(
'protector' => 'FormSpamProtectionExtensionTest_BazProtector', 'protector' => 'FormSpamProtectionExtensionTest_BazProtector',
'title' => 'Qux', 'title' => 'Qux',
'name' => 'Borris' 'name' => 'Borris',
'righttitle' => 'Lipsum'
)); ));
$this->assertEquals('Qux', $form->Fields()->fieldByName('Borris')->Title()); $formfield = $form->Fields()->fieldByName('Borris');
$this->assertEquals('Qux', $formfield->Title());
$this->assertEquals('Lipsum', $formfield->RightTitle());
} }
public function testInsertBefore() public function testInsertBefore()
@ -102,9 +105,20 @@ class FormSpamProtectionExtensionTest extends SapphireTest
*/ */
class FormSpamProtectionExtensionTest_BazProtector implements SpamProtector, TestOnly class FormSpamProtectionExtensionTest_BazProtector implements SpamProtector, TestOnly
{ {
protected $options = array();
public function __construct($options = array())
{
$this->options = $options;
}
public function getFormField($name = null, $title = null, $value = null) public function getFormField($name = null, $title = null, $value = null)
{ {
return new TextField($name, $title, $value); $field = new TextField($name, $title, $value);
if(isset($this->options['righttitle'])) {
$field->setRightTitle($this->options['righttitle']);
}
return $field;
} }
public function setFieldMapping($fieldMapping) public function setFieldMapping($fieldMapping)
@ -117,6 +131,10 @@ class FormSpamProtectionExtensionTest_BazProtector implements SpamProtector, Tes
*/ */
class FormSpamProtectionExtensionTest_BarProtector implements SpamProtector, TestOnly class FormSpamProtectionExtensionTest_BarProtector implements SpamProtector, TestOnly
{ {
public function __construct($options = array())
{
}
public function getFormField($name = null, $title = null, $value = null) public function getFormField($name = null, $title = null, $value = null)
{ {
$title = $title ?: 'Bar'; $title = $title ?: 'Bar';
@ -133,6 +151,10 @@ class FormSpamProtectionExtensionTest_BarProtector implements SpamProtector, Tes
*/ */
class FormSpamProtectionExtensionTest_FooProtector implements SpamProtector, TestOnly class FormSpamProtectionExtensionTest_FooProtector implements SpamProtector, TestOnly
{ {
public function __construct($options = array())
{
}
public function getFormField($name = null, $title = null, $value = null) public function getFormField($name = null, $title = null, $value = null)
{ {
return new TextField($name, 'Foo', $value); return new TextField($name, 'Foo', $value);