src/Security/Voter/FormLeadVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Form;
  4. use App\Entity\FormLead;
  5. use App\Entity\FormLeadData;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\Security;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. class FormLeadVoter extends Voter
  11. {
  12.     public const VIEW 'VIEW';
  13.     /** @var Security */
  14.     private $security;
  15.     public function __construct(Security $security)
  16.     {
  17.         $this->security $security;
  18.     }
  19.     protected function supports(string $attribute$formLead): bool
  20.     {
  21.         return in_array($attribute, [self::VIEW]) && ($formLead instanceof FormLead);
  22.     }
  23.     protected function voteOnAttribute(string $attribute$formLeadTokenInterface $token): bool
  24.     {
  25.         $user $token->getUser();
  26.         if (!$user instanceof UserInterface) {
  27.             return false;
  28.         }
  29.         if ($this->security->isGranted('ROLE_SUPERADMIN')) {
  30.             return true;
  31.         }
  32.         $campaign $formLead->getCampaign();
  33.         switch ($attribute) {
  34.             case self::VIEW:
  35.                 foreach ($user->getCompanies() as $company) {
  36.                     if ($company->getCampaigns()->contains($campaign)) {
  37.                         return true;
  38.                     }
  39.                 }
  40.             return false;
  41.             break;
  42.         }
  43.         return false;
  44.     }
  45. }