fix: if no spam protector set, fail sliently

This commit is contained in:
Will Rossiter 2023-09-11 11:42:09 +12:00
parent 2d59501ef3
commit 17106d8f30
No known key found for this signature in database
GPG Key ID: 9CD50F87382CDF52
9 changed files with 46 additions and 45 deletions

4
.gitignore vendored
View File

@ -1 +1,5 @@
.DS_Store
/vendor
/composer.lock
/public
.phpunit.result.cache

View File

@ -9,14 +9,6 @@
composer require silverstripe/spamprotection
```
## Maintainer Contact
* Saophalkun Ponlu
<phalkunz (at) silverstripe (dot) com>
* Will Rossiter
<will (at) fullscreen (dot) io>
## Documentation
This module provides a generic, consistent API for adding spam protection to
@ -40,7 +32,7 @@ need to rebuild your database through `dev/build` and set the default protector
via SilverStripe's config system. This will update any Form instances that have
spam protection hooks with that protector.
*mysite/_config/spamprotection.yml*
*app/_config/spamprotection.yml*
```yaml
---
@ -67,10 +59,10 @@ implementation client side or server side.
`enableSpamProtection` takes a hash of optional configuration values.
```php
$form->enableSpamProtection(array(
$form->enableSpamProtection([
'protector' => MathSpamProtector::class,
'name' => 'Captcha'
));
]);
```
Options to configure are:
@ -121,6 +113,7 @@ class CustomSpamProtector implements SpamProtector
}
```
## Using Spam Protection with User Forms
This module provides an `EditableSpamProtectionField` wrapper which you can add

View File

@ -6,16 +6,6 @@
"silverstripe",
"spamprotection"
],
"authors": [
{
"name": "Saophalkun Ponlu",
"email": "phalkunz@silverstripe.com"
},
{
"name": "Will Rossiter",
"email": "will@fullscreen.io"
}
],
"require": {
"php": "^8.1",
"silverstripe/framework": "^5"
@ -33,11 +23,17 @@
},
"autoload": {
"psr-4": {
"SilverStripe\\SpamProtection\\": "code/",
"SilverStripe\\SpamProtection\\": "src/",
"SilverStripe\\SpamProtection\\Tests\\": "tests/"
}
},
"license": "BSD-3-Clause",
"minimum-stability": "dev",
"prefer-stable": true
"prefer-stable": true,
"config": {
"allow-plugins": {
"composer/installers": true,
"silverstripe/vendor-plugin": true
}
}
}

View File

@ -2,7 +2,7 @@
<ruleset name="SilverStripe">
<description>CodeSniffer ruleset for SilverStripe coding conventions.</description>
<file>code</file>
<file>src</file>
<file>tests</file>
<rule ref="PSR2" >

View File

@ -8,7 +8,7 @@
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">code/</directory>
<directory suffix=".php">src/</directory>
<exclude>
<directory suffix=".php">tests/</directory>
</exclude>

View File

@ -3,7 +3,6 @@
namespace SilverStripe\SpamProtection;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Convert;
use SilverStripe\Core\Manifest\ModuleLoader;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldGroup;
@ -41,15 +40,15 @@ class EditableSpamProtectionField extends EditableFormField
* @var array
* @config
*/
private static $check_fields = array(
private static $check_fields = [
EditableEmailField::class,
EditableTextField::class,
EditableNumericField::class
);
];
private static $db = array(
private static $db = [
'SpamFieldSettings' => 'Text'
);
];
/**
* @var FormField
@ -69,12 +68,14 @@ class EditableSpamProtectionField extends EditableFormField
}
// Extract saved field mappings and update this field.
$fieldMapping = array();
$fieldMapping = [];
foreach ($this->getCandidateFields() as $otherField) {
$mapSetting = "Map-{$otherField->Name}";
$spamField = $this->spamMapValue($mapSetting);
$fieldMapping[$otherField->Name] = $spamField;
}
$protector->setFieldMapping($fieldMapping);
// Generate field
@ -106,7 +107,8 @@ class EditableSpamProtectionField extends EditableFormField
// Get list of all configured classes available for spam detection
$types = $this->config()->get('check_fields');
$typesInherit = array();
$typesInherit = [];
foreach ($types as $type) {
$subTypes = ClassInfo::subclassesFor($type);
$typesInherit = array_merge($typesInherit, $subTypes);
@ -131,8 +133,9 @@ class EditableSpamProtectionField extends EditableFormField
public function onBeforeWrite()
{
$fieldMap = json_decode($this->SpamFieldSettings ?? '', true);
if (empty($fieldMap)) {
$fieldMap = array();
$fieldMap = [];
}
foreach ($this->record as $key => $value) {
@ -202,7 +205,7 @@ class EditableSpamProtectionField extends EditableFormField
{
$map = json_decode($this->SpamFieldSettings ?? '', true);
if (empty($map)) {
$map = array();
$map = [];
}
if (array_key_exists($mapSetting, $map ?? [])) {
@ -255,16 +258,19 @@ class EditableSpamProtectionField extends EditableFormField
}
}
public function getFieldValidationOptions()
{
return FieldList::create();
}
public function getRequired()
{
return false;
}
public function getIcon()
{
$resource = ModuleLoader::getModule('silverstripe/spamprotection')

View File

@ -14,19 +14,19 @@ class CommentSpamProtection extends Extension
{
public function alterCommentForm(&$form)
{
$form->enableSpamProtection(array(
$form->enableSpamProtection([
'name' => 'IsSpam',
'mapping' => array(
'mapping' => [
'Name' => 'authorName',
'Email' => 'authorEmail',
'URL' => 'authorUrl',
'Comment' => 'body',
'ReturnURL' => 'contextUrl'
),
'checks' => array(
],
'checks' => [
'spam',
'profanity'
)
));
]
]);
}
}

View File

@ -6,6 +6,7 @@ use LogicException;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Extension;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\View\Requirements;
/**
* An extension to the {@link Form} class which provides the method
@ -37,7 +38,7 @@ class FormSpamProtectionExtension extends Extension
*
* @var array $mappable_fields
*/
private static $mappable_fields = array(
private static $mappable_fields = [
'id',
'title',
'body',
@ -48,7 +49,7 @@ class FormSpamProtectionExtension extends Extension
'authorUrl',
'authorIp',
'authorId'
);
];
/**
* @config
@ -88,7 +89,7 @@ class FormSpamProtectionExtension extends Extension
* @throws LogicException when get_protector method returns NULL.
* @return Object
*/
public function enableSpamProtection($options = array())
public function enableSpamProtection($options = [])
{
// captcha form field name (must be unique)
@ -109,7 +110,8 @@ class FormSpamProtectionExtension extends Extension
$protector = self::get_protector($options);
if ($protector === null) {
throw new LogicException('No spam protector has been set. Null is not valid value.');
Requirements::customScript('console.error("No spam protector has been set on this form.")');
return $this->owner;
}
if ($protector && isset($options['mapping'])) {