F
Filament13mo 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
ConnorHowell13mo 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
AbiOP13mo ago
what would I do to disable the action for a certain user role?
toeknee
toeknee13mo ago
->disable(fn() => auth()->user()->hasRole('My Role'))
->disable(fn() => auth()->user()->hasRole('My Role'))
Abi
AbiOP13mo ago
so I use that for the recordAction ?
ConnorHowell
ConnorHowell13mo 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
AbiOP13mo 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
ConnorHowell13mo ago
Oh wait I was being dumb, it's a different page isn't it. Not a modal?
Abi
AbiOP13mo ago
correct should I use recordUrl? ya, that worked
ConnorHowell
ConnorHowell13mo 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
AbiOP13mo 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
ConnorHowell13mo 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
ConnorHowell13mo ago
Generates the URL properly as if you were using the route() helper
Abi
AbiOP13mo ago
ok, let me try that That works. thanks
Want results from more Discord servers?
Add your server