bundles/CheckoutBundle/Security/Voter/ApplicationOwnerVoter.php line 13

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Bdm\CheckoutBundle\Security\Voter;
  4. use Bdm\BackofficeBundle\Entity\Application;
  5. use FOS\OAuthServerBundle\Entity\AccessTokenManager;
  6. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. class ApplicationOwnerVoter extends Voter
  10. {
  11. const APPLICATION_OWNER = 'APPLICATION_OWNER';
  12. protected \FOS\OAuthServerBundle\Entity\AccessTokenManager $oTokenManager;
  13. protected \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface $oTokenStorage;
  14. /**
  15. * @param AccessTokenManager $oTokenManager token manager
  16. * @param TokenStorageInterface $oTokenStorage token storage
  17. */
  18. public function __construct(
  19. AccessTokenManager $oTokenManager,
  20. TokenStorageInterface $oTokenStorage
  21. ) {
  22. $this->oTokenManager = $oTokenManager;
  23. $this->oTokenStorage = $oTokenStorage;
  24. }
  25. /**
  26. * @param string $sAttribute attribute
  27. * @param mixed $oSubject subject
  28. * @return bool
  29. */
  30. public function supports($sAttribute, $oSubject)
  31. {
  32. if ($sAttribute !== self::APPLICATION_OWNER) {
  33. return false;
  34. }
  35. if ($oSubject instanceof Application) {
  36. return true;
  37. }
  38. return false;
  39. }
  40. /**
  41. * @param string $sAttribute attribute
  42. * @param mixed $oApplication application
  43. * @param TokenInterface $oToken token
  44. * @return bool
  45. */
  46. public function voteOnAttribute($sAttribute, $oApplication, TokenInterface $oToken)
  47. {
  48. $oAccessToken = $this->oTokenManager->findTokenByToken(
  49. $this->oTokenStorage->getToken()->getToken()
  50. );
  51. $oMerchant = $oAccessToken->getClient()->getMerchant();
  52. return ($oApplication->getMerchant()->getId() === $oMerchant->getId());
  53. }
  54. }