From 2ab516661fa251f4b7446d1c3fca09570197b525 Mon Sep 17 00:00:00 2001 From: Alex Bouma Date: Fri, 30 Sep 2022 15:07:11 +0200 Subject: [PATCH 1/4] Upgrade php-cs-fixer dependency --- .github/workflows/cs.yaml | 2 +- .gitignore | 16 +++++++++++----- .php_cs => .php-cs-fixer.php | 6 +++++- composer.json | 4 ++-- 4 files changed, 19 insertions(+), 9 deletions(-) rename .php_cs => .php-cs-fixer.php (71%) diff --git a/.github/workflows/cs.yaml b/.github/workflows/cs.yaml index 05440c66..bccfa52f 100644 --- a/.github/workflows/cs.yaml +++ b/.github/workflows/cs.yaml @@ -18,7 +18,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: '7.4' + php-version: '8.1' - name: Install dependencies run: composer update --no-progress --no-interaction --prefer-dist diff --git a/.gitignore b/.gitignore index 693f2bf8..a9560c27 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,13 @@ -*.lock -package.xml -/vendor +# IDE .idea -.php_cs.cache -docs/_build + +# Composer +/vendor +composer.lock + +# phpunit/phpunit .phpunit.result.cache + +# friendsofphp/php-cs-fixer +/.php_cs.cache +/.php-cs-fixer.cache diff --git a/.php_cs b/.php-cs-fixer.php similarity index 71% rename from .php_cs rename to .php-cs-fixer.php index fd5ca3e0..60761206 100644 --- a/.php_cs +++ b/.php-cs-fixer.php @@ -4,9 +4,13 @@ ->in(__DIR__ . '/src') ; -return PhpCsFixer\Config::create() +$config = new PhpCsFixer\Config; + +$config ->setRules([ '@PSR2' => true, ]) ->setFinder($finder) ; + +return $config; diff --git a/composer.json b/composer.json index 940dd906..cd152ece 100644 --- a/composer.json +++ b/composer.json @@ -37,7 +37,7 @@ "phpunit/phpunit": "^5.7 | ^6.5 | ^7.5 | ^8.4 | ^9.3", "laravel/framework": "5.0 - 5.8 | ^6.0 | ^7.0 | ^8.0 | ^9.0", "orchestra/testbench": "3.1 - 3.8 | ^4.7 | ^5.1 | ^6.0 | ^7.0", - "friendsofphp/php-cs-fixer": "2.18.*", + "friendsofphp/php-cs-fixer": "^3.11", "mockery/mockery": "^1.3" }, "autoload-dev": { @@ -56,7 +56,7 @@ "XDEBUG_MODE=coverage vendor/bin/phpunit --verbose --configuration phpunit.xml --coverage-html test/html-report" ], "phpcs": [ - "vendor/bin/php-cs-fixer fix --config=.php_cs --verbose --diff --dry-run" + "vendor/bin/php-cs-fixer fix --verbose --diff --dry-run" ] }, "extra": { From 00e02d4150dd093ae2894d0e3dfc4a3678c68349 Mon Sep 17 00:00:00 2001 From: Alex Bouma Date: Fri, 30 Sep 2022 15:25:43 +0200 Subject: [PATCH 2/4] Try to extract a few more common attributes from the authenticated user --- src/Sentry/Laravel/EventHandler.php | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/Sentry/Laravel/EventHandler.php b/src/Sentry/Laravel/EventHandler.php index e28ede32..6471b8e3 100644 --- a/src/Sentry/Laravel/EventHandler.php +++ b/src/Sentry/Laravel/EventHandler.php @@ -6,9 +6,11 @@ use Illuminate\Auth\Events\Authenticated; use Illuminate\Console\Events\CommandFinished; use Illuminate\Console\Events\CommandStarting; +use Illuminate\Contracts\Auth\Authenticatable; use Illuminate\Contracts\Container\BindingResolutionException; use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Events\Dispatcher; +use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Events\QueryExecuted; use Illuminate\Http\Request; use Illuminate\Log\Events\MessageLogged; @@ -421,9 +423,7 @@ private function addLogBreadcrumb(string $level, ?string $message, array $contex */ protected function authenticatedHandler(Authenticated $event) { - $this->configureUserScopeWithRequest([ - 'id' => $event->user->getAuthIdentifier(), - ]); + $this->configureUserScopeFromModel($event->user); } /** @@ -433,20 +433,29 @@ protected function authenticatedHandler(Authenticated $event) */ protected function sanctumTokenAuthenticatedHandler(Sanctum\TokenAuthenticated $event) { - $this->configureUserScopeWithRequest([ - 'id' => $event->token->tokenable->getAuthIdentifier(), - ]); + $this->configureUserScopeFromModel($event->token->tokenable); } /** * Configures the user scope with the user data and values from the HTTP request. * - * @param array $userData + * @param mixed $authUser * * @return void */ - private function configureUserScopeWithRequest(array $userData): void + private function configureUserScopeFromModel($authUser): void { + // If the user is a Laravel Eloquent model we try to extract some common fields from it + $userData = $authUser instanceof Model + ? [ + 'id' => $authUser instanceof Authenticatable + ? $authUser->getAuthIdentifier() + : $authUser->getKey(), + 'email' => $authUser->getAttribute('email') ?? $authUser->getAttribute('mail'), + 'username' => $authUser->getAttribute('username'), + ] + : []; + try { /** @var \Illuminate\Http\Request $request */ $request = $this->container->make('request'); @@ -463,7 +472,7 @@ private function configureUserScopeWithRequest(array $userData): void } Integration::configureScope(static function (Scope $scope) use ($userData): void { - $scope->setUser($userData); + $scope->setUser(array_filter($userData)); }); } From 6105aa3cd1a33b1359fbae1af8f7c47a8fe5a7cb Mon Sep 17 00:00:00 2001 From: Alex Bouma Date: Fri, 30 Sep 2022 15:36:10 +0200 Subject: [PATCH 3/4] Add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d42b9103..2216e1c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Fix status code not populated on transaction if response did not inherit from `Illuminate\Http\Response` like `Illuminate\Http\JsonResponse` (#573) - Align Span Operations with new spec (#574) - Fix broken `SetRequestMiddleware` on Laravel < 6.0 (#575) +- Also extract the authenticated user `email` and `username` attributes if available (#577) ## 2.13.0 From 388d2016feeb3a4070bbc81a82d7e4ea829f1048 Mon Sep 17 00:00:00 2001 From: Alex Bouma Date: Fri, 30 Sep 2022 15:36:59 +0200 Subject: [PATCH 4/4] CS --- src/Sentry/Laravel/EventHandler.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Sentry/Laravel/EventHandler.php b/src/Sentry/Laravel/EventHandler.php index 6471b8e3..f9f32bfe 100644 --- a/src/Sentry/Laravel/EventHandler.php +++ b/src/Sentry/Laravel/EventHandler.php @@ -445,16 +445,18 @@ protected function sanctumTokenAuthenticatedHandler(Sanctum\TokenAuthenticated $ */ private function configureUserScopeFromModel($authUser): void { + $userData = []; + // If the user is a Laravel Eloquent model we try to extract some common fields from it - $userData = $authUser instanceof Model - ? [ + if ($authUser instanceof Model) { + $userData = [ 'id' => $authUser instanceof Authenticatable ? $authUser->getAuthIdentifier() : $authUser->getKey(), 'email' => $authUser->getAttribute('email') ?? $authUser->getAttribute('mail'), 'username' => $authUser->getAttribute('username'), - ] - : []; + ]; + } try { /** @var \Illuminate\Http\Request $request */