src/Component/Core/Api/Security/Voter/VendorLogoImageVoter.php line 22

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\LogoImageInterface;
  12. use BitBag\OpenMarketplace\Component\Vendor\Entity\ShopUserInterface;
  13. use BitBag\OpenMarketplace\Component\Vendor\Entity\VendorInterface;
  14. use Sylius\Bundle\ApiBundle\Context\UserContextInterface;
  15. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  16. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  17. final class VendorLogoImageVoter extends Voter
  18. {
  19.     public const DELETE 'VENDOR_IMAGE_DELETE';
  20.     public function __construct(
  21.         private UserContextInterface $userContext
  22.     ) {
  23.     }
  24.     protected function supports(string $attribute$subject): bool
  25.     {
  26.         if (!in_array($attribute, [self::DELETE])) {
  27.             return false;
  28.         }
  29.         if (!$subject instanceof LogoImageInterface) {
  30.             return false;
  31.         }
  32.         return true;
  33.     }
  34.     /**
  35.      * @param LogoImageInterface $subject
  36.      */
  37.     protected function voteOnAttribute(
  38.         string $attribute,
  39.         $subject,
  40.         TokenInterface $token
  41.     ): bool {
  42.         if (self::DELETE === $attribute) {
  43.             return $this->voteOnDelete($attribute$subject);
  44.         }
  45.         return true;
  46.     }
  47.     private function voteOnDelete(string $attributeLogoImageInterface $subject): bool
  48.     {
  49.         $currentVendor $this->getCurrentVendor();
  50.         /** @var ?VendorInterface $subjectOwner */
  51.         $subjectOwner $subject->getOwner();
  52.         if (null === $currentVendor || null === $subjectOwner) {
  53.             return false;
  54.         }
  55.         if ($currentVendor->getId() !== $subjectOwner->getId()) {
  56.             return false;
  57.         }
  58.         return true;
  59.     }
  60.     private function getCurrentVendor(): ?VendorInterface
  61.     {
  62.         $shopUser $this->userContext->getUser();
  63.         if (!$shopUser instanceof ShopUserInterface) {
  64.             return null;
  65.         }
  66.         /** @var CustomerInterface $customer */
  67.         $customer $shopUser->getCustomer();
  68.         if (!$customer instanceof CustomerInterface) {
  69.             return null;
  70.         }
  71.         return $customer->getOrganizationVendor();
  72.     }
  73. }