Warning
This package is no longer maintained
Easily define permissions for Filament Resources & Relation Managers
You can install the package via composer:
composer require z3d0x/filament-simple-permissions
This package does not require any additional configuration after installation
This package comes with two traits HasResourcePermissions
and HasRelationManagerPermissions
, which can be used in Filament's Resources and RelationManagers respectively.
To use simply use the trait in your Resource/RelationManger and define your permissions.
//UserResource.php
use Z3d0X\FilamentSimplePermissions\Concerns\HasResourcePermissions;
class UserResource extends Resource
{
use HasResourcePermissions;
protected static array $permissions = [
'viewAny' => 'access-users',
'view' => 'access-users',
'create' => 'create-users',
'update' => 'update-users',
'delete' => ['update-users', 'delete-stuff'], //use an array if multiple permissions are needed
'deleteAny' => false, //also supports boolean, to allow/disallow for all users
];
}
//PostsRelationManager.php
use Z3d0X\FilamentSimplePermissions\Concerns\HasRelationManagerPermissions;
class PostsRelationManager extends HasManyRelationManager
{
use HasRelationManagerPermissions;
protected static array $permissions = [
'create' => 'create-posts',
'update' => 'update-posts',
'delete' => ['update-posts', 'delete-stuff'], //use an array if multiple permissions are needed
'deleteAny' => false, //also supports boolean, to allow/disallow for all users
//Supports relation manager specific actions.
'viewForRecord' => 'access-posts',
'associate' => 'update-posts',
'dissociate' => 'update-posts',
'dissociateAny' => false,
];
}
For advanced usage, it is possible to define a static getPermissions()
method, instead of $permissions
property
//PostsRelationManager.php
use Z3d0X\FilamentSimplePermissions\Concerns\HasRelationManagerPermissions;
use Illuminate\Database\Eloquent\Model;
class PostsRelationManager extends HasManyRelationManager
{
use HasRelationManagerPermissions;
protected static function getPermissions(): array
{
return [
'viewForRecord' => fn (Model $ownerRecord) => $ownerRecord->user_id === auth()->id(),
'update' => function (Model $record) {
return $record->user_id === auth()->id();
},
];
}
}
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.