Git Product home page Git Product logo

filament-shield-multi-tenacy's Introduction

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
  • ๐Ÿ”ฅ Custom Permissions

Table of contents


Upgrade

To upgrade to the latest release first run:

composer update

v2.x

Note Minimum Filament Requirement is now 2.13. for older versions use v1.1.12

Before following along, backup your current config first.

  1. Upgrade from 1.x

    1. Delete Shield folder from App\Filament\Resources
    2. Delete filament-shield from resources\lang\vendor or lang\vendor
    3. Delete filament-shield from resources\views\vendor
    4. Delete filament-shield.php from Config
    5. Now follow Upgrade only
  2. Upgrade only 2.x

    1. Publish the Config:

      php artisan vendor:publish --tag=filament-shield-config --force
    2. Configure: Update the new published config based on your backed-up config

    3. Install:

      php artisan shield:upgrade

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; //or HasFilamentShield

    // ...
}
  1. Publish the config using:
php artisan vendor:publish --tag=filament-shield-config
  1. Setup your configuration
<?php

    return [

          'shield_resource' => [
              'slug' => 'shield/roles',
              'navigation_sort' => -1,
              'navigation_badge' => true,
              'navigation_group' => true,
          ],

          'auth_provider_model' => [
              'fqcn' => 'App\\Models\\User'
          ],

          'super_admin' => [
              'enabled' => true,
              'name'  => 'super_admin',
              'define_via_gate' => false,
              'intercept_gate' => 'before' // after
          ],

          'filament_user' => [
              'enabled' => true,
              'name' => 'filament_user'
          ],

          'permission_prefixes' => [
              'resource' => [
                  'view',
                  'view_any',
                  'create',
                  'update',
                  'restore',
                  'restore_any',
                  'replicate',
                  'reorder',
                  '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' => true
          ],
    ];
  1. Now run the following command to install shield:
php artisan shield:install

Follow the prompts and enjoy!

Resource Custom Permissions

You can add custom permissions for Resources through Config file.

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

Role Policy

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

Third-Party Plugins

Shield also generates policies and permissions for third-party plugins and to enforce the generated policies you will need to register them in your application's AuthServiceProvider:

...
class AuthServiceProvider extends ServiceProvider
{
    ...
    protected $policies = [
        ...,
        'Ramnzys\FilamentEmailLog\Models\Email' => 'App\Policies\EmailPolicy';

    ];

Same applies for models inside folders.

Translations

Publish the translations using:

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

Available Filament Shield Commands

shield:doctor

  • Show useful info about Filament Shield.

shield:install

Setup Core Package requirements and Install Shield. Accepts the following flags:

  • --fresh re-run the migrations
  • --only Only setups shield without generating permissions and creating super-admin

shield:generate

Generate Permissions and/or Policies for Filament entities. Accepts the following flags:

  • --all Generate permissions/policies for all entities
  • --option[=OPTION] Override the config generator option(policies_and_permissions,policies,permissions)
  • --resource[=RESOURCE] One or many resources separated by comma (,)
  • --page[=PAGE] One or many pages separated by comma (,)
  • --widget[=WIDGET] One or many widgets separated by comma (,)
  • --exclude Exclude the given entities during generation
  • --ignore-config-exclude Ignore config exclude option during generation

shield:super-admin

Create a user with super_admin role.

  • Accepts an --user= argument that will use the provided ID to find the user to be made super admin.

shield:upgrade

  • Upgrade shield.

Testing

composer test

Changelog

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

Contributing

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.

filament-shield-multi-tenacy's People

Contributors

bezhansalleh avatar dependabot[bot] avatar atmonshi avatar github-actions[bot] avatar intrepidws avatar felipe-balloni avatar mohamedsabil83 avatar jvkassi avatar pathros avatar simonbuehler avatar oyepez003 avatar hadyfayed avatar pxlrbt avatar sten avatar slamservice avatar alperenersoy avatar sayasuhendra avatar nicolasbaud avatar nathanaelgt avatar trk avatar eugenefvdm avatar brunolipe-a avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.