How to apply Laravel Policy to Filament Actions (Export/Import/Custom Actions)?
Hi everyone š
I'm using FilamentPHP and trying to integrate Laravel Policies into my actions, both default (like Export/Import) and custom ones (like a
PublishPost
action on a PostResource
).
---
š Use Case 1: Export / Import Authorization (resource-wide)
I want to:
* Control visibility of ExportAction
and ImportAction
using policy
* Prevent execution if user is unauthorized
In my PostPolicy
I have:
Then in my PostResource
, I attach:
Is this the correct way? Or is there a more idiomatic "Filament way" to handle this?5 Replies
š§© Use Case 2: Custom action with per-record policy
I also have a custom
PublishAction
where I want to only allow certain users (e.g., editors) to publish certain posts.
In PostPolicy
I added:
And in my PostResource
:
---
ā Questions
* Is using ->authorize()
and ->visible()
like this the recommended way in Filament?
* Can Filament auto-detect Laravel policy methods like export
, import
, or publish
?
* Is there a more elegant/built-in solution I'm missing?
Thanks in advance ā any guidance is much appreciated! šLooks good to me, we use canView and canViewAny for access etc and you want to refine them with certain control over the actions which is does well.
canViewAny() maybe for
export
, import
need authorized create
policy.š Use Case 3: Import Failure CSV Authorization via ImportPolicy
https://filamentphp.com/docs/4.x/actions/import#authorization
I read the docs on customizing access to the failure CSV file for import errors.
By default, only the user who started the import may access the failure CSV. To override this, define your ownI created: And registered it: š§ Question: Can I make the ImportPolicy apply per resource (e.g. different rules forImportPolicy
and register it inAuthServiceProvider
.
PostImport
, UserImport
)?
Right now it seems to be a single global Import
model ā is there a clean way to scope this behavior?Not as far as I am aware.