Skip to content

Commit

Permalink
Excluded GraphQL controller action explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Jun 26, 2021
1 parent e7584e8 commit d7a9537
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 3.0.7 - Unreleased
### Changed
- Excluded GraphQL requests that do not use `application/graphql` as their MIME type from being validated ([#17](https://github.com/putyourlightson/craft-snaptcha/issues/17)).

## 3.0.6 - 2021-06-03
### Fixed
- Fixed a bug in which a migration was not run when updating from v2 to v3 ([#15](https://github.com/putyourlightson/craft-snaptcha/issues/15)).
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "putyourlightson/craft-snaptcha",
"description": "Automatically validates forms and prevents spam bots from submitting to your site.",
"version": "3.0.6",
"version": "3.0.7",
"type": "craft-plugin",
"homepage": "https://putyourlightson.com/plugins/snaptcha",
"license": "proprietary",
Expand Down
2 changes: 1 addition & 1 deletion src/Snaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function validateField(ActionEvent $event)
|| $request->getIsPreview()
|| $request->getMethod() !== 'POST'
|| $request->getFullPath() == Craft::$app->getConfig()->getGeneral()->getSetPasswordPath()
|| $this->snaptcha->isExcludedControllerAction()
|| $this->snaptcha->isExcludedControllerAction($event->action)
) {
return;
}
Expand Down
13 changes: 10 additions & 3 deletions src/services/SnaptchaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@
use putyourlightson\snaptcha\models\SnaptchaModel;
use putyourlightson\snaptcha\records\SnaptchaRecord;
use putyourlightson\snaptcha\Snaptcha;
use yii\base\Action;
use yii\base\Event;

/**
*
* @property-read array $postedValues
*/
class SnaptchaService extends Component
{
/**
Expand All @@ -37,8 +42,9 @@ class SnaptchaService extends Component
*/
const EXCLUDE_CONTROLLER_ACTIONS = [
'commerce/webhooks/process-webhook',
'cookie-consent/consent/update',
'complete-cookie-consent/consent/submit',
'cookie-consent/consent/update',
'graphql/api',
];

/**
Expand Down Expand Up @@ -81,15 +87,16 @@ public function getPostedValues(): array
/**
* Returns whether the controller action is excluded from validation.
*
* @param Action $action
* @return bool
*/
public function isExcludedControllerAction(): bool
public function isExcludedControllerAction(Action $action): bool
{
if (!Craft::$app->getRequest()->getIsActionRequest()) {
return false;
}

$controllerAction = implode('/', Craft::$app->getRequest()->getActionSegments());
$controllerAction = $action->getUniqueId();

// Fire a before event
$event = new ValidateFieldEvent(['excludeControllerActions' => self::EXCLUDE_CONTROLLER_ACTIONS]);
Expand Down

0 comments on commit d7a9537

Please sign in to comment.