All jobs

Add Roles and Permissions to a Filament Admin Panel

Yes. An agent adds roles via spatie/laravel-permission and a policy per resource so Filament hides what a role may not do, and opens a pull request with Pest tests.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent reads the panel providers, the user model and the test bootstrap before deciding where authorization should live. In an admin like filamentphp/demo, composer.json requires filament/filament ^5.0, resolved to v5.10.1 in composer.lock, on laravel/framework ^12.0, with Pest 3, Larastan and Pint in require-dev. app/Providers/Filament/AdminPanelProvider.php is the default panel: it discovers resources from app/Filament/Resources, groups navigation into Shop, HR, Projects and Blog, and uses a Login page under app/Filament/Pages/Auth/ that prefills the demo account. AppPanelProvider.php is a second panel at /app with Team as its tenant. app/Models/User.php implements FilamentUser and HasTenants, and both canAccessPanel and canAccessTenant return true for everyone; there is no app/Policies directory, so every signed-in user can do everything in both panels.

The change has four parts. First, composer require spatie/laravel-permission, its published migration, and the HasRoles trait on User. Second, roles seeded in database/seeders/DatabaseSeeder.php next to the admin user it already creates, one per navigation group plus admin: a shop manager sees Shop, an HR manager sees HR and Projects, an editor sees Blog, and admin sees everything, with the demo user given admin so the prefilled login keeps working. Third, a policy class per model under app/Policies/, for Shop\Order, Shop\Product, Shop\Customer, Shop\Brand, Shop\ProductCategory, the Blog and HR models and Team, with viewAny, view, create, update, delete, deleteAny, restore and forceDelete checking permissions named after the model. Filament resolves these through Laravel's Gate, so OrderResource and the others hide their create button, edit action and DeleteBulkAction for a role without the permission, and ListOrders disappears from navigation when viewAny is denied. Fourth, canAccessPanel in User returns whether the user has any role for the admin panel, and keeps the verified-email check for the app panel.

The custom actions on orders need attention beyond the policy. OrdersTable.php has process, ship, deliver and cancel actions in an ActionGroup beside EditAction and DeleteAction; the agent gives each a ->authorize() call against an update permission on the order so a viewer sees the rows but not the state buttons. Nothing else under app/Filament/Resources/ changes, because the policies carry the rules.

Then the tests. tests/Pest.php binds every test under tests/Filament to RefreshDatabase and actingAs(User::factory()->create()), which after this change would be a user with no role and would fail all 60 test files. The agent adds an admin state to UserFactory that assigns the role after creation and uses it in Pest.php, so the existing suite describes an admin as before. It adds new tests beside tests/Filament/Resources/Shop/Orders/Pages/: a user with the shop manager role can render ListOrders and CreateOrder, an editor gets a forbidden response from ListOrders and cannot see Shop in navigation, and a user with no role cannot reach the panel. It runs composer test:pest, which is pest --parallel, composer test:phpstan and pint --test, the checks .github/workflows/tests.yml runs on PHP 8.4 with SQLite, and opens a pull request.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Add roles and permissions to this Filament admin panel. Read composer.json, the panel providers under app/Providers/Filament, app/Models/User.php, database/seeders/DatabaseSeeder.php, tests/Pest.php and .github/workflows/tests.yml first. Install spatie/laravel-permission, publish its migration and add HasRoles to User. Seed an admin role and one role per navigation group, giving the seeded demo user admin so the prefilled login still works. Write a policy per model under app/Policies with viewAny, view, create, update, delete, deleteAny, restore and forceDelete checking permissions named after the model, so Filament hides actions and navigation a role may not use; authorize the custom order actions against the order update permission. Make canAccessPanel require a role for the admin panel. Add an admin state to UserFactory and use it in Pest.php so the existing suite still runs as an admin, then add Pest tests for a shop manager who can open the orders pages, an editor who is forbidden from them, and a user with no role who cannot access the panel. Run composer test:pest, composer test:phpstan and pint --test. Open a pull request with the role and permission matrix and the test output. Do not change the resources' forms or tables beyond authorization.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

What comes back is a pull request on its own branch. composer.json and composer.lock add the permission package; a new migration and config/permission.php are published; app/Models/User.php gains HasRoles and a real canAccessPanel; app/Policies/ appears with one class per model; OrdersTable.php changes only where the custom actions gain authorize; DatabaseSeeder.php and UserFactory.php learn about roles; and tests/Pest.php plus the new test files cover the roles. The description includes the matrix of roles against permissions.

The repository's tests workflow runs on the branch on PHP 8.4 with SQLite: composer install, npm run build, php artisan key:generate and composer test:pest. The session transcript at GET /o/{org}/sessions/{id}/messages keeps the pest --parallel, phpstan analyse and pint --test runs with their output, so a reviewer can see that the 60 existing test files still pass as an admin and the new ones pass per role.

Before merging, a reviewer checks that the permission names match what the policies check, that the demo seeder still signs in as an admin, and that the app panel's tenancy rules were not loosened by the canAccessPanel change. Wanting a different role split is one follow-up message to the same session, on the same branch.

Run it on every terminal hand-off

This is a change you start and come back to. One POST /o/{org}/sessions with this body and your repository in resources starts the run in its own sandbox with its own SQLite database, Composer cache and built assets; GET /o/{org}/sessions/{id}/events streams it while you watch, and it keeps going when you close the laptop. When the turn ends the sandbox parks with vendor/ and the database in place, so a follow-up through POST /o/{org}/sessions/{id}/messages picks up without reinstalling.

Give the key a webhook_url and Rush POSTs run.completed to your server when the turn finishes, with the result and the session_url. Send a request_id with the request so a retried call returns the run already started.

The use case this job follows, with its automation and trigger: Hand off a long task.

Why run it on Rush?

Authorization is the change nobody wants to do in a hurry on a laptop: it touches every resource, it has to be seeded, and one missed policy means a role that can delete orders. In a Rush sandbox the agent runs composer require, the migration and pest --parallel against its own SQLite database as many times as it needs, with the full suite as the safety net, while your working tree and your local database stay as they were.

What arrives is checkable. The pull request has the role matrix in its description, policies a reviewer can read one class at a time, and tests per role that run in the repository's own workflow. The transcript keeps every pest, phpstan and pint run, so the claim that the existing suite still passes is a log, not a sentence.

Frequently asked questions

Why spatie/laravel-permission rather than Filament's own gates?

Filament authorizes through Laravel policies either way; what the package adds is a place to store roles and permissions in the database and to assign them per user, which the demo has no table for. The policies the agent writes check named permissions, so swapping the storage later means changing the policies, not the resources.

Will existing tests break?

They would, because tests/Pest.php signs every Filament test in as a freshly created user with no role. The agent adds an admin state to UserFactory and uses it there, so the 60 existing test files run as an admin exactly as before. The new tests create users with other roles explicitly.

What happens to the second panel with teams?

AppPanelProvider.php keeps its tenant setup with Team. canAccessPanel receives the panel, so the agent requires a role only for the admin panel and leaves the app panel on its existing verified-email behaviour. If you want team-scoped roles there as well, say so in the prompt; the permission package supports a team foreign key.

Do the custom order actions respect the roles?

The process, ship, deliver and cancel actions in OrdersTable.php are custom Action instances, so a policy alone does not hide them. The agent adds ->authorize() to each against the order update permission, and a test asserts the actions are hidden for an editor and visible for a shop manager.

Does the seeded demo login still work?

Yes. DatabaseSeeder.php creates the demo user and the Login page prefills its credentials; the agent assigns that user the admin role in the same seeder step, so php artisan db:seed followed by the prefilled login lands on a panel that shows every group.

More Filament jobs

More jobs like this: Hand off a long task

Keep reading