src/Component/Core/Api/Security/Voter/VendorBackgroundImageVoter.php line 23

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\BackgroundImageInterface;
  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. use Webmozart\Assert\Assert;
  18. final class VendorBackgroundImageVoter extends Voter
  19. {
  20.     public const DELETE 'VENDOR_BACKGROUND_IMAGE_DELETE';
  21.     public function __construct(
  22.         private UserContextInterface $userContext
  23.     ) {
  24.     }
  25.     protected function supports(string $attribute$subject): bool
  26.     {
  27.         if (!in_array($attribute, [self::DELETE])) {
  28.             return false;
  29.         }
  30.         if (!$subject instanceof BackgroundImageInterface) {
  31.             return false;
  32.         }
  33.         return true;
  34.     }
  35.     /**
  36.      * @param BackgroundImageInterface $subject
  37.      */
  38.     protected function voteOnAttribute(
  39.         string $attribute,
  40.         $subject,
  41.         TokenInterface $token
  42.     ): bool {
  43.         if (self::DELETE === $attribute) {
  44.             return $this->voteOnDelete($attribute$subject);
  45.         }
  46.         return true;
  47.     }
  48.     private function voteOnDelete(string $attributeBackgroundImageInterface $subject): bool
  49.     {
  50.         $currentVendor $this->getCurrentVendor();
  51.         /** @var ?VendorInterface $subjectOwner */
  52.         $subjectOwner $subject->getOwner();
  53.         if (null === $currentVendor || null === $subjectOwner) {
  54.             return false;
  55.         }
  56.         if ($currentVendor->getId() !== $subjectOwner->getId()) {
  57.             return false;
  58.         }
  59.         return true;
  60.     }
  61.     private function getCurrentVendor(): ?VendorInterface
  62.     {
  63.         $shopUser $this->userContext->getUser();
  64.         if (!$shopUser instanceof ShopUserInterface) {
  65.             return null;
  66.         }
  67.         /** @var CustomerInterface $customer */
  68.         $customer $shopUser->getCustomer();
  69.         Assert::isInstanceOf($customerCustomerInterface::class);
  70.         return $customer->getOrganizationVendor();
  71.     }
  72. }