src/Component/Core/Api/Security/Voter/TranslatableVendorAwareVoter.php line 21

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\Core\Api\Context\VendorContextInterface;
  11. use BitBag\OpenMarketplace\Component\Vendor\Entity\VendorInterface;
  12. use BitBag\OpenMarketplace\Component\Vendor\VendorAwareInterface;
  13. use Sylius\Component\Resource\Model\TranslationInterface;
  14. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  15. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  16. final class TranslatableVendorAwareVoter extends Voter
  17. {
  18.     private array $supportedAttributes = [
  19.         'TRANSLATABLE_VENDOR_AWARE_OBJECT_CREATE',
  20.         'TRANSLATABLE_VENDOR_AWARE_OBJECT_READ',
  21.         'TRANSLATABLE_VENDOR_AWARE_OBJECT_UPDATE',
  22.         'TRANSLATABLE_VENDOR_AWARE_OBJECT_DELETE',
  23.     ];
  24.     public function __construct(
  25.         private VendorContextInterface $vendorContext
  26.     ) {
  27.     }
  28.     /** @param TranslationInterface $subject */
  29.     protected function supports(string $attribute$subject): bool
  30.     {
  31.         if (!in_array($attribute$this->supportedAttributes)) {
  32.             return false;
  33.         }
  34.         if (!$subject instanceof TranslationInterface) {
  35.             return false;
  36.         }
  37.         return $subject->getTranslatable() instanceof VendorAwareInterface;
  38.     }
  39.     /**
  40.      * @param TranslationInterface $subject
  41.      */
  42.     protected function voteOnAttribute(
  43.         string $attribute,
  44.         $subject,
  45.         TokenInterface $token
  46.     ): bool {
  47.         if (!in_array($attribute$this->supportedAttributes)) {
  48.             return true;
  49.         }
  50.         $translatable $subject->getTranslatable();
  51.         if (!$translatable instanceof VendorAwareInterface) {
  52.             return true;
  53.         }
  54.         $vendor $this->vendorContext->getVendor();
  55.         if (null === $vendor) {
  56.             return false;
  57.         }
  58.         /** @var VendorInterface $translatableOwner */
  59.         $translatableOwner $translatable->getVendor();
  60.         return $translatableOwner->getId() === $vendor->getId();
  61.     }
  62. }