Skip to content

Commit 97e5a85

Browse files
author
Bert Wijnhoven
committed
catch model-not-found errors + simplify
1 parent ffd5659 commit 97e5a85

File tree

6 files changed

+64
-46
lines changed

6 files changed

+64
-46
lines changed

phpstan-baseline.neon

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,20 @@ parameters:
1111
path: src/FilamentDeveloperLoginsPlugin.php
1212

1313
-
14-
message: "#^Call to an undefined method object\\:\\:where\\(\\)\\.$#"
14+
message: "#^Property DutchCodingCompany\\\\FilamentDeveloperLogins\\\\FilamentDeveloperLoginsPlugin\\:\\:\\$modelClass \\(class\\-string\\<Illuminate\\\\Contracts\\\\Auth\\\\Authenticatable&Illuminate\\\\Database\\\\Eloquent\\\\Model\\>\\) does not accept default value of type string\\.$#"
1515
count: 1
16-
path: src/FilamentDevelopersLogin.php
16+
path: src/FilamentDeveloperLoginsPlugin.php
1717

1818
-
19-
message: "#^Call to an undefined method Livewire\\\\Features\\\\SupportTesting\\\\Testable\\:\\:test\\(\\)\\.$#"
19+
message: "#^Call to an undefined method Illuminate\\\\Testing\\\\TestResponse\\:\\:assertDontSeeLivewire\\(\\)\\.$#"
2020
count: 3
2121
path: tests/Feature/MenuLoginsTest.php
2222

23+
-
24+
message: "#^Call to an undefined method Illuminate\\\\Testing\\\\TestResponse\\:\\:assertSeeLivewire\\(\\)\\.$#"
25+
count: 2
26+
path: tests/Feature/MenuLoginsTest.php
27+
2328
-
2429
message: "#^Access to an undefined property DutchCodingCompany\\\\FilamentDeveloperLogins\\\\Tests\\\\Fixtures\\\\TestUser\\:\\:\\$is_admin\\.$#"
2530
count: 1

resources/lang/en/auth.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66

77
'messages' => [
88
'failed' => 'Login failed, check if the user is allowed to access the panel.',
9+
'failed_not_found' => 'Login failed, the user cannot be found.',
910
],
1011
];
Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,33 @@
1-
@foreach ($users as $label => $credentials)
2-
@if ($loop->first)
3-
<div class="flex flex-col gap-y-6">
4-
<div class="relative flex items-center justify-center text-center">
5-
<div class="absolute border-t border-gray-200 w-full h-px"></div>
6-
<p class="inline-block relative bg-white text-sm p-2 rounded-full font-medium text-gray-500 dark:bg-gray-800 dark:text-gray-100">
7-
{{ __('filament-developer-logins::auth.login-as') }}
1+
@if (! empty($users))
2+
<div class="flex flex-col gap-y-6">
3+
<div class="relative flex items-center justify-center text-center">
4+
<div class="absolute border-t border-gray-200 w-full h-px"></div>
5+
<p class="inline-block relative bg-white text-sm p-2 rounded-full font-medium text-gray-500 dark:bg-gray-800 dark:text-gray-100">
6+
{{ __('filament-developer-logins::auth.login-as') }}
7+
</p>
8+
</div>
9+
10+
@if ($errors->has('developer-logins-failed'))
11+
<div class="justify-center text-center">
12+
<p class="fi-fo-field-wrp-error-message text-sm text-danger-600 dark:text-danger-400">
13+
{{ $errors->first('developer-logins-failed') }}
814
</p>
915
</div>
16+
@endif
17+
<div class="grid grid-cols-2 gap-4">
18+
@foreach ($users as $label => $credentials)
19+
<form action="{{ route('filament-developer-logins.login-as') }}" method="POST">
20+
@csrf
1021

11-
@if ($errors->has('developer-logins-failed'))
12-
<div class="justify-center text-center">
13-
<p class="fi-fo-field-wrp-error-message text-danger-600 dark:text-danger-400">
14-
{{ $errors->first('developer-logins-failed') }}
15-
</p>
16-
</div>
17-
@endif
18-
19-
<div class="grid grid-cols-2 gap-4">
20-
@endif
21-
<form action="{{ route('filament-developer-logins.login-as') }}" method="POST">
22-
@csrf
22+
<input type="hidden" name="panel_id" value="{{ \Filament\Facades\Filament::getId() }}">
23+
<input type="hidden" name="credentials" value="{{ $credentials }}">
2324

24-
<input type="hidden" name="panel_id" value="{{ \Filament\Facades\Filament::getId() }}">
25-
<input type="hidden" name="credentials" value="{{ $credentials }}">
26-
27-
<x-filament::button class="w-full" color="gray" outlined="true" type="submit">
28-
{{ "$label ($credentials)" }}
29-
</x-filament::button>
30-
</form>
31-
@if ($loop->first)
32-
</div>
25+
<x-filament::button class="w-full" color="gray" outlined="true" type="submit">
26+
{{ "$label ($credentials)" }}
27+
</x-filament::button>
28+
</form>
29+
@endforeach
3330
</div>
34-
@endif
35-
@endforeach
31+
</div>
32+
@endif
33+

src/FilamentDeveloperLoginsPlugin.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ class FilamentDeveloperLoginsPlugin implements Plugin
3030

3131
public string $column = 'email';
3232

33+
/**
34+
* @var class-string<\Illuminate\Database\Eloquent\Model&\Illuminate\Contracts\Auth\Authenticatable>
35+
*/
3336
public string $modelClass = User::class;
3437

3538
public function getId(): string
@@ -144,6 +147,9 @@ public function getColumn(): string
144147
return $this->column;
145148
}
146149

150+
/**
151+
* @param class-string<\Illuminate\Database\Eloquent\Model&\Illuminate\Contracts\Auth\Authenticatable> $modelClass
152+
*/
147153
public function modelClass(string $modelClass): static
148154
{
149155
if (! is_a($modelClass, Authenticatable::class, true)) {
@@ -155,6 +161,9 @@ public function modelClass(string $modelClass): static
155161
return $this;
156162
}
157163

164+
/**
165+
* @return class-string<\Illuminate\Database\Eloquent\Model&\Illuminate\Contracts\Auth\Authenticatable>
166+
*/
158167
public function getModelClass(): string
159168
{
160169
return $this->modelClass;

src/FilamentDevelopersLogin.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,28 @@ public function login(Panel $panel, FilamentDeveloperLoginsPlugin $plugin, strin
2121
$panel->auth()->logout();
2222
}
2323

24+
/** @var ?\Illuminate\Contracts\Auth\Authenticatable $model */
2425
$model = (new ($plugin->getModelClass()))
2526
->where($plugin->getColumn(), $credentials)
26-
->firstOrFail();
27-
28-
$panel->auth()->login($model);
29-
30-
if (
31-
($model instanceof FilamentUser) &&
32-
(! $model->canAccessPanel($panel))
33-
) {
34-
$panel->auth()->logout();
27+
->first();
3528

29+
if (! $model) {
3630
throw ValidationException::withMessages([
37-
'developer-logins-failed' => __('filament-developer-logins::auth.messages.failed'),
31+
'developer-logins-failed' => __('filament-developer-logins::auth.messages.failed_not_found'),
3832
]);
3933
}
4034

41-
session()->regenerate();
35+
if (! $model instanceof FilamentUser || $model->canAccessPanel($panel)) {
36+
$panel->auth()->login($model);
37+
38+
session()->regenerate();
39+
40+
return redirect()
41+
->to($plugin->getRedirectTo() ?? $panel->getUrl());
42+
}
4243

43-
return redirect()
44-
->to($plugin->getRedirectTo() ?? $panel->getUrl());
44+
throw ValidationException::withMessages([
45+
'developer-logins-failed' => __('filament-developer-logins::auth.messages.failed'),
46+
]);
4547
}
4648
}

tests/Fixtures/TestUser.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
class TestUser extends Model implements Authenticatable, FilamentUser
1212
{
13+
/**
14+
* @use \Illuminate\Database\Eloquent\Factories\HasFactory<\DutchCodingCompany\FilamentDeveloperLogins\Database\Factories\TestUserFactory>
15+
*/
1316
use HasFactory;
1417

1518
protected $table = 'users';

0 commit comments

Comments
 (0)