Philsquare Permissions

Philsquare Permissions is my open source Laravel package for role-based permissions. Each policy maps roles to its abilities, and one command syncs that map to the database. It's built on Spatie's laravel-permission and works with Filament 5 panels. I've used it since 2023, first as code in the app/ directory and since 2025 as a package. It runs in the client apps I build.

app/Policies/PostPolicy.phpPHP
class PostPolicy extends BasePolicy
{
    public function rolePermissions(): array
    {
        return [
            'admin' => $this->permissions()->all(),
            'editor' => $this->permissions()->crud(),
            'viewer' => $this->permissions()->only(['viewAny', 'view']),
        ];
    }

    // … viewAny(), view(), create(), update(), delete() …
}

A policy lists its roles, each with the policy methods that role may call. all(), crud(), only() and except() build those lists.

The role map decides who may attempt an action. The policy method still holds the rule for the record, such as only updating a draft.

How it's built: BasePolicy::before() returns false unless one of the user's roles holds the permission, and null to let the method decide. Permission names are {model}:{ability} in kebab case, so PurchaseOrderPolicy::updateEta() is purchase-order:update-eta. One class, NameBuilder, builds the name for both the sync and the check. all(), only() and except() read the policy's public methods by reflection.

Package
philsquare/permissions
In use
Since 2023
Version
3.0, November 2026
Requires
PHP 8.4+, Laravel 12 or 13, Spatie laravel-permission 8.3+
Filament
Filament 5, optional

Filament

src/Filament/DenyUndefinedAbilities.phpPHP
protected const ABILITIES = [
    'viewAny' => null,
    'view' => null,
    'create' => null,
    'update' => null,
    'delete' => null,
    'restore' => 'delete',
    'forceDelete' => 'delete',
    'deleteAny' => 'delete',
    'restoreAny' => 'restore',
    'forceDeleteAny' => 'forceDelete',
    'replicate' => 'create',
    'reorder' => 'update',
];

// … __invoke(), the Gate::before callback …

protected function standInFor(BasePolicy $policy, string $ability): ?string
{
    do {
        $ability = self::ABILITIES[$ability] ?? null;
    } while ($ability !== null && ! method_exists($policy, $ability));

    return $ability;
}

Filament checks abilities that Laravel's policy stub doesn't define, such as deleteAny for bulk delete. When a policy has no method for an ability, Filament allows the action without calling the policy, so before() never runs. On its own, that lets a role with no delete access use the DeleteBulkAction in every resource Filament generates.

The package closes this for policies that extend BasePolicy. A missing ability falls back to a related permission: deleteAny to delete, replicate to create, reorder to update. viewAny, view, create, update and delete have no fallback and are denied until the policy defines them. Define a method on the policy and it becomes a permission of its own.

How it's built: DenyUndefinedAbilities is a Gate::before callback, registered only when Filament is installed and after every provider has booted, so an app's own super admin check runs first. standInFor() walks the table until it reaches an ability the policy defines. The tests run these checks against a real Filament resource.

The sync command

src/Commands/RefreshPermissions.phpPHP
try {
    // One transaction, so requests keep seeing the old grants until the
    // new ones are complete and a failure leaves the old ones in place.
    (new $roleClass)->getConnection()->transaction(
        fn () => $this->sync($grants, $roleClass, $permissionClass, $guard)
    );
} finally {
    // Also on failure: the Spatie cache may have been rebuilt from rows
    // that have since been rolled back.
    $registrar->forgetCachedPermissions();
}

php artisan permissions:refresh goes in the deploy script, after the migrations. It finds every policy that declares roles, in app/Policies at any depth and anywhere else one is registered with Gate::policy(). It creates the missing roles and permissions, then makes each role's permissions match the policies exactly.

How it's built: The sync is one transaction. An empty scan changes nothing, because syncing it would remove every grant. The command uses the app's default guard and the Role and Permission models from Spatie's configuration, and resolves each policy through the container, so a policy can have constructor dependencies.

Also in the package

Policy generator

make:policy --withPermissions generates a policy that extends BasePolicy, with a method for every ability Filament uses. Each one is then its own permission, granted explicitly.

How it's built: It extends Laravel's own make:policy command and swaps the stub.

Tests and CI

A Pest suite covers the checks, the sync, the generator and Filament resources. GitHub Actions runs it on PHP 8.4 and 8.5 with Laravel 12 and 13, on the lowest and the newest dependency versions.

How it's built: The workflow also runs weekly, so a Laravel, Filament or Spatie release that breaks the package fails a build even when the package hasn't changed. Pint and Larastan run in the same workflow.

Claude Code plugin

The repository is also a Claude Code plugin marketplace. The plugin has a skill with the package's conventions, an agent that adds rolePermissions() to a new policy, and commands for the generator and the sync.

I build role-based access like this into Laravel and Filament apps. Work with me