<?php
namespace App\Security\Voter;
use App\Data\RoleConstant;
use App\Security\Voter\CRM\CRMAccessVoter;
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;
/**
* @extends Voter<string, mixed>
*/
class SwitchToCustomerVoter extends Voter
{
private Security $security;
public function __construct(
Security $security
) {
$this->security = $security;
}
/**
* @param string $attribute
*/
protected function supports($attribute, $subject): bool
{
return 'CAN_SWITCH_USER' === $attribute && $subject instanceof UserInterface;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
// if the user is anonymous or if the subject is not a user, do not grant access
if (!$user instanceof UserInterface || !$subject instanceof UserInterface) {
return false;
}
// you can still check for ROLE_ALLOWED_TO_SWITCH
if ($this->security->isGranted(CRMAccessVoter::CRM_ACCESS, RoleConstant::ROLE_SUPPORT)) {
return true;
}
return false;
}
}