src/Voter/__Quote/QuoteQuoteVoter.php line 9

Open in your IDE?
  1. <?php
  2. namespace App\Voter\__Quote;
  3. use App\Entity\Booking\Quote;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. class QuoteQuoteVoter extends Voter
  8. {
  9.     const LIST = "QUOTE_QUOTE_LIST";
  10.     const DETAILS_BASE "QUOTE_QUOTE_DETAILS_BASE";
  11.     const UPDATE "QUOTE_QUOTE_UPDATE";
  12.     protected function supports($attribute$subject): bool
  13.     {
  14.         return in_array($attribute, [
  15.                 self::LIST,
  16.                 self::DETAILS_BASE,
  17.                 self::UPDATE,
  18.             ]) && ($subject === null || $subject instanceof Quote);
  19.     }
  20.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  21.     {
  22.         $loggedUser $token->getUser();
  23.         if (!$loggedUser instanceof UserInterface) {
  24.             return false;
  25.         }
  26.         switch ($attribute) {
  27.             case self::LIST:
  28.                 return $this->voteOnList($loggedUser);
  29.             case self::DETAILS_BASE:
  30.                 return $this->voteOnDetailsBase($subject$loggedUser);
  31.             case self::UPDATE:
  32.                 return $this->voteOnUpdate($subject$loggedUser);
  33.         }
  34.         return false;
  35.     }
  36.     private function voteOnList(UserInterface $loggedUser): bool
  37.     {
  38.         return in_array(self::LIST, $loggedUser->getRoles());
  39.     }
  40.     private function voteOnDetailsBase(?Quote $quoteUserInterface $loggedUser): bool
  41.     {
  42.         if ($quote === null) {
  43.             return false;
  44.         }
  45.         return in_array(self::DETAILS_BASE$loggedUser->getRoles());
  46.     }
  47.     private function voteOnUpdate(?Quote $quoteUserInterface $loggedUser): bool
  48.     {
  49.         if ($quote === null) {
  50.             return false;
  51.         }
  52.         return in_array(self::UPDATE$loggedUser->getRoles());
  53.     }
  54. }