F
Filamentβ€’11mo ago
Sorin

Where is the Filament\Actions\Concerns\CanBeHidden::authorize() method in the documentation ?

Hello. I was looking through Filament's documentation, and I have found no mentions of the authorize() method. I want to use this method to allow only admins to click a certain button in my infolist view, and I'm not sure if the authorize() method only hides the button, or if a non-admin user calls the endpoint, they'll get a 403. Does anybody know where in the docs this method is explained ? Thank you πŸ˜„
8 Replies
Sorin
SorinOPβ€’11mo ago
Thank you for the response. I don't see the authorize() method on the page that you've provided. My code looks something like this:
Action::make('Accept')->authorize(fn (Request $request): bool => $request->status === RequestStatus::PENDING && auth()->user()->isAdmin()),
Action::make('Accept')->authorize(fn (Request $request): bool => $request->status === RequestStatus::PENDING && auth()->user()->isAdmin()),
, and Action is: Filament\Infolists\Components\Actions\Action
LeandroFerreira
LeandroFerreiraβ€’11mo ago
not sure if I can explain it, but I believe using authorize is an alternative for controlling action visibility based on policies, abilities... you can use as your example and ->visible() or ->hidden() will have the same result.
Sorin
SorinOPβ€’11mo ago
Hmm, oke. Do you know if they also throw a 403 if an unauthorised user tries to send a request to the action, or only hide the action button ?
LeandroFerreira
LeandroFerreiraβ€’11mo ago
only hide I think
Sorin
SorinOPβ€’11mo ago
It would make sense, based on the name of the methods. Thank you very much for the answers.
Luc Van Keer
Luc Van Keerβ€’6d ago
Excuses for the late answer. You can use the visible() or hidden() to conditionally show/hide the action button. In the ->action() method you can call a method on your livewire component where you can actually check the authorization. Something like: ->action(fn(array $data, Clubmember $record, EditClubmember $livewire) => $livewire->changeDepartment($record, $data)), public function changeDepartment(Clubmember $record, array $data): void { if (!filament()->auth()->user()->can('update_dept_clubmember', $record)) { return; } $record->departments = $data['departments']; $record->save(); }
Sorin
SorinOPβ€’5d ago
I see. Thank you for the response, no worries about the timing, knowledge is always welcome from my pov πŸ˜„

Did you find this page helpful?