src/Component/Core/Vendor/Security/Voter/TokenOwningVoter.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\Vendor\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 TokenOwningVoter extends Voter
  16. {
  17.     public const UPDATE 'UPDATE';
  18.     protected function supports($attribute$subject)
  19.     {
  20.         if (!in_array($attribute, [self::UPDATE])) {
  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::UPDATE:
  43.                 return $this->doesUserOwnTheData($vendorUpdateData$user);
  44.             default:
  45.                 return false;
  46.         }
  47.     }
  48.     private function doesUserOwnTheData(
  49.         ProfileUpdateInterface $profileUpdate,
  50.         ShopUserInterface $user
  51.     ): bool {
  52.         /** @var CustomerInterface $customer */
  53.         $customer $user->getCustomer();
  54.         $loggedInVendor $customer->getOrganizationVendor();
  55.         $vendorData $profileUpdate->getVendor();
  56.         if ($loggedInVendor === $vendorData) {
  57.             return true;
  58.         }
  59.         return false;
  60.     }
  61. }