From 1dfcd7d26c86176953282394d6a1444dd3302d34 Mon Sep 17 00:00:00 2001 From: KingSit3 Date: Sun, 27 Oct 2024 21:16:40 +0700 Subject: [PATCH] feat(fixes Roles #8): Superman & User --- app/Http/Middleware/SupermanMiddleware.php | 21 ++++++++++++++ app/Http/Middleware/UserMiddleware.php | 21 ++++++++++++++ app/Models/User.php | 11 +------- app/Providers/AppServiceProvider.php | 2 +- .../Filament/JamaahPanelProvider.php | 2 ++ .../Filament/SupermanPanelProvider.php | 2 ++ ...lter_users_table_add_is_superman_field.php | 28 +++++++++++++++++++ database/seeders/RoleSeeder.php | 11 +------- 8 files changed, 77 insertions(+), 21 deletions(-) create mode 100644 app/Http/Middleware/SupermanMiddleware.php create mode 100644 app/Http/Middleware/UserMiddleware.php create mode 100644 database/migrations/2024_10_27_134847_alter_users_table_add_is_superman_field.php diff --git a/app/Http/Middleware/SupermanMiddleware.php b/app/Http/Middleware/SupermanMiddleware.php new file mode 100644 index 0000000..9876bf3 --- /dev/null +++ b/app/Http/Middleware/SupermanMiddleware.php @@ -0,0 +1,21 @@ +is_superman) ? $next($request) : redirect(to: route("filament.jamaah.auth.login")); + } +} diff --git a/app/Http/Middleware/UserMiddleware.php b/app/Http/Middleware/UserMiddleware.php new file mode 100644 index 0000000..7a24568 --- /dev/null +++ b/app/Http/Middleware/UserMiddleware.php @@ -0,0 +1,21 @@ +is_superman) ? redirect(to: route("filament.superman.pages.dashboard")) : $next($request); + } +} diff --git a/app/Models/User.php b/app/Models/User.php index c6c1212..45f6fe0 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -49,19 +49,10 @@ protected function casts(): array return [ 'email_verified_at' => 'datetime', 'password' => 'hashed', + 'is_superman' => 'boolean', ]; } - public function canAccessPanel(Panel $panel): bool - { - // Only Superman can go to Krypton 🫳🫳 - if ($panel->getId() === 'superman') { - return $this->hasRole("superman"); - } - - return true; - } - public function jamaah(): BelongsToMany { return $this->belongsToMany(Jamaah::class); diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 55122f1..da4572f 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -27,7 +27,7 @@ public function boot(): void Model::unguard(); Gate::before(function (User $user, string $ability) { - return $user->hasRole("superman") ? true : null; + return $user->is_superman ? true : null; }); } } diff --git a/app/Providers/Filament/JamaahPanelProvider.php b/app/Providers/Filament/JamaahPanelProvider.php index 80d8959..f8c99c0 100644 --- a/app/Providers/Filament/JamaahPanelProvider.php +++ b/app/Providers/Filament/JamaahPanelProvider.php @@ -7,6 +7,7 @@ use App\Filament\Jamaah\Pages\Dashboard; use App\Filament\Jamaah\Pages\JamaahProfile; use App\Filament\Jamaah\Pages\JamaahRegistration; +use App\Http\Middleware\UserMiddleware; use App\Models\Jamaah; use DutchCodingCompany\FilamentSocialite\FilamentSocialitePlugin; use DutchCodingCompany\FilamentSocialite\Provider; @@ -69,6 +70,7 @@ public function panel(Panel $panel): Panel SubstituteBindings::class, DisableBladeIconComponents::class, DispatchServingFilamentEvent::class, + UserMiddleware::class ]) ->tenantMiddleware([ SyncSpatiePermissionsWithFilamentTenants::class, diff --git a/app/Providers/Filament/SupermanPanelProvider.php b/app/Providers/Filament/SupermanPanelProvider.php index 4cf3d79..a16432c 100644 --- a/app/Providers/Filament/SupermanPanelProvider.php +++ b/app/Providers/Filament/SupermanPanelProvider.php @@ -5,6 +5,7 @@ use Althinect\FilamentSpatieRolesPermissions\FilamentSpatieRolesPermissionsPlugin; use Althinect\FilamentSpatieRolesPermissions\Middleware\SyncSpatiePermissionsWithFilamentTenants; use App\Filament\Superman\Pages\Dashboard; +use App\Http\Middleware\SupermanMiddleware; use DutchCodingCompany\FilamentSocialite\FilamentSocialitePlugin; use DutchCodingCompany\FilamentSocialite\Provider; use Filament\Http\Middleware\Authenticate; @@ -62,6 +63,7 @@ public function panel(Panel $panel): Panel SubstituteBindings::class, DisableBladeIconComponents::class, DispatchServingFilamentEvent::class, + SupermanMiddleware::class ]) ->authMiddleware([ Authenticate::class, diff --git a/database/migrations/2024_10_27_134847_alter_users_table_add_is_superman_field.php b/database/migrations/2024_10_27_134847_alter_users_table_add_is_superman_field.php new file mode 100644 index 0000000..f1dda35 --- /dev/null +++ b/database/migrations/2024_10_27_134847_alter_users_table_add_is_superman_field.php @@ -0,0 +1,28 @@ +boolean("is_superman")->after("password")->default(false); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('users', function (Blueprint $table) { + $table->dropColumn("is_superman"); + }); + } +}; diff --git a/database/seeders/RoleSeeder.php b/database/seeders/RoleSeeder.php index 8889446..2229f92 100644 --- a/database/seeders/RoleSeeder.php +++ b/database/seeders/RoleSeeder.php @@ -14,19 +14,10 @@ class RoleSeeder extends Seeder */ public function run(): void { - // superman will get special treatment from kripton's Gate - $superman = Role::create( - ['name' => 'superman', "guard_name" => 'web'] - ); - $superman->givePermissionTo( - Permission::where("guard_name", "web")->get() - ); - // Admin Permissions - $admin = Role::create( + Role::create( ['name' => 'admin', "guard_name" => 'web'], ); - // $admin->givePermissionTo( // ['create jamaah', 'unpublish jamaah'] // );