src/Security/JobProfileVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\User;
  4. use LogicException;
  5. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. class JobProfileVoter extends Voter
  9. {
  10.     public const VIEW_OVERVIEW 'view_job_profile_overview';
  11.     protected ParameterBagInterface $parameterBag;
  12.     /**
  13.      * JobProfileVoter constructor.
  14.      */
  15.     public function __construct(ParameterBagInterface $parameterBag)
  16.     {
  17.         $this->parameterBag $parameterBag;
  18.     }
  19.     protected function supports($attribute$subject)
  20.     {
  21.         return \in_array($attribute, [
  22.             self::VIEW_OVERVIEW,
  23.         ], true);
  24.     }
  25.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  26.     {
  27.         switch ($attribute) {
  28.             case self::VIEW_OVERVIEW:
  29.                 return $this->canViewOverview($token);
  30.         }
  31.         throw new LogicException('This code may never be reached');
  32.     }
  33.     private function canViewOverview(TokenInterface $token)
  34.     {
  35.         if ($this->parameterBag->get('mobility_public_profiles')) {
  36.             return true;
  37.         }
  38.         $user $token->getUser();
  39.         if (!$user instanceof User) {
  40.             return false;
  41.         }
  42.         return \in_array('ROLE_SITE_MOBILITY_USER'$user->getRoles(), true);
  43.     }
  44. }