From 44bb20ba54d848676e9f40c8cb76f99e5faf16a6 Mon Sep 17 00:00:00 2001 From: Craig Smith <952595+phpsa@users.noreply.github.com> Date: Tue, 21 May 2024 20:34:54 +1200 Subject: [PATCH] fix: do not register routes / middlware for disabled functionality --- phpcs.xml | 80 +++++++++++++++++++ resources/views/components/banner.blade.php | 26 ------ .../views/impersonating-banner.blade.php | 23 +++++- routes/web.php | 25 ++++-- src/FilamentAuthentication.php | 7 +- .../Middleware/ImpersonatingMiddleware.php | 8 +- .../Middleware/RenewPasswordMiddleware.php | 2 +- 7 files changed, 131 insertions(+), 40 deletions(-) create mode 100644 phpcs.xml diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..a49689b --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,80 @@ + + + + The Laravel Coding Standards + + + + + + + + + + + + + + + + + + + + + + + + + + + + 10 + + + 10 + + + 10 + + + + + + + + + + + + */_ide_helper.php + */cache/* + */*.js + */*.css + */*.xml + */*.blade.php + */autoload.php + */storage/* + */docs/* + */vendor/* + */migrations/* + */config/* + */public/index.php + + app + database + config + public + resources + routes + tests + + + + + + + + + + diff --git a/resources/views/components/banner.blade.php b/resources/views/components/banner.blade.php index 470e036..e69de29 100644 --- a/resources/views/components/banner.blade.php +++ b/resources/views/components/banner.blade.php @@ -1,26 +0,0 @@ -@if(app('impersonate')->isImpersonating()) - -@php -$impersonating = Filament\Facades\Filament::getUserName(auth()->user()); -@endphp -
-
- {{ __('filament-authentication::filament-authentication.text.impersonating') }} {{ $impersonating }} - {{ __('filament-authentication::filament-authentication.text.impersonating.end') }} -
- -
-@endIf diff --git a/resources/views/impersonating-banner.blade.php b/resources/views/impersonating-banner.blade.php index 4a42c6e..3427ab1 100644 --- a/resources/views/impersonating-banner.blade.php +++ b/resources/views/impersonating-banner.blade.php @@ -1 +1,22 @@ - + + +
+
+ {{ __('filament-authentication::filament-authentication.text.impersonating') }} {{ $impersonating }} + {{ __('filament-authentication::filament-authentication.text.impersonating.end') }} +
+ +
diff --git a/routes/web.php b/routes/web.php index acc1a95..b31298f 100644 --- a/routes/web.php +++ b/routes/web.php @@ -5,14 +5,15 @@ use Phpsa\FilamentAuthentication\Actions\ImpersonateLink; use Phpsa\FilamentAuthentication\Pages\Auth\RenewPassword; -Route::get('/impersonate/stop', fn () => ImpersonateLink::leave()) - ->name('filament-authentication.stop.impersonation') - ->middleware('web'); - - - Route::name('filament.')->group(function () { foreach (Filament::getPanels() as $panel) { + if ($panel->hasPlugin('filament-authentication') === false) { + continue; + } + + /** @var \Phpsa\FilamentAuthentication\FilamentAuthentication */ + $plugin = $panel->getPlugin('filament-authentication'); + $domains = $panel->getDomains(); foreach ((empty($domains) ? [null] : $domains) as $domain) { @@ -20,8 +21,16 @@ ->middleware($panel->getMiddleware()) ->name($panel->getId() . '.') ->prefix($panel->getPath()) - ->group(function () use ($panel) { - Route::get('password/renew', RenewPassword::class)->name('auth.password.renew'); + ->group(function () use ($panel, $plugin) { + + if ($plugin->impersonateEnabled() === false) { + Route::get('/impersonate/stop', fn () => ImpersonateLink::leave()) + ->name('fa.stop.impersonation'); + } + + if ((int) config('filament-authentication.password_renew.renew_password_days_period', 0) > 0) { + Route::get('password/renew', RenewPassword::class)->name('fa.password.renew'); + } }); } } diff --git a/src/FilamentAuthentication.php b/src/FilamentAuthentication.php index bf93dc2..695a7ec 100644 --- a/src/FilamentAuthentication.php +++ b/src/FilamentAuthentication.php @@ -55,7 +55,12 @@ public function getId(): string public function register(Panel $panel): void { $panel->resources($this->resources); - $panel->middleware([ImpersonatingMiddleware::class, RenewPasswordMiddleware::class]); + if ($this->impersonateEnabled()) { + $panel->middleware([ImpersonatingMiddleware::class]); + } + if ((int) config('filament-authentication.password_renew.renew_password_days_period', 0) > 0) { + $panel->middleware([RenewPasswordMiddleware::class]); + } } public function boot(Panel $panel): void diff --git a/src/Http/Middleware/ImpersonatingMiddleware.php b/src/Http/Middleware/ImpersonatingMiddleware.php index ac8637f..04e2c88 100644 --- a/src/Http/Middleware/ImpersonatingMiddleware.php +++ b/src/Http/Middleware/ImpersonatingMiddleware.php @@ -2,10 +2,10 @@ namespace Phpsa\FilamentAuthentication\Http\Middleware; -use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Http\Response; -use Illuminate\Support\Str; +use Filament\Facades\Filament; +use Illuminate\Http\JsonResponse; use Lab404\Impersonate\Services\ImpersonateManager; class ImpersonatingMiddleware @@ -33,8 +33,10 @@ public function handle(Request $request, \Closure $next) protected function getHtmlContent($request): string { + $panel ??= Filament::getCurrentPanel()->getId(); return view('filament-authentication::impersonating-banner', [ - 'isFilament' => Str::startsWith($request->path(), config('filament.path')), + 'panel' => $panel, + 'impersonating' => Filament::getUserName(auth()->user()) ])->render(); } diff --git a/src/Http/Middleware/RenewPasswordMiddleware.php b/src/Http/Middleware/RenewPasswordMiddleware.php index e8f572d..3d85015 100644 --- a/src/Http/Middleware/RenewPasswordMiddleware.php +++ b/src/Http/Middleware/RenewPasswordMiddleware.php @@ -44,6 +44,6 @@ class_uses_recursive(FilamentAuthentication::getPlugin()->getModel('User')) } $panel ??= Filament::getCurrentPanel()->getId(); - return Redirect::guest(URL::route("filament.{$panel}.auth.password.renew")); + return Redirect::guest(URL::route("filament.{$panel}.fa.password.renew")); } }