[Request]: allow policy-based context menu actions with event type detection
What feature would you like to add?
What we need to achieve is that when clicking on a calendar event, the context menu should display actions based on the user's policies/permissions for that particular model type. For example, a user with "view_activity" but not "edit_activity" permissions should only see the view action when clicking on an Activity event(model).
I'm not sure if there's a way to access the event model type before the record is loaded - if there is, I'd appreciate guidance on how to implement this. This would allow proper integration with Laravel's policy system for granular permission control.
Currently calendar context menu actions (view/edit/delete) are always shown regardless of user permissions. This leads to two issues:
- Actions are visible even when users lack permissions
- The
getEventRecord()method returns null on initial load, making it impossible to check model type for permission validation
We need a mechanism to:
- Identify event model type before showing actions
- Hide/show actions based on model policies
Example of current limitation:
public function getEventClickContextMenuActions(): array
{
$event = $this->getEventRecord(); // Returns null initially
// Cannot determine if event is A/B
// Cannot check appropriate policies
}
### Notes
_No response_
Hi @Gonzalo2683, this is currently not possible due to the way the context menu is created.
The context menu is created during the initial rendering phase of the page and then conditionally positioned and made visible when an event is clicked, with every single action configured for the context menu.
And only upon clicking on the action itself it is mounted with the event that was currently opened. So there's noway beforehand to know which actions to render.
If you have a suggestion or idea how to improve this, I'll be more than happy to merge the PR! :)
Until there is a better way to solve this I have implemented a check before the form is filled and just close the model... it's not pretty but works:
editAction:
->beforeFormFilled(static function ($action, CalendarEvent $record) {
if ($record->user_id !== auth()->id()) {
Notification::make()
->danger()
->title(__('You do not have permission to edit this event'))
->send();
$action->cancel();
return false;
}
return true;
}),
Wait a minute, I'm pretty sure the actions do check policies though.
The only thing not possible is to hide the actions completely or disable them if the user is not allowed to call the action.
If you didn't override the onEventClick method, it does check the policy before mounting the action.
Would love event based context actions also. +1 for this
That should be possible in the latest version already
EventClickInfo should be passed also as evaluation parameter to MOST regular filament methods on the action, so you can conditionally hide an action based on the info from the EventClickInfo context
EventClickInfo should be passed also as evaluation parameter to MOST regular filament methods on the action, so you can conditionally hide an action based on the info from the EventClickInfo context
Hi, yes. I can access record from action and set it visible. If all actions are hidden, small context menu container is displayed.
Yeah that's the limitation of the current implementation