src/Security/ApplicationVoter.php line 9

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\Vacancy;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. class ApplicationVoter extends Voter
  7. {
  8.     public const AUTHORIZED_APPLY 'authorized_apply';
  9.     /**
  10.      * @param string $attribute
  11.      * @param mixed  $subject
  12.      *
  13.      * @return bool
  14.      */
  15.     protected function supports($attribute$subject)
  16.     {
  17.         if (!\in_array($attribute, [self::AUTHORIZED_APPLY], true)) {
  18.             return false;
  19.         }
  20.         return Vacancy::class === \get_class($subject);
  21.     }
  22.     /**
  23.      * @param string $attribute
  24.      * @param mixed  $subject
  25.      *
  26.      * @return bool
  27.      */
  28.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  29.     {
  30.         /** @var Vacancy $vacancy */
  31.         $vacancy $subject;
  32.         $domain $vacancy->getDomain();
  33.         if (!$domain) {
  34.             return true;
  35.         }
  36.         switch ($attribute) {
  37.             case self::AUTHORIZED_APPLY:
  38.                 if ($domain->getPublic()) {
  39.                     return true;
  40.                 }
  41.                 if ($domain->getPrivate() && \in_array('ROLE_SITE_MOBILITY_USER'$token->getRoleNames(), true)) {
  42.                     return true;
  43.                 }
  44.                 return false;
  45.             default:
  46.                 return false;
  47.         }
  48.     }
  49. }