Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions analysis-baseline.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,18 @@ code = "possibly-null-operand"
message = "Left operand in `<` comparison might be `null` (type `float|null`)."
count = 1

[[issues]]
file = "src/State/Scope.php"
code = "deprecated-method"
message = 'Call to deprecated method: `Sentry\UserDataBag::getSegment`.'
count = 2

[[issues]]
file = "src/State/Scope.php"
code = "deprecated-method"
message = 'Call to deprecated method: `Sentry\UserDataBag::setSegment`.'
count = 1

[[issues]]
file = "src/State/Scope.php"
code = "impossible-condition"
Expand Down
64 changes: 60 additions & 4 deletions src/State/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class Scope
*/
private $user;

/**
* @var (callable(): (UserDataBag|null))|null The callable that resolves the user each time it is read
*/
private $userResolver;

/**
* @var array<string, array<string, mixed>> The list of contexts associated to this scope
*/
Expand Down Expand Up @@ -248,7 +253,54 @@ public function setExtras(array $extras): self
*/
public function getUser(): ?UserDataBag
{
return $this->user;
if ($this->userResolver === null) {
return $this->user;
}

$userResolver = $this->userResolver;
// unset the callback to prevent recursion in case getUser is called in the callback
$this->userResolver = null;

try {
$resolvedUser = $userResolver();
} catch (\Throwable $exception) {
$resolvedUser = null;
} finally {
$this->userResolver = $userResolver;
}

if ($resolvedUser === null) {
return $this->user;
}

if ($this->user === null) {
return clone $resolvedUser;
}

$user = clone $this->user;
$user->setId($user->getId() ?? $resolvedUser->getId());
$user->setEmail($user->getEmail() ?? $resolvedUser->getEmail());
$user->setUsername($user->getUsername() ?? $resolvedUser->getUsername());
$user->setIpAddress($user->getIpAddress() ?? $resolvedUser->getIpAddress());
Comment thread
Litarnus marked this conversation as resolved.
Resolved
$user->setSegment($user->getSegment() ?? $resolvedUser->getSegment());

return (clone $resolvedUser)->merge($user);
Comment thread
Litarnus marked this conversation as resolved.
Resolved
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
}

/**
* Sets a callable that resolves the user each time it is read, which
* happens for every event, log and metric, so it should be cheap. The
* data set on the scope takes precedence over the data it returns.
*
* @param callable(): (UserDataBag|null) $userResolver
*
* @return $this
*/
public function setUserResolver(callable $userResolver): self
{
$this->userResolver = $userResolver;

return $this;
}

/**
Expand Down Expand Up @@ -285,6 +337,7 @@ public function setUser($user): self
public function removeUser(): self
{
$this->user = null;
$this->userResolver = null;

return $this;
}
Expand Down Expand Up @@ -439,6 +492,7 @@ public static function getExternalPropagationContext(): ?array
public function clear(): self
{
$this->user = null;
$this->userResolver = null;
$this->level = null;
$this->span = null;
$this->fingerprint = [];
Expand Down Expand Up @@ -489,13 +543,15 @@ public function applyToEvent(Event $event, ?EventHint $hint = null, ?Options $op
$event->setExtra(array_merge($this->extra, $event->getExtra()));
}

if ($this->user !== null) {
$scopeUser = $this->getUser();

if ($scopeUser !== null) {
$user = $event->getUser();

if ($user === null) {
$user = $this->user;
$user = $scopeUser;
} else {
$user = $this->user->merge($user);
$user = $scopeUser->merge($user);
Comment thread
Litarnus marked this conversation as resolved.
Resolved
}

$event->setUser($user);
Expand Down
122 changes: 122 additions & 0 deletions tests/State/ScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,125 @@ public function testSetUserThrowsOnInvalidArgument(): void
$scope->setUser('foo');
}

public function testSetUserResolver(): void
{
$scope = new Scope();
$scope->setUser(UserDataBag::createFromUserIpAddress('127.0.0.1'));

$resolvable = false;
$calls = 0;

$scope->setUserResolver(static function () use (&$resolvable, &$calls): ?UserDataBag {
++$calls;

return $resolvable ? UserDataBag::createFromUserIdentifier('unique_id') : null;
});

$this->assertEquals(UserDataBag::createFromUserIpAddress('127.0.0.1'), $scope->getUser());
$this->assertSame(1, $calls);

$resolvable = true;

$event = $scope->applyToEvent(Event::createEvent());

$this->assertNotNull($event);
$this->assertEquals(UserDataBag::createFromArray(['id' => 'unique_id', 'ip_address' => '127.0.0.1']), $event->getUser());
$this->assertSame(2, $calls);

$resolvable = false;

$this->assertEquals(UserDataBag::createFromUserIpAddress('127.0.0.1'), $scope->getUser());
}

public function testSetUserResolverDoesNotOverrideTheUser(): void
{
$scope = new Scope();
$scope->setUser(['id' => 'unique_id', 'subscription' => 'basic']);

$scope->setUserResolver(static function (): UserDataBag {
return UserDataBag::createFromArray([
'id' => 'resolved_id',
'email' => 'foo@example.com',
'segment' => 'my_segment',
'subscription' => 'pro',
'plan' => 'yearly',
]);
});

$this->assertEquals(
UserDataBag::createFromArray([
'id' => 'unique_id',
'email' => 'foo@example.com',
'segment' => 'my_segment',
'subscription' => 'basic',
'plan' => 'yearly',
]),
$scope->getUser()
);
}

public function testSetUserResolverDoesNotChangeTheResolvedUser(): void
{
$resolvedUser = UserDataBag::createFromUserIdentifier('unique_id');

$scope = new Scope();
$scope->setUserResolver(static function () use ($resolvedUser): UserDataBag {
return $resolvedUser;
});

$event = Event::createEvent();
$event->setUser(UserDataBag::createFromUserIpAddress('127.0.0.1'));

$scope->applyToEvent($event);

$this->assertEquals(UserDataBag::createFromUserIdentifier('unique_id'), $resolvedUser);
}

public function testSetUserResolverKeepsTheUserIfTheResolverThrows(): void
{
$scope = new Scope();
$scope->setUser(UserDataBag::createFromUserIpAddress('127.0.0.1'));

$scope->setUserResolver(static function (): UserDataBag {
throw new \RuntimeException('foo');
});

$this->assertEquals(UserDataBag::createFromUserIpAddress('127.0.0.1'), $scope->getUser());
}

public function testSetUserResolverIsNotCalledAgainWhileItRuns(): void
{
$scope = new Scope();
$calls = 0;

$scope->setUserResolver(static function () use ($scope, &$calls): ?UserDataBag {
++$calls;

return $scope->getUser();
});

$this->assertNull($scope->getUser());
$this->assertSame(1, $calls);

$scope->getUser();

$this->assertSame(2, $calls);
}

public function testRemoveUserRemovesTheUserResolver(): void
{
$scope = new Scope();
$scope->setUserResolver(static function (): UserDataBag {
return UserDataBag::createFromUserIdentifier('unique_id');
});

$this->assertEquals(UserDataBag::createFromUserIdentifier('unique_id'), $scope->getUser());

$scope->removeUser();

$this->assertNull($scope->getUser());
}

public function testRemoveUser(): void
{
$scope = new Scope();
Expand Down Expand Up @@ -523,6 +642,9 @@ public function testClear(): void
$scope->setTags(['bar' => 'foo']);
$scope->addFeatureFlag('feature', true);
$scope->setUser(UserDataBag::createFromUserIdentifier('unique_id'));
$scope->setUserResolver(static function (): UserDataBag {
return UserDataBag::createFromUserIdentifier('resolved_id');
});
$scope->clear();

$event = $scope->applyToEvent(Event::createEvent());
Expand Down
Loading