<?php
namespace App\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class ContactVoter extends Voter
{
const LIST = "CONTACT_LIST";
const CREATE = "CONTACT_CREATE";
const UPDATE = "CONTACT_UPDATE";
const DETAIL = "CONTACT_DETAIL";
protected function supports($attribute, $subject): bool
{
return in_array($attribute, [
self::LIST,
self::CREATE,
self::UPDATE,
self::DETAIL,
]);
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
$loggedUser = $token->getUser();
if (!$loggedUser instanceof UserInterface) {
return false;
}
return in_array($attribute, $loggedUser->getRoles());
}
}