bundles/PublicApiBundle/Security/Authorization/Voter/StoredFileVoter.php line 13

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