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.         //dump($user->getType());
  28.         if($user->getType()->getId() === self::SUPER_ADMIN_TYPE){
  29.             return self::ACCESS_GRANTED;
  30.         }
  31.         foreach ($attributes as $attribute) {
  32.             if (is_numeric($attribute)) {
  33.                 if ($attribute == $user->getType()->getId()) {
  34.                     return self::ACCESS_GRANTED;
  35.                 } else {
  36.                     return self::ACCESS_DENIED;
  37.                 }
  38.             }
  39.             if ($attribute === $user->getType()->getLibelle()) {
  40.                 return self::ACCESS_GRANTED;
  41.             }
  42.         }
  43.         return self::ACCESS_ABSTAIN;
  44.     }
  45. }