src/Security/Voter/AppVoter.php line 9

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Utilisateur\Utilisateur;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. class AppVoter extends Voter
  7. {
  8.     public const SUPER_ADMIN_TYPE 1;
  9.     public const ADMIN_TYPE 2;
  10.     public const SALESMAN_TYPE 3;
  11.     public const PICKER_TYPE 4;
  12.     protected function supports(string $attribute$subject): bool
  13.     {
  14.         return (empty($subject));
  15.     }
  16.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  17.     {
  18.         return true;
  19.     }
  20.     public function vote(TokenInterface $token$subject, array $attributes): int
  21.     {
  22.         /** @var ?Utilisateur $user */
  23.         $user $token->getUser();
  24.         if ($user === null) {
  25.             return self::ACCESS_ABSTAIN;
  26.         }
  27.         if($user->getType()->getId() === self::SUPER_ADMIN_TYPE){
  28.             return self::ACCESS_GRANTED;
  29.         }
  30.         foreach ($attributes as $attribute) {
  31.             if (is_numeric($attribute)) {
  32.                 if ($attribute == $user->getType()->getId()) {
  33.                     return self::ACCESS_GRANTED;
  34.                 } else {
  35.                     return self::ACCESS_DENIED;
  36.                 }
  37.             }
  38.             if ($attribute === $user->getType()->getLibelle()) {
  39.                 return self::ACCESS_GRANTED;
  40.             }
  41.         }
  42.         return self::ACCESS_ABSTAIN;
  43.     }
  44. }