src/Security/Voter/ConsultantVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Gos\Consultant\ConsultantSettings;
  4. use App\Entity\Gos\User;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. class ConsultantVoter extends Voter
  9. {
  10.     public const IS_CONSULTANT 'is_consultant';
  11.     private $em;
  12.     public function __construct(EntityManagerInterface $em)
  13.     {
  14.         $this->em $em;
  15.     }
  16.     protected function supports($attribute$subject): bool
  17.     {
  18.         return $attribute === self::IS_CONSULTANT;
  19.     }
  20.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  21.     {
  22.         $user $token->getUser();
  23.         if (!$user instanceof User) {
  24.             return false;
  25.         }
  26.         switch ($attribute) {
  27.             case self::IS_CONSULTANT:
  28.                 return $this->isConsultant($user);
  29.         }
  30.         throw new \LogicException('This code should not be reached!');
  31.     }
  32.     private function isConsultant(User $user): bool
  33.     {
  34.         $consultantSettings $this->em->getRepository(ConsultantSettings::class)->findSettings();
  35.         $consultants $consultantSettings->getConsultants()->toArray();
  36.         return $user->hasRole('ROLE_SUPER_CONSULTANT') || in_array($user$consultantstrue);
  37.     }
  38. }