src/Component/Core/Api/Security/Voter/VendorOwnsVariantVoter.php line 19

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file has been created by developers from BitBag.
  4.  * Feel free to contact us once you face any issues or want to start
  5.  * You can find more information about us on https://bitbag.io and write us
  6.  * an email on hello@bitbag.io.
  7.  */
  8. declare(strict_types=1);
  9. namespace BitBag\OpenMarketplace\Component\Core\Api\Security\Voter;
  10. use BitBag\OpenMarketplace\Component\Customer\Model\CustomerInterface;
  11. use BitBag\OpenMarketplace\Component\Vendor\Entity\ShopUserInterface;
  12. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  13. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  14. final class VendorOwnsVariantVoter extends Voter
  15. {
  16.     public const OWNS_VARIANT 'OWNS_VARIANT';
  17.     protected function supports($attribute$subject)
  18.     {
  19.         if (!in_array($attribute, [self::OWNS_VARIANT])) {
  20.             return false;
  21.         }
  22.         return true;
  23.     }
  24.     /*
  25.      * This method call is ignored because phpstan force us to type hint arguments but this method in symfony 4.4
  26.      * is declared without so type hinted arguments cause trouble
  27.      */
  28.     /** @phpstan-ignore-next-line */
  29.     protected function voteOnAttribute(
  30.         $attribute,
  31.         $subject,
  32.         TokenInterface $token
  33.     ) {
  34.         $user $token->getUser();
  35.         if (!$user instanceof ShopUserInterface || null == $subject) {
  36.             return false;
  37.         }
  38.         switch ($attribute) {
  39.             case self::OWNS_VARIANT:
  40.                 return $this->doesUserOwnTheData($subject$user);
  41.             default:
  42.                 return false;
  43.         }
  44.     }
  45.     private function doesUserOwnTheData(object $dataShopUserInterface $user): bool
  46.     {
  47.         /** @var CustomerInterface $customer */
  48.         $customer $user->getCustomer();
  49.         $loggedInVendor $customer->getOrganizationVendor();
  50.         /** @phpstan-ignore-next-line */
  51.         $vendorData $data->getProduct()->getVendor();
  52.         if ($loggedInVendor === $vendorData) {
  53.             return true;
  54.         }
  55.         return false;
  56.     }
  57. }