Skip to content

Commit 819ad96

Browse files
committed
Fix triggering a missing attribute violation
1 parent 50f0f48 commit 819ad96

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/Sentry/Laravel/EventHandler.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,14 +274,24 @@ private function configureUserScopeFromModel($authUser): void
274274

275275
// If the user is a Laravel Eloquent model we try to extract some common fields from it
276276
if ($authUser instanceof Model) {
277-
$username = $authUser->getAttribute('username');
277+
$email = null;
278+
279+
if ($authUser->hasAttribute('email')) {
280+
$email = $authUser->getAttribute('email');
281+
} elseif ($authUser->hasAttribute('mail')) {
282+
$email = $authUser->getAttribute('mail');
283+
}
284+
285+
$username = $authUser->hasAttribute('username')
286+
? (string)$authUser->getAttribute('username')
287+
: null;
278288

279289
$userData = [
280290
'id' => $authUser instanceof Authenticatable
281291
? $authUser->getAuthIdentifier()
282292
: $authUser->getKey(),
283-
'email' => $authUser->getAttribute('email') ?? $authUser->getAttribute('mail'),
284-
'username' => $username === null ? $username : (string)$username,
293+
'email' => $email,
294+
'username' => $username,
285295
];
286296
}
287297

test/Sentry/EventHandler/AuthEventsTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ class AuthEventsTest extends TestCase
1515

1616
public function testAuthenticatedEventFillsUserOnScope(): void
1717
{
18-
$user = new AuthEventsTestUserModel();
18+
$user = new AuthEventsTestUserModel;
1919

20-
$user->id = 123;
21-
$user->username = 'username';
22-
$user->email = '[email protected]';
20+
$user->forceFill([
21+
'id' => 123,
22+
'username' => 'username',
23+
'email' => '[email protected]',
24+
]);
2325

2426
$scope = $this->getCurrentSentryScope();
2527

0 commit comments

Comments
 (0)