From 7e6c4438a1fa1976816922511c1b7659ead7e799 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Wed, 13 Mar 2019 06:50:39 +0800 Subject: [PATCH] [5.8] Send LogoutOtherDevices event when request is made. This would allow developers to manages other authentications to react to this request such as `Passport`, where the application may choose to revoke all users access_token etc. Signed-off-by: Mior Muhammad Zaki --- .../Auth/Events/LogoutOtherDevices.php | 37 +++++++++++++++++++ src/Illuminate/Auth/SessionGuard.php | 17 +++++++++ tests/Integration/Auth/AuthenticationTest.php | 22 +++++++++++ 3 files changed, 76 insertions(+) create mode 100644 src/Illuminate/Auth/Events/LogoutOtherDevices.php diff --git a/src/Illuminate/Auth/Events/LogoutOtherDevices.php b/src/Illuminate/Auth/Events/LogoutOtherDevices.php new file mode 100644 index 000000000000..580e4ee91b3a --- /dev/null +++ b/src/Illuminate/Auth/Events/LogoutOtherDevices.php @@ -0,0 +1,37 @@ +user = $user; + $this->guard = $guard; + } +} diff --git a/src/Illuminate/Auth/SessionGuard.php b/src/Illuminate/Auth/SessionGuard.php index 0b6cb7fd5675..0b4ea8b259b9 100644 --- a/src/Illuminate/Auth/SessionGuard.php +++ b/src/Illuminate/Auth/SessionGuard.php @@ -552,6 +552,8 @@ public function logoutOtherDevices($password, $attribute = 'password') $this->queueRecallerCookie($this->user()); + $this->fireLogoutOtherDevicesEvent($this->user()); + return $result; } @@ -615,6 +617,21 @@ protected function fireAuthenticatedEvent($user) } } + /** + * Fire the logout other devices event if the dispatcher is set. + * + * @param \Illuminate\Contracts\Auth\Authenticatable $user + * @return void + */ + protected function fireLogoutOtherDevicesEvent($user) + { + if (isset($this->events)) { + $this->events->dispatch(new Events\LogoutOtherDevices( + $this->name, $user + )); + } + } + /** * Fire the failed authentication attempt event with the given arguments. * diff --git a/tests/Integration/Auth/AuthenticationTest.php b/tests/Integration/Auth/AuthenticationTest.php index d4f178ff736e..d61b8c97955c 100644 --- a/tests/Integration/Auth/AuthenticationTest.php +++ b/tests/Integration/Auth/AuthenticationTest.php @@ -13,6 +13,7 @@ use Illuminate\Auth\EloquentUserProvider; use Illuminate\Auth\Events\Authenticated; use Illuminate\Database\Schema\Blueprint; +use Illuminate\Auth\Events\LogoutOtherDevices; use Illuminate\Tests\Integration\Auth\Fixtures\AuthenticationTestUser; /** @@ -185,6 +186,27 @@ public function test_logging_out() }); } + public function test_logging_out_other_devices() + { + Event::fake(); + + $this->app['auth']->loginUsingId(1); + + $user = $this->app['auth']->user(); + + $this->assertEquals(1, $user->id); + + $this->app['auth']->logoutOtherDevices('adifferentpassword'); + $this->assertEquals(1, $user->id); + + Event::assertDispatched(LogoutOtherDevices::class, function ($event) { + $this->assertEquals('web', $event->guard); + $this->assertEquals(1, $event->user->id); + + return true; + }); + } + public function test_logging_in_out_via_attempt_remembering() { $this->assertTrue(