F
Filament10mo ago
vas

Restrict user access to different panels based on roles

i have 2 panels one is the default admin one and second one lets call it superadmin i would like to restrict access to admin only for the users that is_admin returns true for example and is_superadmin for superadmin
7 Replies
vas
vas10mo ago
i had tried
public function canAccessPanel(Panel $panel): bool
{
return $this->is_admin;
}
public function canAccessPanel(Panel $panel): bool
{
return $this->is_admin;
}
but this applies to all users
awcodes
awcodes10mo ago
So, grab the current panel in the method and base your condition on the panel. If super_admin return true. If panel === ‘blah’ return is_admin.
vas
vas10mo ago
if ($this->panel === 'nearmiss') {
return true;
} else {
return $this->is_admin;
}
if ($this->panel === 'nearmiss') {
return true;
} else {
return $this->is_admin;
}
think is working 😄 nah i still get forbbiden for both
vas
vas10mo ago
Lightshot
Screenshot
Captured with Lightshot
vas
vas10mo ago
do i refer to this panels by name
public function canAccessPanel(Panel $panel): bool
{

if (Filament::getCurrentPanel()->getId() === 'admin') {
return $this->is_admin == 1;
}

if (Filament::getCurrentPanel()->getId() === 'nearmiss') {
return true;
}

return false;

}
public function canAccessPanel(Panel $panel): bool
{

if (Filament::getCurrentPanel()->getId() === 'admin') {
return $this->is_admin == 1;
}

if (Filament::getCurrentPanel()->getId() === 'nearmiss') {
return true;
}

return false;

}
this works idk if there is a better way im open to suggestions
awcodes
awcodes10mo ago
This looks ok. But $panel is injected so it should be the current panel.
vas
vas10mo ago
got ya thanks
public function canAccessPanel(Panel $panel): bool
{

if ($panel->getId() === 'admin') {
return $this->is_admin == 1;
}

if ($panel->getId() === 'nearmiss') {
return true;
}

return false;

}
public function canAccessPanel(Panel $panel): bool
{

if ($panel->getId() === 'admin') {
return $this->is_admin == 1;
}

if ($panel->getId() === 'nearmiss') {
return true;
}

return false;

}