Skip to content

The easiest and most intuitive way to add access management to your Filament Admin Resources, Pages & Widgets through `spatie/laravel-permission`

License

Notifications You must be signed in to change notification settings

yammycode/filament-shield

Β 
Β 

Repository files navigation

filament-shield-art

FILAMENT 8.x Packagist Tests Passing Code Style Passing Downloads


Filament Shield

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

  • πŸ”₯ Resources
  • πŸ”₯ Pages
  • πŸ”₯ Widgets
  • πŸ”₯ Settings New

Support Filament

filament-logo

Upgrade

To upgrade to the latest release first run:

composer update

v2.x

Note

Minimum Filament Requirement is now 2.13. v2.x uses database to manage Settings. So you need Delete Shield folder from Filament\Resources and run the following command as well and then go to the settings page and update the settings based on filament-shield config file and click on Save & Generate, then delete the config file.

php artisan shield:install --fresh

Warning

if you have custom permissions and/or updated policies running the above command will overwrite everything.

So if you do not want to break anything just follow along the below steps:

  1. Delete Shield folder from Filament\Resources
  2. Run the following commands:
php artisan vendor:publish --tag=filament-shield-migrations
php artisan vendor:publish --tag=filament-shield-seeder
  1. Open the Database\Seeders\ShieldSettingSeeder.php file and update the $settingKeys based on your config file.
  2. Run the following commands:
php artisan migrate
php artisan db:seed --class=ShieldSettingSeeder
  1. Go to Sheild's setting page and verify your settings then click on Save.

Note

for Filament prior to 2.13 use v1.1.12

Installation

  1. Install the package via composer:
composer require bezhansalleh/filament-shield
  1. 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;

    // ...
}
  1. Now run the following command to setup everything:
php artisan shield:install

Follow the prompts and enjoy!

Resource Custom Permissions

You can add custom permissions for Resources through settings page.

Pages

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.

Pages Hooks

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() {
        ...
    }
}
Pages Redirect Path

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

Widgets

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

RolePolicy

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',
];
...

Translations

Publish the translations using:

php artisan vendor:publish --tag="filament-shield-translations"

Views

Publish the Views using:

php artisan vendor:publish --tag="filament-shield-views"

Available Filament Shield Commands

- 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

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

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.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

The easiest and most intuitive way to add access management to your Filament Admin Resources, Pages & Widgets through `spatie/laravel-permission`

Resources

License

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 99.8%
  • Blade 0.2%