app/Plugin/CustomerGroup42/Security/Authorization/Voter/ProductVoter.php line 21

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of CustomerGroup
  4.  *
  5.  * Copyright(c) Akira Kurozumi <info@a-zumi.net>
  6.  *
  7.  * https://a-zumi.net
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Plugin\CustomerGroup42\Security\Authorization\Voter;
  13. use Eccube\Entity\Product;
  14. use Plugin\CustomerGroup42\Service\Gate\GateContext;
  15. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  16. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  17. class ProductVoter extends Voter
  18. {
  19.     public const VIEW 'view';
  20.     public const CART 'cart';
  21.     /**
  22.      * @var GateContext
  23.      */
  24.     private GateContext $gateContext;
  25.     public function __construct(GateContext $gateContext)
  26.     {
  27.         $this->gateContext $gateContext;
  28.     }
  29.     /**
  30.      * @param string $attribute
  31.      * @param $subject
  32.      *
  33.      * @return bool
  34.      */
  35.     protected function supports(string $attribute$subject): bool
  36.     {
  37.         return $subject instanceof Product && \in_array($attribute, [self::VIEWself::CART], true);
  38.     }
  39.     /**
  40.      * @param string $attribute
  41.      * @param $subject
  42.      * @param TokenInterface $token
  43.      *
  44.      * @return bool
  45.      */
  46.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  47.     {
  48.         return $this->gateContext->permit($attribute$subject$token);
  49.     }
  50. }