Skip to content

Commit

Permalink
Fix triggering a missing attribute violation
Browse files Browse the repository at this point in the history
  • Loading branch information
stayallive committed Feb 4, 2025
1 parent 50f0f48 commit 819ad96
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
16 changes: 13 additions & 3 deletions src/Sentry/Laravel/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,24 @@ 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');
$email = null;

if ($authUser->hasAttribute('email')) {
$email = $authUser->getAttribute('email');
} elseif ($authUser->hasAttribute('mail')) {
$email = $authUser->getAttribute('mail');
}

$username = $authUser->hasAttribute('username')
? (string)$authUser->getAttribute('username')
: null;

$userData = [
'id' => $authUser instanceof Authenticatable
? $authUser->getAuthIdentifier()
: $authUser->getKey(),
'email' => $authUser->getAttribute('email') ?? $authUser->getAttribute('mail'),
'username' => $username === null ? $username : (string)$username,
'email' => $email,
'username' => $username,
];
}

Expand Down
10 changes: 6 additions & 4 deletions test/Sentry/EventHandler/AuthEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ class AuthEventsTest extends TestCase

public function testAuthenticatedEventFillsUserOnScope(): void
{
$user = new AuthEventsTestUserModel();
$user = new AuthEventsTestUserModel;

$user->id = 123;
$user->username = 'username';
$user->email = '[email protected]';
$user->forceFill([
'id' => 123,
'username' => 'username',
'email' => '[email protected]',
]);

$scope = $this->getCurrentSentryScope();

Expand Down

0 comments on commit 819ad96

Please sign in to comment.