<?php
namespace Nen\Bundle\QuestionnaireBundle\Security\Voter;
use App\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
class UserRespondentVoter extends Voter
{
protected function supports(string $attribute, $subject): bool
{
return $subject instanceof User && $subject->hasRespondent();
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
/** @var User $user */
$user = $subject;
if ($attribute !== 'remove') {
return VoterInterface::ACCESS_ABSTAIN;
}
return $user->getRespondent()->getQuestionnaires()->count() === 0;
}
}