PHPackages                             rupadana/filament-shield - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. rupadana/filament-shield

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

rupadana/filament-shield
========================

Filament support for `spatie/laravel-permission`.

3.0.5(2y ago)1370MITPHPPHP ^8.1

Since Sep 11Pushed 1y agoCompare

[ Source](https://github.com/rupadana/filament-shield)[ Packagist](https://packagist.org/packages/rupadana/filament-shield)[ Docs](https://github.com/bezhansalleh/filament-shield)[ GitHub Sponsors](https://github.com/bezhanSalleh)[ RSS](/packages/rupadana-filament-shield/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (12)Versions (7)Used By (0)

[![filament-shield-art](https://user-images.githubusercontent.com/10007504/148662315-35d4bd74-fc1c-4f8c-8c02-689309b414b0.png)](https://github.com/bezhansalleh/filament-shield) [ ![FILAMENT 8.x](https://camo.githubusercontent.com/346fbf187afb5a0f0c9df87435c75f75a07a8e7c675fdc06e848f8921fb0cc7f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f46494c414d454e542d332e782d4542423330343f7374796c653d666f722d7468652d6261646765) ](https://filamentadmin.com/docs/2.x/admin/installation) [ ![Packagist](https://camo.githubusercontent.com/126fd175ebfd3bafa03281545d08de7b31567dea23450ae51545f1059b450119/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f62657a68616e73616c6c65682f66696c616d656e742d736869656c642e7376673f7374796c653d666f722d7468652d6261646765266c6f676f3d7061636b6167697374) ](https://packagist.org/packages/bezhansalleh/filament-shield) [ ![Tests Passing](https://camo.githubusercontent.com/8682b7e5b6a5d23ff0b7c5c1bf7b821b0c5bb6c1ec8755566003b3dbd1cedd24/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f62657a68616e73616c6c65682f66696c616d656e742d736869656c642f72756e2d74657374732e796d6c3f7374796c653d666f722d7468652d6261646765266c6f676f3d676974687562266c6162656c3d7465737473) ](https://github.com/bezhansalleh/filament-shield/actions?query=workflow%3Arun-tests+branch%3A3.x) [ ![Code Style Passing](https://camo.githubusercontent.com/fe92ca3e6cbf868290f2655454d6a51d6b1fd2dd9cc65f137acb50aff3307842/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f62657a68616e73616c6c65682f66696c616d656e742d736869656c642f6c61726176656c2d70696e742e796d6c3f7374796c653d666f722d7468652d6261646765266c6f676f3d676974687562266c6162656c3d636f64652532307374796c65) ](https://github.com/bezhansalleh/filament-shield/actions?query=workflow%3A)[ ![Downloads](https://camo.githubusercontent.com/5f5332676140ec8b3526c6bf3f8baa4322ecef1117f53583723fc40895ee5797/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f62657a68616e73616c6c65682f66696c616d656e742d736869656c642e7376673f7374796c653d666f722d7468652d6261646765) ](https://packagist.org/packages/bezhansalleh/filament-shield)

Shield
======

[](#shield)

The easiest and most intuitive way to add access management to your Filament Admin:

- 🔥 **Resources**
- 🔥 **Pages**
- 🔥 **Widgets**
- 🔥 **Custom Permissions**

Note

For **Filament 2.x** use **[2.x](https://github.com/bezhanSalleh/filament-shield/tree/2.x)** branch

Important

Prior to `v3.1.0` Shield supported [spatie/laravel-permission](https://packagist.org/packages/spatie/laravel-permission):`^5.0` and now it supports version `^6.0`. Which has some breaking changes around migrations. If you are upgrading from a version prior to `v3.1.0` please make sure to remove the old migration file and republish the new one.

Installation
------------

[](#installation)

1. Install the package via composer:

```
composer require bezhansalleh/filament-shield
```

2. 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;

    // ...
}
```

3. Publish the `config` file then setup your configuration:

```
php artisan vendor:publish --tag=filament-shield-config
```

4. Register the plugin for the Filament Panels you want

```
public function panel(Panel $panel): Panel
{
    return $panel
        ->plugins([
            \BezhanSalleh\FilamentShield\FilamentShieldPlugin::make()
        ]);
}
```

5. Now run the following command to install shield:

```
php artisan shield:install
```

Follow the prompts and enjoy!

Filament Panels
---------------

[](#filament-panels)

If you want to enable `Shield` for more than one panel then you need to register the plugin for each panel as mentioned above.

### Panel Access

[](#panel-access)

Shield comes with the `HasPanelShield` trait which provides an easy way to integrate Shield's conventions with the Filament's panel access system.

The `HasPanelShield` trait provides an implementation for the `canAccessPanel` method, determining access based on whether the user possesses the `super_admin` role or the `panel_user` role. It also assigns the `panel_user` role to the user upon creation and removes it upon deletion. Ofcourse the role names can be changed from the plugin's configuration file.

```
use BezhanSalleh\FilamentShield\Traits\HasPanelShield;
use Filament\Models\Contracts\FilamentUser;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable implements FilamentUser
{
    use HasRoles;
    use HasPanelShield;
    // ...
}
```

#### Resources

[](#resources)

Generally there are two scenarios that shield handles permissions for your `Filament` resources.

##### Default

[](#default)

Out of the box `Shield` handles the predefined permissions for `Filament` resources. So if that's all that you need you are all set. If you need to add a single permission (for instance `lock`) and have it available for all your resources just append it to the following `config` key:

```
    permission_prefixes' => [
        'resource' => [
            'view',
            'view_any',
            'create',
            'update',
            'restore',
            'restore_any',
            'replicate',
            'reorder',
            'delete',
            'delete_any',
            'force_delete',
            'force_delete_any',
            'lock'
        ],
        ...
    ],
```

💡 Now you are thinking **`what if I need a permission to be only available for just one resource?`**No worries, that's where [Custom Permissions](#custom-permissions) come to play.

##### Custom Permissions

[](#custom-permissions)

To define custom permissions per `Resource` your `Resource` must implement the `HasShieldPermissions` contract. This contract has a `getPermissionPrefixes()` method which returns an array of permission prefixes for your `Resource`.

Consider you have a `PostResource` and you want a couple of the predefined permissions plus a new permission called `publish_posts` to be only available for `PostResource` only.

```
