src/Security/Voter/EmailVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  4. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  5. use Symfony\Component\Security\Core\Security;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. class EmailVoter extends Voter
  8. {
  9.     public const OWNER_USER 'OWNER_USER';
  10.     /** @var Security */
  11.     private $security;
  12.     public function __construct(Security $security)
  13.     {
  14.         $this->security $security;
  15.     }
  16.     protected function supports(string $attribute$subject): bool
  17.     {
  18.         return in_array($attribute, [self::OWNER_USER]);
  19.     }
  20.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  21.     {
  22.         if(is_null($subject)) {
  23.             return true;
  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.         switch ($attribute) {
  33.             case self::OWNER_USER:
  34.                 foreach ($user->getCompanies() as $company) {
  35.                     if ($company->getEmails()->contains($subject)) {
  36.                         return true;
  37.                     }
  38.                 }
  39.                 return false;
  40.                 break;
  41.         }
  42.         return false;
  43.     }
  44. }