PHP code example of elpandape / filament-permission

1. Go to this page and download the library: Download elpandape/filament-permission 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/ */

    

elpandape / filament-permission example snippets


use ElPandaPe\FilamentPermission\Filament\FilamentPermissionPlugin;

public function panel(Panel $panel): Panel
{
    return $panel
        // …
        ->plugin(FilamentPermissionPlugin::make());
}

use ElPandaPe\FilamentPermission\Filament\AuthorizesPage;
use ElPandaPe\FilamentPermission\Filament\AuthorizesWidget;

final class Invoices extends Page   { use AuthorizesPage; }
final class Revenue  extends Widget { use AuthorizesWidget; }

// config/filament-permissions.php
'discovery' => [
    'exclude' => [
        Filament\Pages\Dashboard::class,
        Filament\Widgets\AccountWidget::class,
    ],
],

// A Resource whose slug is 'security/users'
App\Filament\Resources\Security\Users\UserResource::class

// A standalone page and a widget
App\Filament\Pages\Reports\Overview::class          // class basename: Overview
App\Filament\Widgets\StatsOverview::class           // class basename: StatsOverview

// config/filament-permissions.php
'custom' => [
    'billing' => ['view', 'export'],
],

// config/filament-permissions.php
'custom' => [
    'security/users' => ['impersonate'],   // merges into the entity that already exists
    'reports'        => ['view', 'export'], // opens its own card, "Custom" tab
],

use ElPandaPe\FilamentPermission\Catalog\Ability;
use ElPandaPe\FilamentPermission\Catalog\Entity;
use ElPandaPe\FilamentPermission\Contracts\DefinesPermissions;

final class Invoices extends Page implements DefinesPermissions
{
    public static function permissionEntity(): Entity
    {
        return new Entity('billing/invoices', 'Invoices');
    }

    /** @return array<int, Ability> */
    public static function permissionAbilities(): array
    {
        return [new Ability('view'), new Ability('export')];
    }
}

final class UserPolicy extends ResourcePolicy
{
    protected function entity(): Entity
    {
        return new Entity('security/users', 'User');
    }
}

use ElPandaPe\FilamentPermission\Filament\AuthorizesPage;
use ElPandaPe\FilamentPermission\Filament\AuthorizesWidget;

final class Invoices extends Page   { use AuthorizesPage; }
final class Revenue  extends Widget { use AuthorizesWidget; }

'discovery' => [
    'exclude' => [
        Filament\Pages\Dashboard::class,
        Filament\Widgets\AccountWidget::class,
    ],
],

'custom' => ['security/roles' => ['delegate']],

'grants' => [
    'unrestricted' => 'security/roles.delegate',
    'reserved' => ['billing/*.forceDelete'],
],

  'custom' => [
      'security/roles' => ['delegate'],
  ],
  

'super_admin' => [
    'enabled' => true,
    'role' => 'super-admin',
],

use ElPandaPe\FilamentPermission\Filament\Forms\RolesField;

Section::make('Roles')->schema([
    RolesField::make(),
]);

use ElPandaPe\FilamentPermission\Filament\RelationManagers\RolesRelationManager;

public static function getRelations(): array
{
    return [RolesRelationManager::class];
}

// config/filament-permissions.php
'relation' => [
    'view_icon' => Heroicon::OutlinedEye,
    'permissions_icon' => Heroicon::OutlinedKey,
],

// config/filament-permissions.php
'key' => fn (string $entity, string $ability): string => $ability.'_'.$entity,
bash
php artisan vendor:publish --tag=filament-permissions-config
bash
php artisan vendor:publish --tag=filament-permissions-translations
bash
php artisan vendor:publish --tag=filament-permissions-views
bash
php artisan filament:assets
bash
php artisan make:permission-policy "App\Filament\Resources\Security\Users\UserResource"
bash
php artisan filament-permission:sync
bash
php artisan filament-permission:sync                 # creates what is missing, reports the orphans
php artisan filament-permission:sync --prune         # also deletes them
php artisan filament-permission:sync --check         # writes nothing; exits 1 if they have diverged
php artisan filament-permission:sync --panel=admin   # a different panel; the default panel otherwise
bash
php artisan vendor:publish --tag=filament-permissions-translations