Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jhonoryza/filament-simple-role-permission

Laravel Filament Simple Role and Permission Plugin Scaffolding
https://github.com/jhonoryza/filament-simple-role-permission

filamentphp laravel livewire permission role

Last synced: 2 months ago
JSON representation

Laravel Filament Simple Role and Permission Plugin Scaffolding

Awesome Lists containing this project

README

        

Simple Role Permission Filament




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

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

Then you need to run this command to publish the scaffolding

```bash
php artisan filament-simple-role-permission:install
```

Then you can migrate your database

```bash
php artisan migrate
```

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

```php
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

```bash
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

```bash
php artisan policy:generate
```

## Configuration

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

```php
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:

```bash
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](LICENSE.md) for more information.