From 170cccffb5f8391315ab408ad5aff5501ada8244 Mon Sep 17 00:00:00 2001 From: Duilio Palacios Date: Sun, 29 May 2016 14:08:14 +0100 Subject: [PATCH] Add failed login attempt event --- src/Illuminate/Auth/Events/Failed.php | 41 +++++++++++++++++++++++++++ src/Illuminate/Auth/SessionGuard.php | 22 ++++++++++++++ tests/Auth/AuthGuardTest.php | 15 +++++++++- 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 src/Illuminate/Auth/Events/Failed.php diff --git a/src/Illuminate/Auth/Events/Failed.php b/src/Illuminate/Auth/Events/Failed.php new file mode 100644 index 000000000000..859c1c19fcc9 --- /dev/null +++ b/src/Illuminate/Auth/Events/Failed.php @@ -0,0 +1,41 @@ +credentials = $credentials; + $this->remember = $remember; + $this->user = $user; + } +} diff --git a/src/Illuminate/Auth/SessionGuard.php b/src/Illuminate/Auth/SessionGuard.php index a744962e7f91..7b879bd67738 100644 --- a/src/Illuminate/Auth/SessionGuard.php +++ b/src/Illuminate/Auth/SessionGuard.php @@ -363,6 +363,10 @@ public function attempt(array $credentials = [], $remember = false, $login = tru return true; } + if ($login) { + $this->fireFailedEvent($credentials, $remember, $user); + } + return false; } @@ -395,6 +399,24 @@ protected function fireAttemptEvent(array $credentials, $remember, $login) } } + /** + * Fire the failed login attempt event with the arguments. + * + * @param array $credentials + * @param bool $remember + * @param bool $login + * @param \Illuminate\Contracts\Auth\Authenticatable|null $user + * @return void + */ + protected function fireFailedEvent(array $credentials, $remember, $user = null) + { + if (isset($this->events)) { + $this->events->fire(new Events\Failed( + $credentials, $remember, $user + )); + } + } + /** * Register an authentication attempt event listener. * diff --git a/tests/Auth/AuthGuardTest.php b/tests/Auth/AuthGuardTest.php index 23bba2b22913..e5da65cc9a6a 100755 --- a/tests/Auth/AuthGuardTest.php +++ b/tests/Auth/AuthGuardTest.php @@ -1,8 +1,9 @@ getGuard(); $guard->setDispatcher($events = m::mock('Illuminate\Contracts\Events\Dispatcher')); $events->shouldReceive('fire')->once()->with(m::type(Attempting::class)); + $events->shouldReceive('fire')->once()->with(m::type(Failed::class)); $guard->getProvider()->shouldReceive('retrieveByCredentials')->once()->with(['foo']); $guard->attempt(['foo']); } @@ -88,6 +90,7 @@ public function testAttemptReturnsFalseIfUserNotGiven() $mock = $this->getGuard(); $mock->setDispatcher($events = m::mock('Illuminate\Contracts\Events\Dispatcher')); $events->shouldReceive('fire')->once()->with(m::type(Attempting::class)); + $events->shouldReceive('fire')->once()->with(m::type(Failed::class)); $mock->getProvider()->shouldReceive('retrieveByCredentials')->once()->andReturn(null); $this->assertFalse($mock->attempt(['foo'])); } @@ -118,6 +121,16 @@ public function testLoginFiresLoginEvent() $mock->login($user); } + public function testFailedAttemptFiresFailedEvent() + { + $guard = $this->getGuard(); + $guard->setDispatcher($events = m::mock('Illuminate\Contracts\Events\Dispatcher')); + $events->shouldReceive('fire')->once()->with(m::type(Attempting::class)); + $events->shouldReceive('fire')->once()->with(m::type(Failed::class)); + $guard->getProvider()->shouldReceive('retrieveByCredentials')->once()->with(['foo'])->andReturn(null); + $guard->attempt(['foo']); + } + public function testAuthenticateReturnsUserWhenUserIsNotNull() { $user = m::mock('Illuminate\Contracts\Auth\Authenticatable');