<?php
namespace App\Security\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* Permet de gérer les ROLES qui peuvent switcher (impersonification)
*
* Class SwitchToCustomerVoter
* @package App\Security\Voter
*/
class SwitchToCustomerVoter extends Voter
{
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
protected function supports($attribute, $subject)
{
return in_array($attribute, ['CAN_SWITCH_USER'])
&& $subject instanceof UserInterface;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
if (!$user instanceof UserInterface || !$subject instanceof UserInterface) {
return false;
}
if ($this->security->isGranted('ROLE_ALLOWED_TO_SWITCH')) {
return true;
}
return false;
}
}