Skip to content

Commit 4316604

Browse files
TomHarttom-hart-sky-uk
authored andcommitted
Fix issue if user ID is a Ulid
1 parent 1c77696 commit 4316604

5 files changed

Lines changed: 79 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
Changelog
22
=========
3+
## 2.4.1
4+
* Bugfix: Prevent error in `EntityUserProvider::refreshUser` when `$userId` is a `Ulid`
5+
36
## 2.4.0 (2025-05-29)
47
* Added PHP 8.4 test coverage,
58
* Added: LinkedIn OpenID resource owner,

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@
102102
"symfony/form": "^6.4 || ^7.4 || ^8.0",
103103
"symfony/http-client": "^6.4 || ^7.4 || ^8.0",
104104
"symfony/routing": "^6.4 || ^7.4 || ^8.0",
105-
"symfony/twig-bundle": "^6.4 || ^7.4 || ^8.0"
105+
"symfony/twig-bundle": "^6.4 || ^7.4 || ^8.0",
106+
"symfony/uid": "^6.4 || ^7.4 || ^8.0"
106107
},
107108

108109
"require-dev": {

src/Security/Core/User/EntityUserProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function refreshUser(UserInterface $user): UserInterface
9494
$username = $user->getUserIdentifier();
9595

9696
if (null === $user = $this->findUser([$identifier => $userId])) {
97-
throw $this->createUserNotFoundException($username, \sprintf('User with ID "%d" could not be reloaded.', $userId));
97+
throw $this->createUserNotFoundException($username, \sprintf('User with ID "%s" could not be reloaded.', $userId));
9898
}
9999

100100
return $user;

tests/Fixtures/UlidUser.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the HWIOAuthBundle package.
5+
*
6+
* (c) Hardware Info <opensource@hardware.info>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace HWI\Bundle\OAuthBundle\Tests\Fixtures;
13+
14+
use Symfony\Component\Security\Core\User\UserInterface;
15+
use Symfony\Component\Uid\Ulid;
16+
17+
final class UlidUser implements UserInterface
18+
{
19+
private Ulid $ulid;
20+
21+
public function __construct()
22+
{
23+
$this->ulid = new Ulid('01KGW7EGJX0SHRF6RZ5YYFBR40');
24+
}
25+
26+
public function getId(): Ulid
27+
{
28+
return $this->ulid;
29+
}
30+
31+
public function getRoles(): array
32+
{
33+
return [];
34+
}
35+
36+
public function getUserIdentifier(): string
37+
{
38+
return 'abc';
39+
}
40+
}

tests/Security/Core/User/EntityUserProviderTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/*
46
* This file is part of the HWIOAuthBundle package.
57
*
@@ -15,9 +17,11 @@
1517
use Doctrine\Persistence\ManagerRegistry;
1618
use Doctrine\Persistence\ObjectManager;
1719
use Doctrine\Persistence\ObjectRepository;
20+
use ErrorException;
1821
use HWI\Bundle\OAuthBundle\OAuth\ResourceOwnerInterface;
1922
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
2023
use HWI\Bundle\OAuthBundle\Security\Core\User\EntityUserProvider;
24+
use HWI\Bundle\OAuthBundle\Tests\Fixtures\UlidUser;
2125
use HWI\Bundle\OAuthBundle\Tests\Fixtures\User;
2226
use PHPUnit\Framework\Attributes\Group;
2327
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
@@ -184,4 +188,33 @@ private function assertUserNotFoundException(): void
184188
{
185189
$this->expectException(UserNotFoundException::class);
186190
}
191+
192+
public function testRefreshUserWorksWithUlid(): void
193+
{
194+
// Simulate `framework.php_errors.throw: true`
195+
set_error_handler(static function (
196+
int $severity,
197+
string $message,
198+
string $file,
199+
int $line
200+
): never {
201+
throw new ErrorException($message, 0, $severity, $file, $line);
202+
});
203+
204+
try {
205+
$this->assertUserNotFoundException();
206+
$this->expectExceptionMessage('User with ID "01KGW7EGJX0SHRF6RZ5YYFBR40" could not be reloaded.');
207+
208+
$provider = new EntityUserProvider(
209+
$this->createManagerRegistryMock(),
210+
UlidUser::class,
211+
[]
212+
);
213+
214+
$provider->refreshUser(new UlidUser());
215+
} finally {
216+
restore_error_handler();
217+
}
218+
}
219+
187220
}

0 commit comments

Comments
 (0)