src/Security/Voter/CustomerPortalVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Customer\Customer;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. class CustomerPortalVoter extends Voter
  8. {
  9.     const MANAGE_PROTOCOLS "customerPortal:protocols:manage";
  10.     /** @var EntityManagerInterface */
  11.     private $entityManager;
  12.     /**
  13.      * @param EntityManagerInterface $entityManager
  14.      */
  15.     public function __construct(EntityManagerInterface $entityManager)
  16.     {
  17.         $this->entityManager $entityManager;
  18.     }
  19.     protected function supports(string $attribute$subject)
  20.     {
  21.         return $attribute == self::MANAGE_PROTOCOLS;
  22.     }
  23.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token)
  24.     {
  25.         $customer $this->entityManager
  26.             ->getRepository(Customer::class)
  27.             ->getCustomerForUser($token->getUser());
  28.         if ($customer === null) {
  29.             return false;
  30.         }
  31.         switch ($attribute) {
  32.             case self::MANAGE_PROTOCOLS:
  33.                 return $customer->getEnableProtocolManagement();
  34.             default:
  35.                 return false;
  36.         }
  37.     }
  38. }