The easiest and most intuitive way to add access management to your Filament Admin:
- 🔥 Resources
- 🔥 Pages
- 🔥 Widgets
- 🔥 Settings
To upgrade to the latest release first run:
composer update
Note Minimum Filament Requirement is now
2.13
.
Before following along, backup your current config
first.
-
Upgrade from
1.x
- Delete
Shield
folder fromApp\Filament\Resources
- Delete
filament-shield
fromresources\lang\vendor
- Delete
filament-shield.php
fromConfig
- Now either do
2.Upgrade only
or3.Upgrade and enable Setting Page
- Delete
-
Upgrade only
-
Publish the
Config
:php artisan vendor:publish --tag=filament-shield-config --force
-
Configure: Update the new
published config
based on yourbacked-up config
-
Install:
php artisan shield:install --fresh
-
Generate:
php artisan shield:generate
-
-
Upgrade and enable
Setting
Page-
Follow Upgrade only's step 1 & 2
-
Run the following command
php artisan shield:install --fresh --setting
-
Note for Filament prior to 2.13 use v1.1.12
- Install the package via composer:
composer require bezhansalleh/filament-shield
- Add the
Spatie\Permission\Traits\HasRoles
trait to your User model(s):
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
use HasRoles; //or HasFilamentShield
// ...
}
- Publish the
config
using:
php artisan vendor:publish --tag=filament-shield-config
- Setup your configuration
<?php
return [
'shield_resource' => [
'slug' => 'shield/roles',
'navigation_sort' => -1,
],
'auth_provider_model' => [
'fqcn' => 'App\\Models\\User'
],
'settings' => [
'enabled' => false,
],
'super_admin' => [
'enabled' => true,
'name' => 'super_admin'
],
'filament_user' => [
'enabled' => false,
'name' => 'filament_user'
],
'permission_prefixes' => [
'resource' => [
'view',
'view_any',
'create',
'update',
'restore',
'restore_any',
'replicate',
'delete',
'delete_any',
'force_delete',
'force_delete_any',
],
'page' => 'page',
'widget' => 'widget',
],
'entities' => [
'pages' => true,
'widgets' => true,
'resources' => true,
'custom_permissions' => false,
],
'generator' => [
'option' => 'policies_and_permissions'
],
'exclude' => [
'enabled' => true,
'pages' => [
'Dashboard',
],
'widgets' => [
'AccountWidget','FilamentInfoWidget',
],
'resources' => [],
],
'register_role_policy' => [
'enabled' => false
],
];
- Now run the following command to install shield:
- Install only
php artisan shield:install
- Install and Enable
Setting
Pagephp artisan shield:install --setting
- Install only
Follow the prompts and enjoy!
You can add custom permissions for Resources
through settings page.
If you have generated permissions for Pages
you can toggle the page's navigation from sidebar and restricted access to the page. You can set this up manually but this package comes with a HasPageShield
trait to speed up this process. All you have to do is use the trait in you pages:
<?php
namespace App\Filament\Pages;
use ...;
use BezhanSalleh\FilamentShield\Traits\HasPageShield;
class MyPage extends Page
{
use HasPageShield;
...
}
📕 HasPageShield
uses the booted
method to check the user's permissions and makes sure to execute the booted
page method in the parent page if exists.
However if you need to perform some methods before and after the booted method you can declare the next hooks methods in your filament page.
<?php
namespace App\Filament\Pages;
use ...;
use BezhanSalleh\FilamentShield\Traits\HasPageShield;
class MyPage extends Page
{
use HasPageShield;
...
protected function beforeBooted : void() {
...
}
protected function afterBooted : void() {
...
}
/**
* Hook to perform an action before redirect if the user
* doesn't have access to the page.
* */
protected function beforeShieldRedirects : void() {
...
}
}
HasPageShield
uses the config('filament.path')
value by default to perform the shield redirection. If you need to overwrite the rediretion path, just add the next method to your page:
<?php
namespace App\Filament\Pages;
use ...;
use BezhanSalleh\FilamentShield\Traits\HasPageShield;
class MyPage extends Page
{
use HasPageShield;
...
protected function getShieldRedirectPath(): string {
return '/'; // redirect to the root index...
}
}
if you have generated permissions for Widgets
you can toggle their state based on whether a user have permission or not. You can set this up manually but this package comes with a HasWidgetShield
trait to speed up this process. All you have to do is use the trait in you widgets:
<?php
namespace App\Filament\Widgets;
use ...;
use BezhanSalleh\FilamentShield\Traits\HasWidgetShield;
class IncomeWidget extends LineChartWidget
{
use HasWidgetShield;
...
}
You can skip this if have set the 'register_role_policy' => true
in the config.
To ensure RoleResource
access via RolePolicy
you would need to add the following to your AuthServiceProvider
:
//AuthServiceProvider.php
...
protected $policies = [
'Spatie\Permission\Models\Role' => 'App\Policies\RolePolicy',
];
...
Publish the translations using:
php artisan vendor:publish --tag="filament-shield-translations"
Publish the Views using:
php artisan vendor:publish --tag="filament-shield-views"
- install # One Command to Rule them All 🔥
- generate # (Re)Discovers Filament resources and (re)generates Permissions and Policies.
- create # Create Permissions and/or Policy for the given Filament Resource Model
- super-admin # Create a user with super_admin role
composer test
Please see CHANGELOG for more information on what has changed recently.
If you want to contribute to this packages, you may want to test it in a real Filament project:
- Fork this repository to your GitHub account.
- Create a Filament app locally.
- Clone your fork in your Filament app's root directory.
- In the
/filament-shield
directory, create a branch for your fix, e.g.fix/error-message
.
Install the packages in your app's composer.json
:
"require": {
"bezhansalleh/filament-shield": "dev-fix/error-message as main-dev",
},
"repositories": [
{
"type": "path",
"url": "filament-shield"
}
]
Now, run composer update
.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.