Restrict Impersonation permissions on the User model
masterYou can control who can impersonate and who can be impersonated by adding specific methods to your User model:
canImpersonate(): Determines if the current authenticated user has permission to start an impersonation session.canBeImpersonated(): Determines if a specific target user is allowed to be impersonated.
class User {
public function canImpersonate()
{
return $this->is_admin;
}
public function canBeImpersonated()
{
return !str_ends_with($this->email, '@mycorp.com');
}
}