Skip to content

Commit

Permalink
Added Validate Users setting
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Croker committed Apr 2, 2021
1 parent ac5c901 commit 4493a18
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 14 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
### Added
- Added a customisable error screen that allows users to resubmit their data if a submission is flagged as spam.
- Added an “Exclude Controller Actions” setting.
- Added a “Validate Users” setting that lets you disable validation for logged-in users.
- Added an “Exclude Controller Actions” setting that lets you disable validation for specific controller actions.
- Added an “Allow List” of IP addresses that will not be validated.
- Added unit tests.

### Changed
- Changed the minimum requirement of Craft to version 3.2.1.
- Renamed “Blacklist” to “Deny List”, a list of IP addresses that will always be denied.
- Renamed “Blacklist” to “Deny List”.
- The hidden input field now has an `autocomplete` attribute set to `off`.
- IP addresses are now stored as anonymous hash values.
- IP addresses are now stored as anonymous hashed values.

### Removed
- Removed the “Excluded URI Patterns” setting. Use the new “Exclude Controller Actions” setting instead.
Expand Down
9 changes: 5 additions & 4 deletions codeception.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ modules:
config:
\craft\test\Craft:
configFile: 'tests/_craft/config/test.php'
entryUrl: 'http://craft3/index.php'
entryUrl: 'http://craft.nitro/index.php'
entryScript: 'index.php'
projectConfig: {}
migrations: []
plugins:
sprig:
snaptcha:
class: '\putyourlightson\snaptcha\Snaptcha'
handle: snaptcha
# cleanup: true
cleanup: true
transaction: false
# dbSetup: {clean: true, setupCraft: true}
dbSetup: {clean: true, setupCraft: true}
fullMock: false
3 changes: 3 additions & 0 deletions src/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
// Whether form submissions should be validated. Ensure that all of your forms that submit via POST requests have the necessary tags in place before enabling this.
//'validationEnabled' => false,

// Whether form submissions should be validated for logged-in users (recommended if public user registration is enabled).
//'validateUsers' => true,

// Whether form submissions should be limited to one time per page refresh (recommended for low to medium traffic sites).
//'oneTimeKey' => true,

Expand Down
5 changes: 5 additions & 0 deletions src/models/SettingsModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ class SettingsModel extends Model
*/
public $validationEnabled = false;

/**
* @var bool
*/
public $validateUsers = true;

/**
* @var bool
*/
Expand Down
15 changes: 15 additions & 0 deletions src/services/SnaptchaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ public function isExcludedControllerAction(): bool
return false;
}

/**
* Returns whether the user is allowed.
*
* @return bool
*/
public function isUserAllowed(): bool
{
return !Snaptcha::$plugin->settings->validateUsers && Craft::$app->getUser()->getIsGuest() === false;
}

/**
* Returns whether the IP address is allowed.
*
Expand Down Expand Up @@ -164,6 +174,11 @@ public function validateField(string $value = null): bool
return false;
}

// Check if user is allowed
if ($this->isUserAllowed()) {
return true;
}

// Check if IP address is allowed
if ($this->isIpAllowed()) {
return true;
Expand Down
10 changes: 10 additions & 0 deletions src/templates/_settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@
errors: settings.getErrors('validationEnabled')
}) }}

{{ forms.lightswitchField({
required: true,
label: 'Validate Users'|t('snaptcha'),
name: 'validateUsers',
instructions: 'Whether form submissions should be validated for logged-in users (recommended if public user registration is enabled).'|t('snaptcha'),
warning: (config.validateUsers is defined ? configWarning('validateUsers')),
on: settings.validateUsers,
errors: settings.getErrors('validateUsers')
}) }}

{{ forms.lightswitchField({
required: true,
label: 'One Time Key'|t('snaptcha'),
Expand Down
8 changes: 4 additions & 4 deletions tests/.env
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Set in accordance to your environment
DB_DSN="mysql:host=127.0.0.1;port=3306;dbname=craft-test"
DB_USER="root"
DB_PASSWORD="root"
DB_DSN="mysql:host=mysql-8.0-3307.database.nitro;port=3306;dbname=craft-test"
DB_USER="nitro"
DB_PASSWORD="nitro"

# Set this to the `entryUrl` param in the `codeception.yml` file.
DEFAULT_SITE_URL="http://craft3/index.php"
DEFAULT_SITE_URL="http://craft/index.php"
4 changes: 2 additions & 2 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ To run static analysis on the plugin, install PHPStan and run the following comm

To test the plugin, install Codeception, update `.env` and run the following command from the root of your project.

codecept run -c ./vendor/putyourlightson/craft-sherlock
./vendor/bin/codecept run -c ./vendor/putyourlightson/craft-sherlock

Or to run a specific test.

codecept run -c ./vendor/putyourlightson/craft-sherlock unit variables/SnaptchaVariableTest:getField
./vendor/bin/codecept run -c ./vendor/putyourlightson/craft-sherlock unit variables/SnaptchaVariableTest:getField

> Ensure that the database you specify in `.env` is not one that actually contains any data as it will be cleared when the tests are run.
2 changes: 1 addition & 1 deletion tests/_bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//define('CRAFT_VENDOR_PATH', dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor');

// Use absolute path if the plugin directory is a symlink
define('CRAFT_VENDOR_PATH', '/Users/ben/Sites/craft3/vendor');
define('CRAFT_VENDOR_PATH', '/app/vendor');

$devMode = true;

Expand Down

0 comments on commit 4493a18

Please sign in to comment.