F
Filament2y ago
Abi

Show infolist conditionally based on user role

On the Admin Panel if a particular resource has an infolist instead of the View page, is there a way enable access to the infolist only for a specific user role? Passing an empty array for an infolist doesn't help as the table rows of the resource is still clickable. Any way to disable row clicks based on user role as an alternative?
Solution:
Personally instead of typing out the url, do this:
->recordUrl(fn($record) => auth()->user()->hasRole('CUSTOMER') ? null : self::getUrl('view', ['record' => $record]))
->recordUrl(fn($record) => auth()->user()->hasRole('CUSTOMER') ? null : self::getUrl('view', ['record' => $record]))
...
Jump to solution
13 Replies
ConnorHowell
ConnorHowell2y ago
You can use the recordAction() method on the table to override what action it runs when clicked (it accepts a callback so you can add in your logic there)
Abi
AbiOP2y ago
what would I do to disable the action for a certain user role?
toeknee
toeknee2y ago
->disable(fn() => auth()->user()->hasRole('My Role'))
->disable(fn() => auth()->user()->hasRole('My Role'))
Abi
AbiOP2y ago
so I use that for the recordAction ?
ConnorHowell
ConnorHowell2y ago
So let's say the role is "admin"
->recordAction(fn() => auth()->user()->hasRole('admin') ? 'view' : null)
->recordAction(fn() => auth()->user()->hasRole('admin') ? 'view' : null)
So when a user with the admin role clicks on the row, it'll open the infolist. If they don't it does nothing
Abi
AbiOP2y ago
tried that, that din't help, it still allows to click and goes to the infolist Still not sure how to do this to give it a try
ConnorHowell
ConnorHowell2y ago
Oh wait I was being dumb, it's a different page isn't it. Not a modal?
Abi
AbiOP2y ago
correct should I use recordUrl? ya, that worked
ConnorHowell
ConnorHowell2y ago
Yeah, instead of passing the string 'view' you'll want to inject $record and use the getUrl method Ah cool Guess the string does work
Abi
AbiOP2y ago
so, I had to do this
->recordUrl(fn($record) => auth()->user()->hasRole('CUSTOMER') ? null : "transactions/{$record->id}/view");
->recordUrl(fn($record) => auth()->user()->hasRole('CUSTOMER') ? null : "transactions/{$record->id}/view");
Solution
ConnorHowell
ConnorHowell2y ago
Personally instead of typing out the url, do this:
->recordUrl(fn($record) => auth()->user()->hasRole('CUSTOMER') ? null : self::getUrl('view', ['record' => $record]))
->recordUrl(fn($record) => auth()->user()->hasRole('CUSTOMER') ? null : self::getUrl('view', ['record' => $record]))
ConnorHowell
ConnorHowell2y ago
Generates the URL properly as if you were using the route() helper
Abi
AbiOP2y ago
ok, let me try that That works. thanks

Did you find this page helpful?