src/Security/Voter/FeaturesVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Company;
  4. use App\Entity\User;
  5. use App\Service\Utils\Constante\AbonnementConst;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. class FeaturesVoter extends Voter
  10. {
  11.     public const FEAT_SECOURS'FEAT_SECOURS_ACCESS';
  12.     protected function supports(string $attribute$subject)
  13.     {
  14.         return in_array($attribute, [self::FEAT_SECOURS]);
  15.     }
  16.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token)
  17.     {
  18.         //utilisateur connecté
  19.         $user $token->getUser();
  20.         // if the user is anonymous, do not grant access
  21.         if (!$user instanceof UserInterface) {
  22.             return false;
  23.         }
  24.         switch ($attribute) {
  25.             case self::FEAT_SECOURS:
  26.                 return $this->canShowSecours($user);
  27.         }
  28.         return false;
  29.     }
  30.     private function canShowSecours(UserInterface $userTarget): bool
  31.     {
  32.         if(in_array(AbonnementConst::FEAT_SECOURS,$userTarget->getCompany()->getFeatures())){
  33.             return true;
  34.         }
  35.         return false;
  36.     }
  37. }