Skip to content

Commit

Permalink
feat(fixes Roles #8): Superman & User
Browse files Browse the repository at this point in the history
  • Loading branch information
KingSit3 committed Oct 27, 2024
1 parent ef05ffa commit 1dfcd7d
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 21 deletions.
21 changes: 21 additions & 0 deletions app/Http/Middleware/SupermanMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\Response;

class SupermanMiddleware
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
return (Auth::user()->is_superman) ? $next($request) : redirect(to: route("filament.jamaah.auth.login"));
}
}
21 changes: 21 additions & 0 deletions app/Http/Middleware/UserMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\Response;

class UserMiddleware
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
return (Auth::user()?->is_superman) ? redirect(to: route("filament.superman.pages.dashboard")) : $next($request);
}
}
11 changes: 1 addition & 10 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}
}
2 changes: 2 additions & 0 deletions app/Providers/Filament/JamaahPanelProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -69,6 +70,7 @@ public function panel(Panel $panel): Panel
SubstituteBindings::class,
DisableBladeIconComponents::class,
DispatchServingFilamentEvent::class,
UserMiddleware::class
])
->tenantMiddleware([
SyncSpatiePermissionsWithFilamentTenants::class,
Expand Down
2 changes: 2 additions & 0 deletions app/Providers/Filament/SupermanPanelProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,6 +63,7 @@ public function panel(Panel $panel): Panel
SubstituteBindings::class,
DisableBladeIconComponents::class,
DispatchServingFilamentEvent::class,
SupermanMiddleware::class
])
->authMiddleware([
Authenticate::class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->boolean("is_superman")->after("password")->default(false);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn("is_superman");
});
}
};
11 changes: 1 addition & 10 deletions database/seeders/RoleSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']
// );
Expand Down

0 comments on commit 1dfcd7d

Please sign in to comment.