<?php
namespace WellCommerce\Bundle\AppBundle\Security;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use WellCommerce\Bundle\AppBundle\Entity\User;
class AdminVoter extends Voter
{
protected EntityManagerInterface $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
protected function supports($attribute, $subject)
{
return true;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
if (!$user instanceof User) {
return false;
}
$permission = $this->entityManager->getRepository(User::class)->getUserPermission($attribute, $user);
if (empty($permission)) {
return false;
}
return true;
}
}