Filament Panel - how hide / show items based on user privileges?

Hi in my app we have 2 classes of user - internal_researcher & external_researcher. For the latter, we want to show few options in the menu (which I have done by adding into the relevant resources code similar to this)
public static function shouldRegisterNavigation(): bool
{
// Replace this with your condition
if (auth()->user()->is_external_researcher)
return true;
else
return false;
}
public static function shouldRegisterNavigation(): bool
{
// Replace this with your condition
if (auth()->user()->is_external_researcher)
return true;
else
return false;
}
The dashboard has some widgets and another panel and I am trying to understand / locate documentation to describe how to conditionally show these based on user some condition. Would appreciate a pointer to relevant docs. Thanks in advance, j
6 Replies
toeknee
toeknee2w ago
You should use canAccess
public static function canAccess(): bool
{
return auth()->user()->is_external_researcher;
}
public static function canAccess(): bool
{
return auth()->user()->is_external_researcher;
}
Matthew
Matthew2w ago
You can add this method to your widget class:
public static function canView() : bool
{
return ...some logic
}
public static function canView() : bool
{
return ...some logic
}
toeknee
toeknee2w ago
I wouldn't use canView since this is for individual model records, canAccess is the first instance checking if they can access the resource.
Matthew
Matthew2w ago
Does that work on widgets?
Dennis Koch
Dennis Koch2w ago
It's better to use Policies or canAccess() for pages, because the pages will still be accessible. The second part was about Widgets not Pages.
jjo63
jjo63OP2d ago
I meant to say "Thanks all for your guidance" - I don't like to leave responses un-acknowledged!

Did you find this page helpful?