src/Voters/QuoteVoter.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Voters;
  3. use App\Model\Quote\Quote;
  4. use App\Security\SecurityConfig;
  5. use App\Security\User;
  6. use LogicException;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. /**
  10.  * Class QuoteVoter.
  11.  */
  12. final class QuoteVoter extends Voter
  13. {
  14.     /**
  15.      * @param string $attribute
  16.      * @param Quote  $subject
  17.      *
  18.      * @return bool
  19.      */
  20.     protected function supports($attribute$subject): bool
  21.     {
  22.         if (!in_array($attribute, [
  23.                 SecurityConfig::QUOTE_SHOW,
  24.                 SecurityConfig::QUOTE_CUD,
  25.                 SecurityConfig::MY_QUOTE_BY_DEFAULT,
  26.                 SecurityConfig::QUOTE_DUPLICATE,
  27.             ], true)) {
  28.             return false;
  29.         }
  30.         return true;
  31.     }
  32.     /**
  33.      * @param string         $attribute
  34.      * @param Quote          $subject
  35.      * @param TokenInterface $token
  36.      *
  37.      * @return bool
  38.      */
  39.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  40.     {
  41.         $user $token->getUser();
  42.         if (!$user instanceof User) {
  43.             return false;
  44.         }
  45.         switch ($attribute) {
  46.             case SecurityConfig::QUOTE_SHOW:
  47.                 return $this->canViewQuote($user);
  48.             case SecurityConfig::QUOTE_CUD:
  49.                 return $this->canCudQuote($user);
  50.             case SecurityConfig::MY_QUOTE_BY_DEFAULT:
  51.                 return $this->hasHisQuoteByDefault($user);
  52.             case SecurityConfig::QUOTE_DUPLICATE:
  53.                 return $this->canDuplicateQuote($user$subject);
  54.         }
  55.         throw new LogicException('This code should not be reached!');
  56.     }
  57.     /**
  58.      * @param User $user
  59.      *
  60.      * @return bool
  61.      */
  62.     private function canViewQuote(User $user): bool
  63.     {
  64.         if ($user->isAdmin()) {
  65.             return true;
  66.         }
  67.         return in_array(SecurityConfig::QUOTE_SHOW$user->getRoles(), true);
  68.     }
  69.     /**
  70.      * @param User $user
  71.      *
  72.      * @return bool
  73.      */
  74.     private function canCudQuote(User $user): bool
  75.     {
  76.         if ($user->isAdmin()) {
  77.             return true;
  78.         }
  79.         return in_array(SecurityConfig::QUOTE_CUD$user->getRoles(), true);
  80.     }
  81.     /**
  82.      * @param User $user
  83.      *
  84.      * @return bool
  85.      */
  86.     private function hasHisQuoteByDefault(User $user): bool
  87.     {
  88.         return in_array(SecurityConfig::MY_QUOTE_BY_DEFAULT$user->getRoles(), true);
  89.     }
  90.     /**
  91.      * @param User  $user
  92.      * @param Quote $quote
  93.      *
  94.      * @return bool
  95.      */
  96.     private function canDuplicateQuote(User $userQuote $quote): bool
  97.     {
  98.         return $quote->getCustomerId() === $user->getCustomerId();
  99.     }
  100. }