diff --git a/composer.json b/composer.json index 6be24a3..67481ae 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,7 @@ "name": "Captcha", "slug": "captcha", "description": "Protect your Statamic forms using a Captcha service", - "version": "1.4.0" + "version": "1.5.0" }, "laravel": { "providers": [ diff --git a/config/captcha.php b/config/captcha.php index 453a1e3..07bc2cd 100644 --- a/config/captcha.php +++ b/config/captcha.php @@ -6,6 +6,7 @@ 'secret' => env('CAPTCHA_SECRET', ''), 'collections' => [], 'forms' => [], + 'user_login' => false, 'user_registration' => false, 'error_message' => 'Captcha failed.', 'disclaimer' => '', diff --git a/src/CaptchaServiceProvider.php b/src/CaptchaServiceProvider.php index 049ce2c..d872a11 100644 --- a/src/CaptchaServiceProvider.php +++ b/src/CaptchaServiceProvider.php @@ -4,7 +4,9 @@ use AryehRaber\Captcha\Listeners\ValidateEntry; use AryehRaber\Captcha\Listeners\ValidateFormSubmission; +use AryehRaber\Captcha\Listeners\ValidateUserLogin; use AryehRaber\Captcha\Listeners\ValidateUserRegistration; +use Illuminate\Auth\Events\Login; use Statamic\Events\EntrySaving; use Statamic\Events\FormSubmitted; use Statamic\Events\UserRegistering; @@ -21,6 +23,7 @@ class CaptchaServiceProvider extends AddonServiceProvider protected $listen = [ EntrySaving::class => [ValidateEntry::class], FormSubmitted::class => [ValidateFormSubmission::class], + Login::class => [ValidateUserLogin::class], UserRegistering::class => [ValidateUserRegistration::class], ]; diff --git a/src/Listeners/ValidateUserLogin.php b/src/Listeners/ValidateUserLogin.php new file mode 100644 index 0000000..452fd21 --- /dev/null +++ b/src/Listeners/ValidateUserLogin.php @@ -0,0 +1,37 @@ +captcha = $captcha; + } + + public function handle(Login $event) + { + $user = $event->user; + + if (! $this->shouldVerify()) { + return $user; + } + + if ($this->captcha->verify()->invalidResponse()) { + throw ValidationException::withMessages(['captcha' => config('captcha.error_message')]); + } + + return $user; + } + + protected function shouldVerify() + { + return config('captcha.user_login', false); + } +}