src/Component/Core/Api/Security/Voter/VendorAwareVoter.php line 20

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\Messaging\Entity\ConversationInterface;
  12. use BitBag\OpenMarketplace\Component\Vendor\VendorAwareInterface;
  13. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  14. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  15. final class VendorAwareVoter extends Voter
  16. {
  17.     private array $supportedAttributes = [
  18.         'VENDOR_AWARE_OBJECT_CREATE',
  19.         'VENDOR_AWARE_OBJECT_READ',
  20.         'VENDOR_AWARE_OBJECT_UPDATE',
  21.         'VENDOR_AWARE_OBJECT_DELETE',
  22.     ];
  23.     public function __construct(
  24.         private VendorContextInterface $vendorContext
  25.     ) {
  26.     }
  27.     protected function supports(string $attribute$subject): bool
  28.     {
  29.         $supportsAttribute in_array($attribute$this->supportedAttributes);
  30.         $supportsSubject = ($subject instanceof VendorAwareInterface) || ($subject instanceof ConversationInterface);
  31.         return $supportsAttribute && $supportsSubject;
  32.     }
  33.     /**
  34.      * @param VendorAwareInterface $subject
  35.      */
  36.     protected function voteOnAttribute(
  37.         string $attribute,
  38.         $subject,
  39.         TokenInterface $token
  40.     ): bool {
  41.         if (
  42.             in_array($attribute$this->supportedAttributes) &&
  43.             null === $this->vendorContext->getVendor()
  44.         ) {
  45.             return false;
  46.         }
  47.         return true;
  48.     }
  49. }