src/Security/OrderVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\System\Employee;
  4. use App\Entity\System\Order;
  5. use App\Entity\System\PaymentMethod;
  6. use App\Entity\System\Role;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. class OrderVoter extends Voter
  11. {
  12.     public const SHOW 'show';
  13.     public const PUBLISH 'publish';
  14.     public const CHECK_PAYPAL 'publish';
  15.     private AuthorizationCheckerInterface $authorizationChecker;
  16.     public function __construct(AuthorizationCheckerInterface $authorizationChecker)
  17.     {
  18.         $this->authorizationChecker $authorizationChecker;
  19.     }
  20.     /**
  21.      * @param Order $subject
  22.      */
  23.     protected function supports(string $attribute$subject): bool
  24.     {
  25.         if (!(get_class($subject) === Order::class)) {
  26.             return false;
  27.         }
  28.         return $this->supportsAttribute($attribute);
  29.     }
  30.     public function supportsAttribute(string $attribute): bool
  31.     {
  32.         $voter = new \ReflectionClass(__CLASS__);
  33.         $allowedValues $voter->getConstants();
  34.         if (!\array_key_exists($attribute, \array_flip($allowedValues))) {
  35.             return false;
  36.         }
  37.         return true;
  38.     }
  39.     /**
  40.      * @param Order $order
  41.      */
  42.     protected function voteOnAttribute(string $attribute$orderTokenInterface $token): bool
  43.     {
  44.         $user $token->getUser();
  45.         if (!$user instanceof Employee) {
  46.             return false;
  47.         }
  48.         switch ($attribute) {
  49.             case self::SHOW:
  50.                 return $this->showAllowed($order$user);
  51.             case self::PUBLISH:
  52.                 return $this->publishAllowed($order);
  53.             case self::CHECK_PAYPAL:
  54.                 return $this->checkPayPal($order);
  55.             default:
  56.                 throw new \LogicException('This code should not be reached!');
  57.         }
  58.     }
  59.     private function showAllowed(Order $orderEmployee $user): bool
  60.     {
  61.         if ($this->authorizationChecker->isGranted(Role::ROLE_DEV)) {
  62.             return true;
  63.         }
  64.         return false;
  65.     }
  66.     private function publishAllowed(Order $order): bool
  67.     {
  68.         if (!$this->authorizationChecker->isGranted(Role::ROLE_DEV)) {
  69.             return false;
  70.         }
  71.         return $order->getA4bUuid() === null;
  72.     }
  73.     private function checkPayPal(Order $order): bool
  74.     {
  75.         if (
  76.             !$this->authorizationChecker->isGranted(Role::ROLE_DEV)
  77.             || $order->getPaypalPayerEmail() !== null || $order->getTransactionId() === null
  78.         ) {
  79.             return false;
  80.         }
  81.         return $order->getPayment() === PaymentMethod::PAYMENT_METHOD_PAYPAL;
  82.     }
  83. }