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.         /** @var ?Utilisateur $user */
  19.         $user $token->getUser();
  20.         if ($user === null) {
  21.             return self::ACCESS_ABSTAIN;
  22.         }
  23.         if($user->getType()->getId() === self::SUPER_ADMIN_TYPE){
  24.             return self::ACCESS_GRANTED;
  25.         }
  26.         $attributes = [$attribute];
  27.         foreach ($attributes as $attribute) {
  28.             if (is_numeric($attribute)) {
  29.                 if ($attribute == $user->getType()->getId()) {
  30.                     return self::ACCESS_GRANTED;
  31.                 } else {
  32.                     return self::ACCESS_DENIED;
  33.                 }
  34.             }
  35.             if ($attribute === $user->getType()->getLibelle()) {
  36.                 return self::ACCESS_GRANTED;
  37.             }
  38.         }
  39.         return self::ACCESS_ABSTAIN;
  40.     }
  41.     /*
  42.     public function vote(TokenInterface $token, $subject, array $attributes): int
  43.     {
  44.     }
  45.     */
  46. }