From 069b57c7b339d2382227dc6a50a9bb9ccb807139 Mon Sep 17 00:00:00 2001 From: Ivo Spijkerman Date: Sun, 31 Aug 2025 21:12:03 +0200 Subject: [PATCH] add filament and admin user --- .env.example | 3 + app/Models/User.php | 16 ++++- app/Providers/Filament/AdminPanelProvider.php | 59 +++++++++++++++++++ bootstrap/providers.php | 1 + config/admin.php | 6 ++ .../2025_08_31_185011_add_admin_user.php | 28 +++++++++ docker/start.sh | 1 + 7 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 app/Providers/Filament/AdminPanelProvider.php create mode 100644 config/admin.php create mode 100644 database/migrations/2025_08_31_185011_add_admin_user.php diff --git a/.env.example b/.env.example index 4ca9a36b..df55add5 100644 --- a/.env.example +++ b/.env.example @@ -4,6 +4,9 @@ APP_KEY= APP_DEBUG=true APP_URL=http://localhost +ADMIN_EMAIL=ivo@spijkerman.com +ADMIN_PASS= + APP_LOCALE=nl APP_FALLBACK_LOCALE=nl APP_FAKER_LOCALE=nl_NL diff --git a/app/Models/User.php b/app/Models/User.php index 749c7b77..1fa72858 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -3,13 +3,20 @@ namespace App\Models; // use Illuminate\Contracts\Auth\MustVerifyEmail; +use Database\Factories\UserFactory; +use Filament\Models\Contracts\FilamentUser; +use Filament\Panel; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; -class User extends Authenticatable +/** + * @mixin Builder + */ +class User extends Authenticatable implements FilamentUser { - /** @use HasFactory<\Database\Factories\UserFactory> */ + /** @use HasFactory */ use HasFactory, Notifiable; /** @@ -45,4 +52,9 @@ class User extends Authenticatable 'password' => 'hashed', ]; } + + public function canAccessPanel(Panel $panel): bool + { + return $this->name === 'admin'; + } } diff --git a/app/Providers/Filament/AdminPanelProvider.php b/app/Providers/Filament/AdminPanelProvider.php new file mode 100644 index 00000000..8ce4eb04 --- /dev/null +++ b/app/Providers/Filament/AdminPanelProvider.php @@ -0,0 +1,59 @@ +default() + ->id('admin') + ->path('admin') + ->login() + ->colors([ + 'primary' => Color::Amber, + ]) + ->discoverResources(in: app_path('Filament/Resources'), for: 'App\Filament\Resources') + ->discoverPages(in: app_path('Filament/Pages'), for: 'App\Filament\Pages') + ->pages([ + Dashboard::class, + ]) + ->discoverWidgets(in: app_path('Filament/Widgets'), for: 'App\Filament\Widgets') + ->widgets([ + AccountWidget::class, + FilamentInfoWidget::class, + ]) + ->middleware([ + EncryptCookies::class, + AddQueuedCookiesToResponse::class, + StartSession::class, + AuthenticateSession::class, + ShareErrorsFromSession::class, + VerifyCsrfToken::class, + SubstituteBindings::class, + DisableBladeIconComponents::class, + DispatchServingFilamentEvent::class, + ]) + ->authMiddleware([ + Authenticate::class, + ]); + } +} diff --git a/bootstrap/providers.php b/bootstrap/providers.php index 38b258d1..22744d11 100644 --- a/bootstrap/providers.php +++ b/bootstrap/providers.php @@ -2,4 +2,5 @@ return [ App\Providers\AppServiceProvider::class, + App\Providers\Filament\AdminPanelProvider::class, ]; diff --git a/config/admin.php b/config/admin.php new file mode 100644 index 00000000..da123cd7 --- /dev/null +++ b/config/admin.php @@ -0,0 +1,6 @@ + env('ADMIN_EMAIL'), + 'password' => env('ADMIN_PASS'), +]; diff --git a/database/migrations/2025_08_31_185011_add_admin_user.php b/database/migrations/2025_08_31_185011_add_admin_user.php new file mode 100644 index 00000000..821aed4f --- /dev/null +++ b/database/migrations/2025_08_31_185011_add_admin_user.php @@ -0,0 +1,28 @@ +create([ + 'name' => 'admin', + 'email' => config('admin.email'), + 'password' => Hash::make(config('admin.password')), + ]); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + User::where('name', '=', 'admin')->delete(); + } +}; diff --git a/docker/start.sh b/docker/start.sh index 31f197c3..1c5c0f9d 100755 --- a/docker/start.sh +++ b/docker/start.sh @@ -10,6 +10,7 @@ php artisan migrate --force php artisan config:cache php artisan route:cache php artisan view:cache +php artisan filament:optimize # Start supervisor /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf