two different panel with roles

Hi any option to do this? 1. i have two panel, admin and client 2. how i can set only specific role to be able to access admin panel 3. the other roles can only see client. 4. admin user of course can see both client and admin panel how i can achieve this? i already got both panels and got shield plugin. thank you
Solution:
$this = User model instance
Jump to solution
8 Replies
rahat392
rahat3922mo ago
There is a method called authMiddleware that is called when someone is visiting the panel you can put your logic there...
$panel
->default()
->id('backoffice')
->path('backoffice')
->login()
->colors([
'primary' => Color::Amber,
])
->discoverResources(in: app_path('Filament/Resources'), for: 'App\\Filament\\Resources')
->discoverPages(in: app_path('Filament/Pages'), for: 'App\\Filament\\Pages')
->pages([
Pages\Dashboard::class,
])
->discoverWidgets(in: app_path('Filament/Widgets'), for: 'App\\Filament\\Widgets')
->widgets([
Widgets\AccountWidget::class,
Widgets\FilamentInfoWidget::class,
])
->middleware([
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
AuthenticateSession::class,
ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SubstituteBindings::class,
DisableBladeIconComponents::class,
DispatchServingFilamentEvent::class,
])
->authMiddleware([
Authenticate::class,
]);
$panel
->default()
->id('backoffice')
->path('backoffice')
->login()
->colors([
'primary' => Color::Amber,
])
->discoverResources(in: app_path('Filament/Resources'), for: 'App\\Filament\\Resources')
->discoverPages(in: app_path('Filament/Pages'), for: 'App\\Filament\\Pages')
->pages([
Pages\Dashboard::class,
])
->discoverWidgets(in: app_path('Filament/Widgets'), for: 'App\\Filament\\Widgets')
->widgets([
Widgets\AccountWidget::class,
Widgets\FilamentInfoWidget::class,
])
->middleware([
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
AuthenticateSession::class,
ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SubstituteBindings::class,
DisableBladeIconComponents::class,
DispatchServingFilamentEvent::class,
])
->authMiddleware([
Authenticate::class,
]);
Bruno Pereira
Bruno Pereira2mo ago
The user model has a canAccessPanel function
agaitan026
agaitan026OP2mo ago
public function canAccessPanel(Panel $panel): bool
{
if ($panel->getId() === 'admin') {
return str_ends_with($this->email, '@yourdomain.com') && $this->hasVerifiedEmail();
} else if ($panel->getId() === 'client') {
return str_ends_with($this->email, '@yourdomain.com') && $this->hasVerifiedEmail();
}

return false; // default case, in case $panel->getId() is neither 'admin' nor 'owner'
}
public function canAccessPanel(Panel $panel): bool
{
if ($panel->getId() === 'admin') {
return str_ends_with($this->email, '@yourdomain.com') && $this->hasVerifiedEmail();
} else if ($panel->getId() === 'client') {
return str_ends_with($this->email, '@yourdomain.com') && $this->hasVerifiedEmail();
}

return false; // default case, in case $panel->getId() is neither 'admin' nor 'owner'
}
means i can use something like this? but how i get to the correct panel by checking roles, i have role super_admin and client
Bruno Pereira
Bruno Pereira2mo ago
since you're in the user model, you can access the instance. like $this->where(...)
agaitan026
agaitan026OP2mo ago
and im using shield
Bruno Pereira
Bruno Pereira2mo ago
same thing $this->can('permission') or other method
Solution
Bruno Pereira
Bruno Pereira2mo ago
$this = User model instance
Want results from more Discord servers?
Add your server