From e5216cd15ee8b947ec2ada827f8d1a1d45f71543 Mon Sep 17 00:00:00 2001 From: Rodrigo Pedra Brum Date: Tue, 11 Sep 2018 10:31:51 -0300 Subject: [PATCH] Do not pass the guard instance to the authentication events (#25568) --- src/Illuminate/Auth/Events/Attempting.php | 6 +-- src/Illuminate/Auth/Events/Authenticated.php | 6 +-- src/Illuminate/Auth/Events/Failed.php | 6 +-- src/Illuminate/Auth/Events/Login.php | 6 +-- src/Illuminate/Auth/Events/Logout.php | 6 +-- src/Illuminate/Auth/SessionGuard.php | 10 ++-- tests/Integration/Auth/AuthenticationTest.php | 51 +++++++++++++++++-- 7 files changed, 66 insertions(+), 25 deletions(-) diff --git a/src/Illuminate/Auth/Events/Attempting.php b/src/Illuminate/Auth/Events/Attempting.php index c7c8437db964..3f911bac58e8 100644 --- a/src/Illuminate/Auth/Events/Attempting.php +++ b/src/Illuminate/Auth/Events/Attempting.php @@ -5,9 +5,9 @@ class Attempting { /** - * The authentication guard implementation. + * The authentication guard name. * - * @var \Illuminate\Contracts\Auth\StatefulGuard + * @var string */ public $guard; @@ -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 diff --git a/src/Illuminate/Auth/Events/Authenticated.php b/src/Illuminate/Auth/Events/Authenticated.php index a674ec8fcd6d..faefcbecba3a 100644 --- a/src/Illuminate/Auth/Events/Authenticated.php +++ b/src/Illuminate/Auth/Events/Authenticated.php @@ -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; @@ -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 */ diff --git a/src/Illuminate/Auth/Events/Failed.php b/src/Illuminate/Auth/Events/Failed.php index 8341d4691b44..34f812487027 100644 --- a/src/Illuminate/Auth/Events/Failed.php +++ b/src/Illuminate/Auth/Events/Failed.php @@ -5,9 +5,9 @@ class Failed { /** - * The authentication guard implementation. + * The authentication guard name. * - * @var \Illuminate\Contracts\Auth\StatefulGuard + * @var string */ public $guard; @@ -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 diff --git a/src/Illuminate/Auth/Events/Login.php b/src/Illuminate/Auth/Events/Login.php index ab587f08d244..3005183a9bc2 100644 --- a/src/Illuminate/Auth/Events/Login.php +++ b/src/Illuminate/Auth/Events/Login.php @@ -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; @@ -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 diff --git a/src/Illuminate/Auth/Events/Logout.php b/src/Illuminate/Auth/Events/Logout.php index 5e8f77ddee34..bc8c39400857 100644 --- a/src/Illuminate/Auth/Events/Logout.php +++ b/src/Illuminate/Auth/Events/Logout.php @@ -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; @@ -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 */ diff --git a/src/Illuminate/Auth/SessionGuard.php b/src/Illuminate/Auth/SessionGuard.php index 5d22cfa828c8..d719cc5a2258 100644 --- a/src/Illuminate/Auth/SessionGuard.php +++ b/src/Illuminate/Auth/SessionGuard.php @@ -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 @@ -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 )); } } @@ -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 )); } } @@ -611,7 +611,7 @@ protected function fireAuthenticatedEvent($user) { if (isset($this->events)) { $this->events->dispatch(new Events\Authenticated( - $this, $user + $this->name, $user )); } } @@ -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 )); } } diff --git a/tests/Integration/Auth/AuthenticationTest.php b/tests/Integration/Auth/AuthenticationTest.php index d8cdb978ca19..00bb307206b7 100644 --- a/tests/Integration/Auth/AuthenticationTest.php +++ b/tests/Integration/Auth/AuthenticationTest.php @@ -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(); @@ -123,7 +123,27 @@ 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']) @@ -131,8 +151,24 @@ public function logging_in_via_attempt() $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; + }); } /** @@ -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; + }); } /**