PHP code example of chiiya / filament-access-control

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

    

chiiya / filament-access-control example snippets


use Chiiya\FilamentAccessControl\FilamentAccessControlPlugin;

return $panel
    ->default()
    ->id('admin')
    ->path('admin')
    ->plugin(FilamentAccessControlPlugin::make())

class ProductPolicy
{
    public function viewAny(FilamentUser $user): bool
    {
        return $user->can('products.view');
    }
    
    // ...
}

use Chiiya\FilamentAccessControl\Traits\AuthorizesPageAccess;

class MyPage extends Page
{
    use AuthorizesPageAccess;
    
    public static string $permission = 'my-page.view';
    
    public function mount(): void
    {
        static::authorizePageAccess();
    }
}

ButtonAction::make('exports')
    ->visible(fn () => Filament::auth()->user()->can('exports.view'))

'features' => [
    \Chiiya\FilamentAccessControl\Enumerators\Feature::ACCOUNT_EXPIRY,
],

use Chiiya\FilamentAccessControl\Http\Middleware\EnsureAccountIsNotExpired;

...
->authMiddleware([
    Authenticate::class,
    EnsureAccountIsNotExpired::class,
]);

'features' => [
    \Chiiya\FilamentAccessControl\Enumerators\Feature::TWO_FACTOR,
],

'user_model' => CustomFilamentUser::class,

use Chiiya\FilamentAccessControl\Models\FilamentUser;
use Chiiya\FilamentAccessControl\Contracts\AccessControlUser;
use Filament\Models\Contracts\FilamentUser as FilamentUserInterface;
use Filament\Models\Contracts\HasName;
use Illuminate\Foundation\Auth\User as Authenticatable;

class CustomFilamentUser extends FilamentUser
{
    // ...
}

// Or alternatively
class CustomFilamentUser extends Authenticatable implements AccessControlUser, FilamentUserInterface, HasName
{
    // ...
}

    /*
    |--------------------------------------------------------------------------
    | Resources
    |--------------------------------------------------------------------------
    | Resources used for managing users, roles and permissions.
    */
    'resources' => [
        'user' => FilamentUserResource::class,
        'role' => RoleResource::class,
        'permission' => PermissionResource::class,
    ]
bash
php artisan vendor:publish --tag="filament-access-control-migrations"
php artisan vendor:publish --tag="filament-access-control-config"
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"
php artisan migrate
bash
php artisan vendor:publish --tag="filament-access-control-translations"
bash
php artisan vendor:publish --tag="filament-access-control-views"

    public static function insertBeforeFormSchema(): array
    {
        return [];
    }

    public static function insertAfterFormSchema(): array
    {
        return [];
    }

    public static function insertBeforeTableSchema(): array
    {
        return [];
    }

    public static function insertAfterTableSchema(): array
    {
        return [];
    }