<?php
namespace App\Security\Voter;
use App\Model\DataObject\Customer;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
class ImpersonateVoter extends Voter
{
public function __construct(
private Security $security,
) {
}
protected function supports($attribute, $subject)
{
return $attribute === 'CAN_SWITCH_USER';
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
if ($this->security->isGranted('ROLE_REPRESENTATIVE')) {
return true;
}
/** @var Customer $customer */
$customer = $token->getUser();
$linkedCustomersIds = $customer->getLinkedPapyrusIds();
if( !$linkedCustomersIds ) {
return false;
}
$linkedCustomersIds = explode(';', $linkedCustomersIds);
$subjectPapyrusId = $subject->getPapyrusId();
return in_array($subjectPapyrusId, $linkedCustomersIds);
}
}