<?php
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;
class RightVoter extends Voter
{
const RIGHT_LIST = "SECURITY_RIGHT_LIST";
/**
* @inheritDoc
*/
protected function supports($attribute, $subject): bool
{
return $attribute == self::RIGHT_LIST && ($subject === null || $subject instanceof Right);
}
/**
* @inheritDoc
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
/** @var User $loggedUser */
$loggedUser = $token->getUser();
if (!$loggedUser instanceof UserInterface)
return false;
if ($attribute == self::RIGHT_LIST) {
return $this->voteOnList($loggedUser);
}
return false;
}
private function voteOnList(UserInterface $loggedUser): bool
{
if (in_array(self::RIGHT_LIST, $loggedUser->getRoles())) {
return true;
}
return false;
}
}