This package is a filament scaffolding for simple role and permission, provides filament resources, models, migration, seeder and policy generator
- PHP >= 8.2
- Laravel >= 10.0
- Filament >= 3.0
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
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 => '',
};
}
To customize Policy class template:
php artisan vendor:publish --tag="simple-role-permission-stubs"
If you discover any security related issues, please create an issue.
The MIT License (MIT). Please see License File for more information.