PHPackages                             abdelhamiderrahmouni/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. abdelhamiderrahmouni/filament-shield

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

abdelhamiderrahmouni/filament-shield
====================================

Filament support for `spatie/laravel-permission`.

v3.4.3(10mo ago)024MITPHPPHP ^8.1

Since Jun 25Pushed 10mo agoCompare

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

READMEChangelogDependencies (14)Versions (5)Used By (0)

[![filament-shield-art](https://user-images.githubusercontent.com/10007504/148662315-35d4bd74-fc1c-4f8c-8c02-689309b414b0.png)](https://github.com/abdelhamiderrahmouni/filament-shield) [ ![FILAMENT 8.x](https://camo.githubusercontent.com/346fbf187afb5a0f0c9df87435c75f75a07a8e7c675fdc06e848f8921fb0cc7f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f46494c414d454e542d332e782d4542423330343f7374796c653d666f722d7468652d6261646765) ](https://filamentadmin.com/docs/2.x/admin/installation) [ ![Packagist](https://camo.githubusercontent.com/6b7e882b20f7539561cb10e65a1607f4991ea06fec126c95f7453fea5dace3d3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f616264656c68616d696465727261686d6f756e692f66696c616d656e742d736869656c642e7376673f7374796c653d666f722d7468652d6261646765266c6f676f3d7061636b6167697374) ](https://packagist.org/packages/abdelhamiderrahmouni/filament-shield) [ ![Tests Passing](https://camo.githubusercontent.com/4189629d6626dbfea6d35bc20d86e957dd43f9a01976af9f37d16dacd5f11ae8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f616264656c68616d696465727261686d6f756e692f66696c616d656e742d736869656c642f72756e2d74657374732e796d6c3f7374796c653d666f722d7468652d6261646765266c6f676f3d676974687562266c6162656c3d7465737473) ](https://github.com/abdelhamiderrahmouni/filament-shield/actions?query=workflow%3Arun-tests+branch%3A3.x) [ ![Code Style Passing](https://camo.githubusercontent.com/2e01b9fdf7d966d1603d27c950c8c320cdfa1a8156bf0c0d373fa6ee883a9741/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f616264656c68616d696465727261686d6f756e692f66696c616d656e742d736869656c642f6c61726176656c2d70696e742e796d6c3f7374796c653d666f722d7468652d6261646765266c6f676f3d676974687562266c6162656c3d636f64652532307374796c65) ](https://github.com/abdelhamiderrahmouni/filament-shield/actions?query=workflow%3A)[ ![Downloads](https://camo.githubusercontent.com/8f0b984b9c6942473aa1236122cce18fadfc4d458d49f29d6be1f68962a61944/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f616264656c68616d696465727261686d6f756e692f66696c616d656e742d736869656c642e7376673f7374796c653d666f722d7468652d6261646765) ](https://packagist.org/packages/abdelhamiderrahmouni/filament-shield)

Shield
======

[](#shield)

The easiest and most intuitive way to add access management to your Filament Panels.

Features
--------

[](#features)

- 🛡️ **Complete Authorization Management**
    - Resource Permissions
    - Page Permissions
    - Widget Permissions
    - Custom Permissions
- 🔄 **Multi-tenancy Support**
- 🚀 **Easy Setup &amp; Configuration**
- 🎨 **Best UI**
- 📦 **Policy Generation**
- 🌐 **Translations Support**

Compatibility
-------------

[](#compatibility)

Package VersionFilament Version[2.x](https://github.com/abdelhamiderrahmouni/filament-shield/tree/2.x)2.x**3.x****3.x**[4.x](https://github.com/abdelhamiderrahmouni/filament-shield/tree/4.x)4.x**Table of Contents**- [Shield](#shield)
    - [Features](#features)
    - [Requirements](#requirements)
    - [Installation](#installation)
        - [1. Install Package](#1-install-package)
        - [2. Configure Auth Provider](#2-configure-auth-provider)
        - [3. Setup Shield](#3-setup-shield)
        - [4. Install for Panel](#4-install-for-panel)
    - [Usage](#usage)
        - [Configuration](#configuration)
        - [Resources](#resources)
            - [Default](#default)
            - [Custom Permissions](#custom-permissions)
            - [Configure Permission Identifier](#configure-permission-identifier)
            - [Custom Navigation Group](#custom-navigation-group)
        - [Pages](#pages)
            - [Pages Hooks](#pages-hooks)
            - [Pages Redirect Path](#pages-redirect-path)
        - [Widgets](#widgets)
        - [Policies](#policies)
            - [Path](#path)
            - [Policy Discovery](#policy-discovery)
                - [Using Laravel 10](#using-laravel-10)
                - [Using Laravel 11](#using-laravel-11)
            - [Users (Assigning Roles to Users)](#users-assigning-roles-to-users)
            - [Layout Customization](#layout-customization)
    - [Available Commands](#available-commands)
        - [Prohibited Commands](#prohibited-commands)
        - [Core Commands](#core-commands)
        - [Generate Command Options](#generate-command-options)
            - [Translations](#translations)
    - [Testing](#testing)
    - [Changelog](#changelog)
    - [Contributing](#contributing)
    - [Security Vulnerabilities](#security-vulnerabilities)
    - [Credits](#credits)
    - [License](#license)

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

[](#installation)

### 1. Install Package

[](#1-install-package)

```
composer require abdelhamiderrahmouni/filament-shield
```

### 2. Configure Auth Provider

[](#2-configure-auth-provider)

#### 2.1. Publish the config and setup your auth provider model.

[](#21-publish-the-config-and-setup-your-auth-provider-model)

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

```
//config/filament-shield.php
...
    'auth_provider_model' => [
        'fqcn' => 'App\\Models\\User',
    ],
...
```

#### 2.2 Add the `HasRoles` trait to your auth provider model:

[](#22-add-the-hasroles-trait-to-your-auth-provider-model)

```
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    use HasRoles;
}
```

### 3. Setup Shield

[](#3-setup-shield)

3.1 **Without Tenancy:**

```
php artisan shield:setup
```

3.2 **With Tenancy:**

```
php artisan shield:setup --tenant=App\\Models\\Team
# Replace Team with your tenant model
```

This command will:

- Publish core package config
- Publish core package migrations
- Run initial migrations
- Publish shield config
- Configure tenancy if specified

### 4. Install for Panel

[](#4-install-for-panel)

The install command will register the plugin for your panel automatically. Choose the appropriate installation method:

4.1 **Without Tenancy:**

```
php artisan shield:install admin
# Replace 'admin' with your panel ID
```

Or instead of the above command you can register the plugin manually in your `xPanelProvider`:

```
    ->plugins([
        \AbdelhamidErrahmouni\FilamentShield\FilamentShieldPlugin::make(),
    ])
```

4.2 **With Tenancy:**

```
php artisan shield:install admin --tenant --generate-relationships
# Replace 'admin' with your panel ID
```

Or instead of the above command you can register the plugin and enable tenancy manually in your `xPanelProvider`:

```
    ->tenant(YourTenantModel::class)
    ->tenantMiddleware([
        \AbdelhamidErrahmouni\FilamentShield\Middleware\SyncShieldTenant::class,
    ], isPersistent: true)
    ->plugins([
        \AbdelhamidErrahmouni\FilamentShield\FilamentShieldPlugin::make(),
    ])
```

This command will:

- Register Shield plugin for your panel
- If `--tenant` flag is provided:
    - Activates tenancy features
    - Makes the panel tenantable
    - Adds `SyncShieldTenant` middleware to the panel
    - Configures tenant model from the config
- If `--generate-relationships` flag is provided:
    - Generates required relationships between resource models and the tenant model
    - Adds necessary relationship methods in both the resource and tenant models

Usage
-----

[](#usage)

#### Configuration

[](#configuration)

See [config/filament-shield.php](config/filament-shield.php) for full configuration options.

#### 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.

```
