bundles/BackofficeBundle/Security/Authorization/Voter/SalerecordVoter.php line 13

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Bdm\BackofficeBundle\Security\Authorization\Voter;
  4. use Bdm\OAuth2Bundle\Entity\AccessToken;
  5. use Bdm\PublicApiBundle\Entity\Salerecord;
  6. use Doctrine\ORM\EntityManager;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. class SalerecordVoter extends Voter
  10. {
  11. const VIEW = 'VIEW';
  12. /**
  13. * @param EntityManager $oEntityManager entity manager
  14. */
  15. public function __construct(
  16. private readonly EntityManager $oEntityManager
  17. ) {
  18. }
  19. /**
  20. * @param string $sAttribute attribute
  21. * @param mixed $oSubject subject
  22. * @return bool
  23. */
  24. public function supports($sAttribute, $oSubject)
  25. {
  26. if (!in_array($sAttribute, [self::VIEW])) {
  27. return false;
  28. }
  29. if (!$oSubject instanceof Salerecord) {
  30. return false;
  31. }
  32. return true;
  33. }
  34. /**
  35. * @param string $sAttribute attribute
  36. * @param Salerecord $oSalerecord Salerecord
  37. * @param TokenInterface $oToken token
  38. *
  39. * @return bool
  40. */
  41. public function voteOnAttribute($sAttribute, $oSalerecord, TokenInterface $oToken)
  42. {
  43. $oAccessToken = $this->oEntityManager
  44. ->getRepository(AccessToken::class)
  45. ->findOneBy(['token' => $oToken->getToken()]);
  46. $oMerchant = $oAccessToken->getClient()->getMerchant();
  47. return $oMerchant == $oSalerecord->getSeller()->getApplication()->getMerchant();
  48. }
  49. }