Skip to content

Commit

Permalink
Do not pass the guard instance to the authentication events (#25568)
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigopedra authored and taylorotwell committed Sep 11, 2018
1 parent d54ffa5 commit e5216cd
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 25 deletions.
6 changes: 3 additions & 3 deletions src/Illuminate/Auth/Events/Attempting.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
class Attempting
{
/**
* The authentication guard implementation.
* The authentication guard name.
*
* @var \Illuminate\Contracts\Auth\StatefulGuard
* @var string
*/
public $guard;

Expand All @@ -28,7 +28,7 @@ class Attempting
/**
* Create a new event instance.
*
* @param \Illuminate\Contracts\Auth\StatefulGuard $guard
* @param string $guard
* @param array $credentials
* @param bool $remember
* @return void
Expand Down
6 changes: 3 additions & 3 deletions src/Illuminate/Auth/Events/Authenticated.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class Authenticated
use SerializesModels;

/**
* The authentication guard implementation.
* The authentication guard name.
*
* @var \Illuminate\Contracts\Auth\StatefulGuard
* @var string
*/
public $guard;

Expand All @@ -25,7 +25,7 @@ class Authenticated
/**
* Create a new event instance.
*
* @param \Illuminate\Contracts\Auth\StatefulGuard $guard
* @param string $guard
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @return void
*/
Expand Down
6 changes: 3 additions & 3 deletions src/Illuminate/Auth/Events/Failed.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
class Failed
{
/**
* The authentication guard implementation.
* The authentication guard name.
*
* @var \Illuminate\Contracts\Auth\StatefulGuard
* @var string
*/
public $guard;

Expand All @@ -28,7 +28,7 @@ class Failed
/**
* Create a new event instance.
*
* @param \Illuminate\Contracts\Auth\StatefulGuard $guard
* @param string $guard
* @param \Illuminate\Contracts\Auth\Authenticatable|null $user
* @param array $credentials
* @return void
Expand Down
6 changes: 3 additions & 3 deletions src/Illuminate/Auth/Events/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class Login
use SerializesModels;

/**
* The authentication guard implementation.
* The authentication guard name.
*
* @var \Illuminate\Contracts\Auth\StatefulGuard
* @var string
*/
public $guard;

Expand All @@ -32,7 +32,7 @@ class Login
/**
* Create a new event instance.
*
* @param \Illuminate\Contracts\Auth\StatefulGuard $guard
* @param string $guard
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @param bool $remember
* @return void
Expand Down
6 changes: 3 additions & 3 deletions src/Illuminate/Auth/Events/Logout.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class Logout
use SerializesModels;

/**
* The authenticationg guard implementation.
* The authentication guard name.
*
* @var \Illuminate\Contracts\Auth\StatefulGuard
* @var string
*/
public $guard;

Expand All @@ -25,7 +25,7 @@ class Logout
/**
* Create a new event instance.
*
* @param \Illuminate\Contracts\Auth\StatefulGuard $guard
* @param string $guard
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @return void
*/
Expand Down
10 changes: 5 additions & 5 deletions src/Illuminate/Auth/SessionGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ public function logout()
}

if (isset($this->events)) {
$this->events->dispatch(new Events\Logout($this, $user));
$this->events->dispatch(new Events\Logout($this->name, $user));
}

// Once we have fired the logout event we will clear the users out of memory
Expand Down Expand Up @@ -580,7 +580,7 @@ protected function fireAttemptEvent(array $credentials, $remember = false)
{
if (isset($this->events)) {
$this->events->dispatch(new Events\Attempting(
$this, $credentials, $remember
$this->name, $credentials, $remember
));
}
}
Expand All @@ -596,7 +596,7 @@ protected function fireLoginEvent($user, $remember = false)
{
if (isset($this->events)) {
$this->events->dispatch(new Events\Login(
$this, $user, $remember
$this->name, $user, $remember
));
}
}
Expand All @@ -611,7 +611,7 @@ protected function fireAuthenticatedEvent($user)
{
if (isset($this->events)) {
$this->events->dispatch(new Events\Authenticated(
$this, $user
$this->name, $user
));
}
}
Expand All @@ -627,7 +627,7 @@ protected function fireFailedEvent($user, array $credentials)
{
if (isset($this->events)) {
$this->events->dispatch(new Events\Failed(
$this, $user, $credentials
$this->name, $user, $credentials
));
}
}
Expand Down
51 changes: 46 additions & 5 deletions tests/Integration/Auth/AuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function basic_auth_fails_on_wrong_credentials()
/**
* @test
*/
public function logging_in_via_attempt()
public function logging_in_fails_via_attempt()
{
Event::fake();

Expand All @@ -123,16 +123,52 @@ public function logging_in_via_attempt()
);
$this->assertFalse($this->app['auth']->check());
$this->assertNull($this->app['auth']->user());
Event::assertDispatched(\Illuminate\Auth\Events\Failed::class);
Event::assertDispatched(\Illuminate\Auth\Events\Attempting::class, function ($event) {
$this->assertEquals('web', $event->guard);
$this->assertEquals(['email' => 'wrong', 'password' => 'password'], $event->credentials);

return true;
});
Event::assertDispatched(\Illuminate\Auth\Events\Failed::class, function ($event) {
$this->assertEquals('web', $event->guard);
$this->assertEquals(['email' => 'wrong', 'password' => 'password'], $event->credentials);
$this->assertNull($event->user);

return true;
});
}

/**
* @test
*/
public function logging_in_succeeds_via_attempt()
{
Event::fake();

$this->assertTrue(
$this->app['auth']->attempt(['email' => 'email', 'password' => 'password'])
);
$this->assertInstanceOf(AuthenticationTestUser::class, $this->app['auth']->user());
$this->assertTrue($this->app['auth']->check());

Event::assertDispatched(\Illuminate\Auth\Events\Login::class);
Event::assertDispatched(\Illuminate\Auth\Events\Authenticated::class);
Event::assertDispatched(\Illuminate\Auth\Events\Attempting::class, function ($event) {
$this->assertEquals('web', $event->guard);
$this->assertEquals(['email' => 'email', 'password' => 'password'], $event->credentials);

return true;
});
Event::assertDispatched(\Illuminate\Auth\Events\Login::class, function ($event) {
$this->assertEquals('web', $event->guard);
$this->assertEquals(1, $event->user->id);

return true;
});
Event::assertDispatched(\Illuminate\Auth\Events\Authenticated::class, function ($event) {
$this->assertEquals('web', $event->guard);
$this->assertEquals(1, $event->user->id);

return true;
});
}

/**
Expand All @@ -158,7 +194,12 @@ public function test_logging_out()

$this->app['auth']->logout();
$this->assertNull($this->app['auth']->user());
Event::assertDispatched(\Illuminate\Auth\Events\Logout::class);
Event::assertDispatched(\Illuminate\Auth\Events\Logout::class, function ($event) {
$this->assertEquals('web', $event->guard);
$this->assertEquals(1, $event->user->id);

return true;
});
}

/**
Expand Down

0 comments on commit e5216cd

Please sign in to comment.