1. Go to this page and download the library: Download elpandape/filament-warden 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-warden example snippets
'grid' => ['class_names' => true],
// app/Policies/OrderPolicy.php
use App\Models\Order;
use App\Models\User;
use ElPandaPe\FilamentWarden\Policies\WardenPolicy;
final class OrderPolicy extends WardenPolicy
{
public function viewAny(User $user): bool
{
return $this->allows($user, 'viewAny', Order::class);
}
public function view(User $user, Order $order): bool
{
return $this->allows($user, 'view', $order);
}
public function update(User $user, Order $order): bool
{
return $this->allows($user, 'update', $order);
}
}
// app/Models/User.php
use ElPandaPe\FilamentWarden\Concerns\AccessesPanels;
use Filament\Models\Contracts\FilamentUser;
final class User extends Authenticatable implements FilamentUser
{
use AccessesPanels;
}
use ElPandaPe\FilamentWarden\Filament\Concerns\AuthorizesPageAccess;
use ElPandaPe\FilamentWarden\Filament\Concerns\AuthorizesWidgetView;
final class Reports extends Page
{
use AuthorizesPageAccess; // Generates: page:App\Filament\Pages\Reports
}
final class RevenueChart extends ChartWidget
{
use AuthorizesWidgetView; // Generates: widget:App\Filament\Widgets\RevenueChart
}
FilamentWardenPlugin::make()
->roles(false) // no role resource, no grid
->permissions(false); // no permission resource
use ElPandaPe\FilamentWarden\Policies\WardenPolicy;
final class OrderPolicy extends WardenPolicy
{
public function viewAny(User $user): bool
{
// For listings: pass the class
return $this->allows($user, 'viewAny', Order::class);
}
public function view(User $user, Order $order): bool
{
// For individual records: pass the instance
return $this->allows($user, 'view', $order);
}
}
use ElPandaPe\FilamentWarden\Concerns\AccessesPanels;
final class User extends Authenticatable implements FilamentUser
{
use AccessesPanels;
}
use AccessesPanels {
canAccessPanel as wardenCanAccessPanel;
}
public function canAccessPanel(Panel $panel): bool
{
return $this->isActive() && $this->wardenCanAccessPanel($panel);
}
use ElPandaPe\FilamentWarden\Filament\RelationManagers\RolesRelationManager;
public static function getRelations(): array
{
return [RolesRelationManager::class];
}
use ElPandaPe\FilamentWarden\Filament\Forms\RoleAssignment;
RoleAssignment::make('roles')->columnSpanFull(),
use ElPandaPe\FilamentWarden\Filament\RelationManagers\PermissionsRelationManager;
public static function getRelations(): array
{
return [RolesRelationManager::class, PermissionsRelationManager::class];
}
use ElPandaPe\FilamentWarden\Support\Access;
// Loose permission
Access::grantedToCurrentUser('export-reports');
// Over a class
Access::grantedToCurrentUser('viewAny', Invoice::class);
// Over a specific record
Access::granted($otherUser, 'view', $invoice);
use ElPandaPe\Warden\Facades\Warden;
it('lets an editor update their own posts and nobody else touch them', function () {
$fake = Warden::fake();
$fake->allow('update', Post::class)->for($editor);
expect(Filament\get_authorization_response('update', $post)->allowed())->toBeTrue();
$fake->assertChecked('update');
});
// config/filament-warden.php
'guard' => [
'pages' => true, // refuse to start on an unguarded page
'widgets' => true, // refuse to start on an unguarded widget
],
use ElPandaPe\Warden\Concerns\QueriesByPermission;
class Document extends Model
{
use QueriesByPermission;
}
use ElPandaPe\FilamentWarden\Catalog\Catalog;
use Filament\Facades\Filament;
$entries = Catalog::for(Filament::getPanel('admin'))->entries;
foreach ($entries as $entry) {
$entry->name; // 'viewAny', 'page:App\...'
$entry->entityType; // Morph alias or null
$entry->model; // Model class or null
$entry->scope; // Read | Write | Withdraw | Irreversible
$entry->origin; // Resource | Model | Page | Widget | Custom | Panel
$entry->key(); // 'viewAny|order'
}
'grid' => [
'explain' => true, // Inspector
'constraints' => true, // Show scope
'expiry' => true, // May the grid set an end date
'class_names' => false, // Draw the class under each entity
],
'catalog' => [
'models' => [], // models with a policy and no resource
'custom' => [], // loose permissions, as name => scope
'scopes' => [
'read' => ['viewAny', 'view'],
'write' => ['create', 'update'],
'withdraw' => ['delete', 'deleteAny', 'restore', 'restoreAny'],
'irreversible' => ['forceDelete', 'forceDeleteAny'],
],
],
'navigation' => [
'group' => null, // falls back to this package's own translated group
'roles' => [
'slug' => 'roles', // what the URL says
'icon' => null, // falls back to a shield
'sort' => null, // navigation order
],
'permissions' => [
'slug' => 'permissions',
'icon' => null, // falls back to a key
'sort' => null,
],
],
bash
# 1. Install the package
composer el
use ElPandaPe\FilamentWarden\FilamentWardenPlugin;
public function panel(Panel $panel): Panel
{
return $panel
// ...
->plugin(FilamentWardenPlugin::make());
}
# 3. Install warden (creates tables)
php artisan warden:install --migrate
# 4. Publish assets
php artisan filament:assets