src/Voters/AdvancedQuoteVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Voters;
  3. use App\Security\SecurityConfig;
  4. use App\Security\User;
  5. use LogicException;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\Security;
  9. class AdvancedQuoteVoter extends Voter
  10. {
  11.     const QUOTE_LINES 'quote_lines';
  12.     const QUOTE_LINES_SEND_PDF 'quote_lines_send_pdf';
  13.     /**
  14.      * @var Security
  15.      */
  16.     private $security;
  17.     /**
  18.      * @param Security $security
  19.      */
  20.     public function __construct(Security $security)
  21.     {
  22.         $this->security $security;
  23.     }
  24.     /**
  25.      * @param $attribute
  26.      * @param $subject
  27.      *
  28.      * @return bool
  29.      */
  30.     protected function supports($attribute$subject): bool
  31.     {
  32.         return in_array($attribute, [
  33.             self::QUOTE_LINES,
  34.             self::QUOTE_LINES_SEND_PDF,
  35.         ], true);
  36.     }
  37.     /**
  38.      * @param $attribute
  39.      * @param $subject
  40.      * @param TokenInterface $token
  41.      *
  42.      * @return bool
  43.      */
  44.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  45.     {
  46.         $user $token->getUser();
  47.         if (!$user instanceof User) {
  48.             return false;
  49.         }
  50.         switch ($attribute) {
  51.             case self::QUOTE_LINES:
  52.                 return $this->canAddAdvancedQuoteLines();
  53.             case self::QUOTE_LINES_SEND_PDF:
  54.                 return $this->canSendQuotesPdf();
  55.         }
  56.         throw new LogicException('This should never happen');
  57.     }
  58.     /**
  59.      * @return bool
  60.      */
  61.     private function canAccessModuleAdvancedQuote(): bool
  62.     {
  63.         return $this->security->isGranted(SecurityConfig::MODULE_QUOTE_LINE);
  64.     }
  65.     /**
  66.      * @return bool
  67.      */
  68.     private function canAddAdvancedQuoteLines(): bool
  69.     {
  70.         return $this->canAccessModuleAdvancedQuote();
  71.     }
  72.     /**
  73.      * @return bool
  74.      */
  75.     private function canSendQuotesPdf(): bool
  76.     {
  77.         return $this->canAccessModuleAdvancedQuote();
  78.     }
  79. }