src/WellCommerce/Bundle/AppBundle/Security/AdminVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace WellCommerce\Bundle\AppBundle\Security;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use WellCommerce\Bundle\AppBundle\Entity\User;
  7. class AdminVoter extends Voter
  8. {
  9.     protected EntityManagerInterface $entityManager;
  10.     public function __construct(EntityManagerInterface $entityManager)
  11.     {
  12.         $this->entityManager $entityManager;
  13.     }
  14.     protected function supports($attribute$subject)
  15.     {
  16.         return true;
  17.     }
  18.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  19.     {
  20.         $user $token->getUser();
  21.         if (!$user instanceof User) {
  22.             return false;
  23.         }
  24.         $permission $this->entityManager->getRepository(User::class)->getUserPermission($attribute$user);
  25.         if (empty($permission)) {
  26.             return false;
  27.         }
  28.         return true;
  29.     }
  30. }