src/Component/Core/Common/Security/Voter/ObjectOwningVoter.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\Common\Security\Voter;
  10. use BitBag\OpenMarketplace\Component\Customer\Model\CustomerInterface;
  11. use BitBag\OpenMarketplace\Component\Vendor\Entity\ProfileUpdate\ProfileUpdateInterface;
  12. use BitBag\OpenMarketplace\Component\Vendor\Entity\ShopUserInterface;
  13. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  14. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  15. final class ObjectOwningVoter extends Voter
  16. {
  17.     public const OWNIT 'OWNIT';
  18.     protected function supports($attribute$subject)
  19.     {
  20.         if (!in_array($attribute, [self::OWNIT])) {
  21.             return false;
  22.         }
  23.         return true;
  24.     }
  25.     /*
  26.      * This method call is ignored because phpstan force us to type hint arguments but this method in symfony 4.4
  27.      * is declared without so type hinted arguments cause trouble
  28.      */
  29.     /** @phpstan-ignore-next-line */
  30.     protected function voteOnAttribute(
  31.         $attribute,
  32.         $subject,
  33.         TokenInterface $token
  34.     ) {
  35.         $user $token->getUser();
  36.         if (!$user instanceof ShopUserInterface || null == $subject) {
  37.             return false;
  38.         }
  39.         /** @var ProfileUpdateInterface $vendorUpdateData */
  40.         $vendorUpdateData $subject;
  41.         switch ($attribute) {
  42.             case self::OWNIT:
  43.                 return $this->doesUserOwnTheData($subject$user);
  44.             default:
  45.                 return false;
  46.         }
  47.     }
  48.     private function doesUserOwnTheData(object $dataShopUserInterface $user): bool
  49.     {
  50.         /** @var CustomerInterface $customer */
  51.         $customer $user->getCustomer();
  52.         $loggedInVendor $customer->getOrganizationVendor();
  53.         /** @phpstan-ignore-next-line */
  54.         $vendorData $data->getVendor();
  55.         if ($loggedInVendor === $vendorData) {
  56.             return true;
  57.         }
  58.         return false;
  59.     }
  60. }