Courses

Filament 3 From Scratch: Practical Course

Shield Plugin for Roles and Permissions

You're reading a FREE PREVIEW of a PREMIUM course.
Summary of this lesson:
- Implementing Shield package
- Managing roles and permissions
- Assigning roles to users
- Configuring access control

Until now, we checked if the user can do something by checking if users.is_admin or users.is_accountant is true.

This can be done better by using roles and permissions. And Filament has an excellent plugin for that called Shield which under the hood uses spatie/laravel-permission package.

This lesson will use the Shield package to add roles and permissions functionality.

roles


Composer Install Package

First, let's install the package via composer.

composer require bezhansalleh/filament-shield "^3.0@beta"

Then we need to add Spatie\Permission\Traits\HasRoles trait to the User Model.

app/Models/User.php:

use Spatie\Permission\Traits\HasRoles;
 
class User extends Authenticatable implements FilamentUser
{
use HasApiTokens, HasFactory, Notifiable;
use HasRoles;
 
// ...
}

Next, we need to register this package in the panel.

app/Providers/Filament/AdminPanelProvider.php:

class AdminPanelProvider extends PanelProvider
{
public function panel(Panel $panel): Panel
{
return $panel
// ...
->plugins([
\BezhanSalleh\FilamentShield\FilamentShieldPlugin::make()
]);
}
}

If you have multiple panels, you may choose to add it only for admin users. This way, only users with access to...

The full lesson is only for Premium Members.
Want to access all 24 lessons of this course? (101 min read)

You also get:

  • 76 courses
  • Premium tutorials
  • Access to repositories
  • Private Discord