Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…to DarkGhostHunter-6.x
  • Loading branch information
taylorotwell committed Feb 5, 2020
2 parents 38f9fa4 + 5aead14 commit f206e26
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
37 changes: 37 additions & 0 deletions src/Illuminate/Auth/Events/Validated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Illuminate\Auth\Events;

use Illuminate\Queue\SerializesModels;

class Validated
{
use SerializesModels;

/**
* The authentication guard name.
*
* @var string
*/
public $guard;

/**
* The user retrieved and validated from the User Provider.
*
* @var \Illuminate\Contracts\Auth\Authenticatable
*/
public $user;

/**
* Create a new event instance.
*
* @param string $guard
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @return void
*/
public function __construct($guard, $user)
{
$this->user = $user;
$this->guard = $guard;
}
}
25 changes: 23 additions & 2 deletions src/Illuminate/Auth/SessionGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Auth\Events\Login;
use Illuminate\Auth\Events\Logout;
use Illuminate\Auth\Events\OtherDeviceLogout;
use Illuminate\Auth\Events\Validated;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\StatefulGuard;
use Illuminate\Contracts\Auth\SupportsBasicAuth;
Expand Down Expand Up @@ -367,7 +368,7 @@ public function attempt(array $credentials = [], $remember = false)
// If the authentication attempt fails we will fire an event so that the user
// may be notified of any suspicious attempts to access their account from
// an unrecognized user. A developer may listen to this event as needed.
$this->fireFailedEvent($user, $credentials);
$this->fireFailedEvent($this->lastAttempted, $credentials);

return false;
}
Expand All @@ -381,7 +382,13 @@ public function attempt(array $credentials = [], $remember = false)
*/
protected function hasValidCredentials($user, $credentials)
{
return ! is_null($user) && $this->provider->validateCredentials($user, $credentials);
$validated = ! is_null($user) && $this->provider->validateCredentials($user, $credentials);

if ($validated) {
$this->fireValidatedEvent($user);
}

return $validated;
}

/**
Expand Down Expand Up @@ -622,6 +629,20 @@ protected function fireAttemptEvent(array $credentials, $remember = false)
}
}

/**
* Fires the retrieved event if the dispatcher is set.
*
* @param $user
*/
protected function fireValidatedEvent($user)
{
if (isset($this->events)) {
$this->events->dispatch(new Validated(
$this->name, $user
));
}
}

/**
* Fire the login event if the dispatcher is set.
*
Expand Down
5 changes: 5 additions & 0 deletions tests/Auth/AuthGuardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Auth\Events\Failed;
use Illuminate\Auth\Events\Login;
use Illuminate\Auth\Events\Logout;
use Illuminate\Auth\Events\Validated;
use Illuminate\Auth\SessionGuard;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider;
Expand Down Expand Up @@ -96,6 +97,7 @@ public function testAttemptCallsRetrieveByCredentials()
$guard->setDispatcher($events = m::mock(Dispatcher::class));
$events->shouldReceive('dispatch')->once()->with(m::type(Attempting::class));
$events->shouldReceive('dispatch')->once()->with(m::type(Failed::class));
$events->shouldNotReceive('dispatch')->with(m::type(Validated::class));
$guard->getProvider()->shouldReceive('retrieveByCredentials')->once()->with(['foo']);
$guard->attempt(['foo']);
}
Expand All @@ -106,6 +108,7 @@ public function testAttemptReturnsUserInterface()
$guard = $this->getMockBuilder(SessionGuard::class)->setMethods(['login'])->setConstructorArgs(['default', $provider, $session, $request])->getMock();
$guard->setDispatcher($events = m::mock(Dispatcher::class));
$events->shouldReceive('dispatch')->once()->with(m::type(Attempting::class));
$events->shouldReceive('dispatch')->once()->with(m::type(Validated::class));
$user = $this->createMock(Authenticatable::class);
$guard->getProvider()->shouldReceive('retrieveByCredentials')->once()->andReturn($user);
$guard->getProvider()->shouldReceive('validateCredentials')->with($user, ['foo'])->andReturn(true);
Expand All @@ -119,6 +122,7 @@ public function testAttemptReturnsFalseIfUserNotGiven()
$mock->setDispatcher($events = m::mock(Dispatcher::class));
$events->shouldReceive('dispatch')->once()->with(m::type(Attempting::class));
$events->shouldReceive('dispatch')->once()->with(m::type(Failed::class));
$events->shouldNotReceive('dispatch')->with(m::type(Validated::class));
$mock->getProvider()->shouldReceive('retrieveByCredentials')->once()->andReturn(null);
$this->assertFalse($mock->attempt(['foo']));
}
Expand Down Expand Up @@ -169,6 +173,7 @@ public function testFailedAttemptFiresFailedEvent()
$guard->setDispatcher($events = m::mock(Dispatcher::class));
$events->shouldReceive('dispatch')->once()->with(m::type(Attempting::class));
$events->shouldReceive('dispatch')->once()->with(m::type(Failed::class));
$events->shouldNotReceive('dispatch')->with(m::type(Validated::class));
$guard->getProvider()->shouldReceive('retrieveByCredentials')->once()->with(['foo'])->andReturn(null);
$guard->attempt(['foo']);
}
Expand Down
8 changes: 8 additions & 0 deletions tests/Integration/Auth/AuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Auth\Events\Login;
use Illuminate\Auth\Events\Logout;
use Illuminate\Auth\Events\OtherDeviceLogout;
use Illuminate\Auth\Events\Validated;
use Illuminate\Auth\SessionGuard;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Events\Dispatcher;
Expand Down Expand Up @@ -126,6 +127,7 @@ public function testLoggingInFailsViaAttempt()

return true;
});
Event::assertNotDispatched(Validated::class);
Event::assertDispatched(Failed::class, function ($event) {
$this->assertSame('web', $event->guard);
$this->assertEquals(['email' => 'wrong', 'password' => 'password'], $event->credentials);
Expand All @@ -151,6 +153,12 @@ public function testLoggingInSucceedsViaAttempt()

return true;
});
Event::assertDispatched(Validated::class, function ($event) {
$this->assertSame('web', $event->guard);
$this->assertEquals(1, $event->user->id);

return true;
});
Event::assertDispatched(Login::class, function ($event) {
$this->assertSame('web', $event->guard);
$this->assertEquals(1, $event->user->id);
Expand Down

0 comments on commit f206e26

Please sign in to comment.