Revert "fix: if no spam protector set, fail sliently"

This reverts commit 17106d8f30.
This commit is contained in:
Guy Sartorelli 2023-09-12 09:45:39 +12:00
parent 17106d8f30
commit 221089136f
No known key found for this signature in database
GPG Key ID: F313E3B9504D496A
9 changed files with 45 additions and 46 deletions

4
.gitignore vendored
View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -6,6 +6,16 @@
"silverstripe", "silverstripe",
"spamprotection" "spamprotection"
], ],
"authors": [
{
"name": "Saophalkun Ponlu",
"email": "phalkunz@silverstripe.com"
},
{
"name": "Will Rossiter",
"email": "will@fullscreen.io"
}
],
"require": { "require": {
"php": "^8.1", "php": "^8.1",
"silverstripe/framework": "^5" "silverstripe/framework": "^5"
@ -23,17 +33,11 @@
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"SilverStripe\\SpamProtection\\": "src/", "SilverStripe\\SpamProtection\\": "code/",
"SilverStripe\\SpamProtection\\Tests\\": "tests/" "SilverStripe\\SpamProtection\\Tests\\": "tests/"
} }
}, },
"license": "BSD-3-Clause", "license": "BSD-3-Clause",
"minimum-stability": "dev", "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"> <ruleset name="SilverStripe">
<description>CodeSniffer ruleset for SilverStripe coding conventions.</description> <description>CodeSniffer ruleset for SilverStripe coding conventions.</description>
<file>src</file> <file>code</file>
<file>tests</file> <file>tests</file>
<rule ref="PSR2" > <rule ref="PSR2" >

View File

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