Skip to content

Commit 8a27bbd

Browse files
authored
Improve user guard resolution (#217)
1 parent 90ec0f7 commit 8a27bbd

File tree

3 files changed

+73
-19
lines changed

3 files changed

+73
-19
lines changed

src/UserProvider.php

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,37 +42,73 @@ public function __construct(
4242
*/
4343
public function id(): LazyValue|string
4444
{
45-
try {
45+
if (! $this->auth->hasResolvedGuards()) {
46+
return $this->lazyUserId();
47+
}
48+
49+
if ($this->auth->hasUser()) {
50+
return $this->currentUserId();
51+
}
52+
53+
if ($this->rememberedUser) {
54+
return $this->rememberedUserId();
55+
}
56+
57+
return '';
58+
}
59+
60+
/**
61+
* @return LazyValue<string>
62+
*/
63+
private function lazyUserId(): LazyValue
64+
{
65+
return new LazyValue(function () {
66+
if (! $this->auth->hasResolvedGuards()) {
67+
return '';
68+
}
69+
4670
if ($this->auth->hasUser()) {
47-
return Str::tinyText((string) $this->auth->id());
71+
return $this->currentUserId();
72+
}
73+
74+
if ($this->rememberedUser) {
75+
return $this->rememberedUserId();
4876
}
77+
78+
return '';
79+
});
80+
}
81+
82+
private function currentUserId(): string
83+
{
84+
try {
85+
return Str::tinyText((string) $this->auth->id());
4986
} catch (Throwable $e) {
5087
$this->reportResolvingUserIdException($e);
5188

5289
return '';
5390
}
91+
}
5492

55-
return new LazyValue(function () {
56-
try {
57-
if ($this->auth->hasUser()) {
58-
return Str::tinyText((string) $this->auth->id());
59-
} else {
60-
return Str::tinyText((string) $this->rememberedUser?->getAuthIdentifier()); // @phpstan-ignore cast.string
61-
}
62-
} catch (Throwable $e) {
63-
$this->reportResolvingUserIdException($e);
93+
private function rememberedUserId(): string
94+
{
95+
try {
96+
return Str::tinyText((string) $this->rememberedUser?->getAuthIdentifier()); // @phpstan-ignore cast.string
97+
} catch (Throwable $e) {
98+
$this->reportResolvingUserIdException($e);
6499

65-
return '';
66-
}
67-
});
100+
return '';
101+
}
68102
}
69103

70104
/**
71105
* @return array{ id: mixed, name?: mixed, username?: mixed }|null
72106
*/
73107
public function details(): ?array
74108
{
75-
$user = $this->auth->user() ?? $this->rememberedUser;
109+
$user = $this->auth->hasResolvedGuards()
110+
? $this->auth->user() ?? $this->rememberedUser
111+
: $this->rememberedUser;
76112

77113
if ($user === null) {
78114
return null;

tests/Feature/Sensors/UserSensorTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,14 @@ public function test_it_gracefully_handles_exceptions_while_resolving_user_ids()
248248
$ingest->assertLatestWrite('query:1.user', '');
249249
$ingest->assertLatestWrite('request:0.user', '');
250250
}
251+
252+
public function test_it_does_not_actively_resolve_guards(): void
253+
{
254+
Route::get('/test', fn () => 'ok');
255+
256+
$response = $this->get('/test');
257+
258+
$response->assertOk();
259+
$this->assertFalse(Auth::hasResolvedGuards());
260+
}
251261
}

tests/Unit/UserProviderTest.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tests\Unit;
44

5+
use App\Models\User;
56
use Illuminate\Auth\GenericUser;
67
use Illuminate\Support\Facades\Auth;
78
use Laravel\Nightwatch\UserProvider;
@@ -15,6 +16,13 @@
1516

1617
class UserProviderTest extends TestCase
1718
{
19+
protected function setUp(): void
20+
{
21+
$this->forceRequestExecutionState();
22+
23+
parent::setUp();
24+
}
25+
1826
public function test_it_limits_the_length_of_the_user_identifier(): void
1927
{
2028
Auth::login(new GenericUser([
@@ -41,12 +49,12 @@ public function test_it_can_lazily_retrieve_the_user(): void
4149

4250
public function test_it_can_remember_an_authenticated_user_and_limits_the_length_of_their_identifier(): void
4351
{
44-
$provider = new UserProvider($this->app['auth'], fn () => [], fn () => fn () => null);
45-
$provider->remember($user = new GenericUser([
52+
Auth::login((new User([
4653
'id' => str_repeat('x', 1000),
47-
]));
54+
]))->setKeyType('string'));
55+
Auth::logout();
4856

49-
$this->assertSame(str_repeat('x', 255), $provider->id()->jsonSerialize());
57+
$this->assertSame(str_repeat('x', 255), $this->core->executionState->user->id());
5058
}
5159

5260
public function test_it_only_reports_exceptions_occurring_while_resolving_user_ids_once_before_user_is_available(): void

0 commit comments

Comments
 (0)