<?php
declare(strict_types=1);
namespace Crea\SecurityBundle\Voter;
use Crea\SecurityBundle\Entity\Right;
use Crea\SecurityBundle\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @extends Voter<string, Right>
*/
class RightVoter extends Voter
{
public const RIGHT_LIST = 'SECURITY_RIGHT_LIST';
protected function supports($attribute, $subject): bool
{
return self::RIGHT_LIST == $attribute && (null === $subject || $subject instanceof Right);
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
/** @var User $loggedUser */
$loggedUser = $token->getUser();
if (!$loggedUser instanceof UserInterface) {
return false;
}
if (self::RIGHT_LIST == $attribute) {
return $this->voteOnList($loggedUser);
}
return false;
}
private function voteOnList(UserInterface $loggedUser): bool
{
if (in_array(self::RIGHT_LIST, $loggedUser->getRoles())) {
return true;
}
return false;
}
}