Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.2] Add failed login attempt event #13761

Merged
merged 1 commit into from
May 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/Illuminate/Auth/Events/Failed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Illuminate\Auth\Events;

class Failed
{
/**
* The credentials for the user.
*
* @var array
*/
public $credentials;

/**
* Indicates if the user should be "remembered".
*
* @var bool
*/
public $remember;

/**
* The authenticated user.
*
* @var \Illuminate\Contracts\Auth\Authenticatable|null
*/
public $user;

/**
* Create a new event instance.
*
* @param array $credentials
* @param bool $remember
* @param \Illuminate\Contracts\Auth\Authenticatable|null $user
*/
public function __construct($credentials, $remember, $user = null)
{
$this->credentials = $credentials;
$this->remember = $remember;
$this->user = $user;
}
}
22 changes: 22 additions & 0 deletions src/Illuminate/Auth/SessionGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,10 @@ public function attempt(array $credentials = [], $remember = false, $login = tru
return true;
}

if ($login) {
$this->fireFailedEvent($credentials, $remember, $user);
}

return false;
}

Expand Down Expand Up @@ -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.
*
Expand Down
15 changes: 14 additions & 1 deletion tests/Auth/AuthGuardTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

use Mockery as m;
use Symfony\Component\HttpFoundation\Request;
use Illuminate\Auth\Events\Failed;
use Illuminate\Auth\Events\Attempting;
use Symfony\Component\HttpFoundation\Request;

class AuthGuardTest extends PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -66,6 +67,7 @@ public function testAttemptCallsRetrieveByCredentials()
$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']);
$guard->attempt(['foo']);
}
Expand All @@ -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']));
}
Expand Down Expand Up @@ -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');
Expand Down