From 9bb3dbf223b40a145850485c85b18676bf5c8212 Mon Sep 17 00:00:00 2001 From: Alex Bouma Date: Thu, 18 Apr 2024 11:36:38 +0200 Subject: [PATCH 1/2] Add tests --- test/Sentry/EventHandler/AuthEventsTest.php | 79 +++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 test/Sentry/EventHandler/AuthEventsTest.php diff --git a/test/Sentry/EventHandler/AuthEventsTest.php b/test/Sentry/EventHandler/AuthEventsTest.php new file mode 100644 index 00000000..1f5769d2 --- /dev/null +++ b/test/Sentry/EventHandler/AuthEventsTest.php @@ -0,0 +1,79 @@ + true, + ]; + + public function testAuthenticatedEventFillsUserOnScope(): void + { + $user = new AuthEventsTestUserModel(); + + $user->id = 123; + $user->username = 'username'; + $user->email = 'foo@example.com'; + + $scope = $this->getCurrentSentryScope(); + + $this->assertNull($scope->getUser()); + + $this->dispatchLaravelEvent(new Authenticated('test', $user)); + + $this->assertNotNull($scope->getUser()); + + $this->assertEquals($scope->getUser()->getId(), 123); + $this->assertEquals($scope->getUser()->getUsername(), 'username'); + $this->assertEquals($scope->getUser()->getEmail(), 'foo@example.com'); + } + + public function testAuthenticatedEventFillsUserOnScopeWhenUsernameIsNotAString(): void + { + $user = new AuthEventsTestUserModel(); + + $user->id = 123; + $user->username = 456; + + $scope = $this->getCurrentSentryScope(); + + $this->assertNull($scope->getUser()); + + $this->dispatchLaravelEvent(new Authenticated('test', $user)); + + $this->assertNotNull($scope->getUser()); + + $this->assertEquals($scope->getUser()->getId(), 123); + $this->assertEquals($scope->getUser()->getUsername(), '456'); + } + + public function testAuthenticatedEventDoesNotFillUserOnScopeWhenPIIShouldNotBeSent(): void + { + $this->resetApplicationWithConfig([ + 'sentry.send_default_pii' => false, + ]); + + $user = new AuthEventsTestUserModel(); + + $user->id = 123; + + $scope = $this->getCurrentSentryScope(); + + $this->assertNull($scope->getUser()); + + $this->dispatchLaravelEvent(new Authenticated('test', $user)); + + $this->assertNull($scope->getUser()); + } +} + +class AuthEventsTestUserModel extends Model implements Authenticatable +{ + use \Illuminate\Auth\Authenticatable; +} From 85f57a03c60f4b6d14916a3e44e6ac2aac92d85e Mon Sep 17 00:00:00 2001 From: Alex Bouma Date: Thu, 18 Apr 2024 11:36:44 +0200 Subject: [PATCH 2/2] Cast username to string --- src/Sentry/Laravel/EventHandler.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Sentry/Laravel/EventHandler.php b/src/Sentry/Laravel/EventHandler.php index 1596042f..3aeaa843 100644 --- a/src/Sentry/Laravel/EventHandler.php +++ b/src/Sentry/Laravel/EventHandler.php @@ -274,12 +274,14 @@ private function configureUserScopeFromModel($authUser): void // If the user is a Laravel Eloquent model we try to extract some common fields from it if ($authUser instanceof Model) { + $username = $authUser->getAttribute('username'); + $userData = [ 'id' => $authUser instanceof Authenticatable ? $authUser->getAuthIdentifier() : $authUser->getKey(), 'email' => $authUser->getAttribute('email') ?? $authUser->getAttribute('mail'), - 'username' => $authUser->getAttribute('username'), + 'username' => $username === null ? $username : (string)$username, ]; }