F
Filament14mo 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
vasOP14mo 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
awcodes14mo 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
vasOP14mo 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
vasOP14mo ago
Lightshot
Screenshot
Captured with Lightshot
vas
vasOP14mo 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
awcodes14mo ago
This looks ok. But $panel is injected so it should be the current panel.
vas
vasOP14mo 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;

}
Want results from more Discord servers?
Add your server