Restricting Access to a Custom Filament Page Based on User Role
Hello everyone,
I'm currently working on a Filament project and have created a custom page named ApproveMaterials. I need to restrict access to this page so that only users with the 'admin' role can view it. However, I'm facing challenges in implementing this restriction.
Could anyone offer insights or guidance on the best way to restrict access to a custom Filament page based on user roles (and the make invisible the navigationLabel in the sidebar) ? Any tips or examples would be greatly appreciated!
Thank you in advance for your help.
6 Replies
This is a really easy implementation usually, eaither by using Policies.... as recommended. But if it is just the single page, put:
or whatever role checker function you have on your user instance.
I've tried but it isn't working, i can still have access while i'm a simple 'user' and not an 'admin'.
Do you have a role in your user ?
Yes I have a ‘user’ role
Maybe you can try filament shield plugin
Do you mind sharing your code?
Do you mean you can actually use the page? Or just that it still shows in the nav menu?
I've used the following to control access to some pages:
So I guess you could then also add:
(or maybe canView() can't be static in your page? depends where you're using it)