menu item depending on user role or permission

How can i set a menu item only, if the user has a specific role? I tried https://filamentphp.com/tricks/conditions-on-user-menu-items-based-on-roles But cant get it running. I installed in config/filament.php
'auth' => [
Authenticate::class,
UserMenuItemMiddleware::class,
'auth' => [
Authenticate::class,
UserMenuItemMiddleware::class,
` I created
php artisan make:middleware UserMenuItemMiddleware
php artisan make:middleware UserMenuItemMiddleware
public function handle(Request $request, Closure $next): Response
{
if (Auth::user()->hasRole('Root')) {

Filament::serving(function () {

Filament::registerUserMenuItems([
'GitLog' => UserMenuItem::make()
->label('Git Log')
->url(route('filament.pages.settings'))
->icon('heroicon-s-cog'),

]);
});
}
public function handle(Request $request, Closure $next): Response
{
if (Auth::user()->hasRole('Root')) {

Filament::serving(function () {

Filament::registerUserMenuItems([
'GitLog' => UserMenuItem::make()
->label('Git Log')
->url(route('filament.pages.settings'))
->icon('heroicon-s-cog'),

]);
});
}
I have a Page in app/Filament/Pages/GitLog.php with
class GitLog extends Page
{
protected static ?string $title = 'Git Log';
protected static ?string $navigationLabel = 'Git Log';
protected static ?string $navigationIcon = 'heroicon-o-document-text';
//protected static ?string $navigationGroup = 'Settings';
//protected static ?string $navigationIcon = 'heroicon-o-beaker';
protected static string $view = 'filament.pages.git-log';
protected static ?int $navigationSort = 4;

public $content;

public function mount(): void
{
//abort_unless(auth()->user()->hasRole(['Root', 'Admin']), 403);
abort_unless(auth()->user()->hasRole(['Root']), 403);
$this->content = file_get_contents(base_path('git-log.txt'));

}

}
class GitLog extends Page
{
protected static ?string $title = 'Git Log';
protected static ?string $navigationLabel = 'Git Log';
protected static ?string $navigationIcon = 'heroicon-o-document-text';
//protected static ?string $navigationGroup = 'Settings';
//protected static ?string $navigationIcon = 'heroicon-o-beaker';
protected static string $view = 'filament.pages.git-log';
protected static ?int $navigationSort = 4;

public $content;

public function mount(): void
{
//abort_unless(auth()->user()->hasRole(['Root', 'Admin']), 403);
abort_unless(auth()->user()->hasRole(['Root']), 403);
$this->content = file_get_contents(base_path('git-log.txt'));

}

}
What to i have to put in app/Http/Middleware/UserMenuItemMiddleware.php please? I want to show the page GitLog.php only when the user has role "Root".
Filament
Conditions on user menu items based on roles by sheldon - Tricks - ...
Filament is a collection of tools for rapidly building beautiful TALL stack apps, designed for humans.
8 Replies
Patrick Boivin
Patrick Boivin14mo ago
Sorry, I'm not sure I understand the need for a middleware... I think you could just do this in AppServiceProvider boot():
Filament::serving(function () {
if (Auth::user()->hasRole('Root')) {
Filament::registerUserMenuItems([
// ...
]);
}
});
Filament::serving(function () {
if (Auth::user()->hasRole('Root')) {
Filament::registerUserMenuItems([
// ...
]);
}
});
Falk Maria Zeitsprung
Thanks @pboivin I changed it. What do i have to place please inside Filament::registerUserMenuItems My Page i want to show only for user role "xx" is app/Filament/Pages/GitLog.php Or better said the MenuItem i want to show.
Patrick Boivin
Patrick Boivin14mo ago
Probably something like ->url(GitLog::getUrl())
Falk Maria Zeitsprung
if (Auth::user()->hasRole('Root')) { returns error: Call to a member function hasRole() on null. the workaround trick is done because of this it seems: https://filamentphp.com/tricks/conditions-on-user-menu-items-based-on-roles
Filament
Conditions on user menu items based on roles by sheldon - Tricks - ...
Filament is a collection of tools for rapidly building beautiful TALL stack apps, designed for humans.
Patrick Boivin
Patrick Boivin14mo ago
Try Auth::user()?->hasRole('Root')
Falk Maria Zeitsprung
now there is no error 🙂 but my problem is that the menu item i try to load does not appear in the dashboard menu. Nore if the user has roel Root or Admin. I have in AppServiceProvider:
Filament::serving(function () {
// Using Vite
Filament::registerViteTheme('resources/css/filament.css');

Auth::user()?->hasRole('Admin') ? Filament::registerUserMenuItems([
'GitLog' => UserMenuItem::make()
->label('test')
->url(route('filament.pages.git-log'))
]) : null;

});
Filament::serving(function () {
// Using Vite
Filament::registerViteTheme('resources/css/filament.css');

Auth::user()?->hasRole('Admin') ? Filament::registerUserMenuItems([
'GitLog' => UserMenuItem::make()
->label('test')
->url(route('filament.pages.git-log'))
]) : null;

});
and in app/Filament/Pages/GitLog.php
<?php

namespace App\Filament\Pages;

use Filament\Facades\Filament;
use Filament\Pages\Page;

class GitLog extends Page
{
protected static bool $shouldRegisterNavigation = false;
protected static string $view = 'filament.pages.git-log';

public string $content;

public function mount(): void
{
//abort_unless(auth()->user()->hasRole(['Root', 'Admin']), 403);
abort_unless(auth()->user()->hasRole(['Root']), 403);
$this->content = file_get_contents(base_path('git-log.txt'));

}

}
<?php

namespace App\Filament\Pages;

use Filament\Facades\Filament;
use Filament\Pages\Page;

class GitLog extends Page
{
protected static bool $shouldRegisterNavigation = false;
protected static string $view = 'filament.pages.git-log';

public string $content;

public function mount(): void
{
//abort_unless(auth()->user()->hasRole(['Root', 'Admin']), 403);
abort_unless(auth()->user()->hasRole(['Root']), 403);
$this->content = file_get_contents(base_path('git-log.txt'));

}

}
but the menu item will not show up. for no user role. What is my mistake please? I found it out! I thought UserMenuItems is in the Dashboard menu 🙈 Now is see its the "user"menu. UFFF I want to register in the Dashboard menu on the left side. Is that registerNavigationItems?
Dan Harrin
Dan Harrin14mo ago
yes
Falk Maria Zeitsprung
Thanks very much for all your great help. @Dan Harrin and @pboivin now it is working perfect! Filament is really great! Regards from Madrid.