diff --git a/config/packages/security.yaml b/config/packages/security.yaml index d56bb39..04d7aa8 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -22,7 +22,7 @@ security: custom_authenticators: - App\Security\BpnAuthenticator switch_user: - role: ROLE_ADMIN + role: CAN_IMPERSONATE logout: path: app_security_logout target: app_security_login diff --git a/src/Security/Voter/ImpersonationVoter.php b/src/Security/Voter/ImpersonationVoter.php new file mode 100644 index 0000000..691301b --- /dev/null +++ b/src/Security/Voter/ImpersonationVoter.php @@ -0,0 +1,45 @@ +getUser(); + $targetUser = $subject; + + // if the user is anonymous or if the subject is not a user, do not grant access + if (!$currentUser instanceof User || !$targetUser instanceof User) { + return false; + } + + // if the current user is trying to impersonate herself, do not grant access + if ($currentUser === $targetUser) { + return false; + } + + // if the current user is already impersonating, do not grant access + if ($this->security->isGranted('IS_IMPERSONATOR')) { + return false; + } + + // Admin is the only role allowed to impersonate + return $this->security->isGranted('ROLE_ADMIN'); + } +}