PHP code example of z3d0x / filament-simple-permissions
1. Go to this page and download the library: Download z3d0x/filament-simple-permissions library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
z3d0x / filament-simple-permissions example snippets
//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,
];
}
//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();
},
];
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.