i created a custom voter it's name CustomVoter. I want to check user role in html.twig and if it has role i want to do something. My logged user has 'CAN_REMOVE' role that indicated in CustomVoter. Unfortunately it is not working or cannot see voter in html.twig. What is the problem?
{% if (is_granted(constant('App\\Security\\Voter\\CustomVoter::CAN_REMOVE'))) %}
// do something
{% endif %}
<?php
namespace App\Security\Voter;
use App\Entity\User;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
class CustomVoter extends Voter
{
const CAN_REMOVE = 'CAN_REMOVE';
/**
* @param string $attribute
* @param mixed $subject
*
* @return bool
*/
protected function supports($attribute, $subject): bool
{
if (!in_array($attribute, [self::CAN_REMOVE])) {
return false;
}
if (!$subject instanceof User) {
return false;
}
return true;
}
/**
* @param string $attribute
* @param User $subject
* @param TokenInterface $token
*
* @return bool
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof UserInterface) {
return false;
}
switch ($attribute) {
case self::CAN_REMOVE:
return !empty(array_intersect([UserVoter::ROLE_SUPER_ADMIN, self::CAN_REMOVE], $user->getRoles()));
break;
}
return false;
}
}