Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to extract a few more common attributes from the authenticated user #577

Merged
merged 4 commits into from Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/cs.yaml
Expand Up @@ -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
Expand Down
16 changes: 11 additions & 5 deletions .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
6 changes: 5 additions & 1 deletion .php_cs → .php-cs-fixer.php
Expand Up @@ -4,9 +4,13 @@
->in(__DIR__ . '/src')
;

return PhpCsFixer\Config::create()
$config = new PhpCsFixer\Config;

$config
->setRules([
'@PSR2' => true,
])
->setFinder($finder)
;

return $config;
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -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": {
Expand All @@ -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": {
Expand Down
27 changes: 18 additions & 9 deletions src/Sentry/Laravel/EventHandler.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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'),
]
: [];
stayallive marked this conversation as resolved.
Show resolved Hide resolved

try {
/** @var \Illuminate\Http\Request $request */
$request = $this->container->make('request');
Expand All @@ -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));
});
}

Expand Down