Demonstrates how to use spatie/laravel-permission package in Filament and auto-assign a role when the user registers for the application.
The registration form is enabled in AdminPanelProvider
.
app/Providers/Filament/AdminPanelProvider.php:
class AdminPanelProvider extends PanelProvider{ public function panel(Panel $panel): Panel { return $panel ->default() ->id('admin') ->path('admin') ->login() ->registration() // here we enable registration feature // ... }}
The spatie/laravel-permission package is used for the roles and permissions. Two roles are seeded: admin and user.
When a new user registers to the application, the user role is assigned using the closure Eloquent event created
.
app/Models/User.php:
class User extends Authenticatable implements FilamentUser{ // ... protected static function booted(): void { static::created(function (User $user) { $user->assignRole('user'); }); } // ...}
In the Admin Panel, we have two Filament resources: TaskResource
and UserResource
.
The UserResource
menu item "Users" can only be seen by the user with an admin
role.
For that...