Filament::navigation and permissions

I am creating my navigation inside a ServiceProvider with Filament::navigation callback, by adding custom groups. - How can I prevent showing NavigationItems based on the current user? - I am using resource getNavigationItems() methods as well. Why is this not taking the policies into account? Example:
Filament::navigation(function (NavigationBuilder $builder): NavigationBuilder {
return $builder
->items([
NavigationItem::make('Dashboard')
->icon('heroicon-o-home')
->isActiveWhen(fn (): bool => request()->routeIs('filament.pages.dashboard'))
->url(route('filament.pages.dashboard')),
])
->groups([
NavigationGroup::make()->label('Content')->collapsed()->items([
...ArticleResource::getNavigationItems(),
])
]);
});
Filament::navigation(function (NavigationBuilder $builder): NavigationBuilder {
return $builder
->items([
NavigationItem::make('Dashboard')
->icon('heroicon-o-home')
->isActiveWhen(fn (): bool => request()->routeIs('filament.pages.dashboard'))
->url(route('filament.pages.dashboard')),
])
->groups([
NavigationGroup::make()->label('Content')->collapsed()->items([
...ArticleResource::getNavigationItems(),
])
]);
});
7 Replies
Dennis Koch
Dennis Koch2y ago
Only add them to the array if the user has permissions?
bernhard
bernhardOP2y ago
ok, so there is no build in logic? Because when just using the resources, the permissions are respected and users only see resources they are allowed to see
Dennis Koch
Dennis Koch2y ago
No, I don't think there is some Closure magic for this part.
bernhard
bernhardOP2y ago
and there is even no ->visible method 😦
Dennis Koch
Dennis Koch2y ago
Yeah, it's not that sexy, but just use if conditions and an array 😅
bernhard
bernhardOP2y ago
ah ok, its because this check is in another method:
public static function registerNavigationItems(): void
{
if (! static::shouldRegisterNavigation()) {
return;
}

if (! static::canViewAny()) {
return;
}

Filament::registerNavigationItems(static::getNavigationItems());
}
public static function registerNavigationItems(): void
{
if (! static::shouldRegisterNavigation()) {
return;
}

if (! static::canViewAny()) {
return;
}

Filament::registerNavigationItems(static::getNavigationItems());
}
Dennis Koch
Dennis Koch2y ago
Please open a separate thread and don’t just post into random questions
Want results from more Discord servers?
Add your server