jhonoryza/filament-simple-role-permission

filament scaffolding for role and permission

0.0.4 2024-08-27 06:50 UTC

This package is auto-updated.

Last update: 2024-11-27 07:21:49 UTC


README

Total Downloads Latest Stable Version License

This package is a filament scaffolding for simple role and permission, provides filament resources, models, migration, seeder and policy generator

Screenshot

ERD

Requirements

  • PHP >= 8.2
  • Laravel >= 10.0
  • Filament >= 3.0

Installation

You can install the package via composer

composer require --dev jhonoryza/filament-simple-role-permission

Then you need to run this command to publish the scaffolding

php artisan filament-simple-role-permission:install

Then you can migrate your database

php artisan migrate

Edit app\Models\User.php add HasRole trait and predefined roles

use App\Models\Concern\HasRole;
class User extends Authenticatable
{
    use HasRole;

    const SUPER = 'super-admin';

    const ADMIN = 'admin';

    public static function getPredefined(): array
    {
        return [
            self::SUPER,
            self::ADMIN,
        ];
    }

    protected $fillable = [
        // ... etc

        'role_id',
    ]
}

const SUPER is used in UserSeeder class

Then seed it with some default role and permission

php artisan db:seed --class=PermissionSeeder
php artisan db:seed --class=RoleSeeder
php artisan db:seed --class=UserSeeder

Then generate all policy file in app\Policies directory base on predefined permissions from Permission model

php artisan policy:generate

Configuration

You can configure predefined permissions by adjust function getPredefined() and function match() in Permission model class like this

public static function getPredefined(): array
{
    return [
        'users' => [
            'view-any',
            'view',
            'create',
            'update',
            'delete',
            'bulk-delete',
        ],
        'roles' => [
            'view-any',
            'view',
            'create',
            'update',
            'delete',
            'bulk-delete',
        ],
        'permissions' => [
            'view-any',
            'view',
            'create',
            'update',
            'delete',
            'bulk-delete',
        ],
    ];
}

public static function match($permission): string
{
    return match ($permission) {
        'view-any' => 'viewAnyPermission',
        'view' => 'viewPermission',
        'create' => 'createPermission',
        'update' => 'updatePermission',
        'delete' => 'deletePermission',
        'bulk-delete' => 'bulkDeletePermission',
        default => '',
    };
}

Publishing policy stubs

To customize Policy class template:

php artisan vendor:publish --tag="simple-role-permission-stubs"

Security

If you discover any security related issues, please create an issue.

License

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