bundles/BackofficeBundle/Security/Authorization/Voter/SubscriptionVoter.php line 38

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SubscriptionVoter.php File.
  5. * This file is part of the Payment.net Project.
  6. *
  7. * PHP version 5
  8. *
  9. * @category Application
  10. * @package Bdm\BackofficeBundle\Security\Authorization\Voter
  11. * @author Pavel Baraulya <pbaraulya@bdmultimedia.fr>
  12. * @link http://www.payment.net/
  13. *
  14. * FEATURES :
  15. * ==========
  16. *
  17. * TODO-LIST :
  18. * ===========
  19. *
  20. * HISTORY :
  21. * =========
  22. * 20150709 - Pavel Baraulya
  23. *
  24. **/
  25. namespace Bdm\BackofficeBundle\Security\Authorization\Voter;
  26. use Bdm\BackofficeBundle\Entity\Application;
  27. use Bdm\BackofficeBundle\Entity\Merchant;
  28. use Bdm\CheckoutBundle\Entity\Subscription;
  29. use Bdm\CheckoutBundle\Entity\SubscriptionPlan;
  30. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  31. /**
  32. * Voter for subscription entity
  33. */
  34. class SubscriptionVoter extends AbstractVoter
  35. {
  36. /**
  37. * @param string $sAttribute attribute
  38. * @param mixed $oSubject subject
  39. * @return bool
  40. */
  41. #[\Override]
  42. public function supports($sAttribute, $oSubject)
  43. {
  44. return $oSubject instanceof Subscription && parent::supports($sAttribute, $oSubject);
  45. }
  46. /**
  47. * @param string $sAttr attr
  48. * @param mixed $mEntity entity
  49. * @param TokenInterface $oToken token
  50. *
  51. * @return bool
  52. */
  53. protected function voteOnAttribute($sAttr, $mEntity, TokenInterface $oToken)
  54. {
  55. $oUser = $oToken->getUser();
  56. if (!$oUser instanceof Merchant) {
  57. return false;
  58. }
  59. return (bool) $this->findByOwner($oUser->getId(), $mEntity->getId());
  60. }
  61. /**
  62. * Find entity by owner id
  63. *
  64. * @param int $iOwnerId owner id
  65. * @param int $iEntityId entity id
  66. *
  67. * @return mixed
  68. * @throws \Doctrine\ORM\NonUniqueResultException
  69. */
  70. protected function findByOwner($iOwnerId, $iEntityId)
  71. {
  72. $oQuery = $this->oEm->createQuery('
  73. SELECT s FROM ' . Subscription::class . ' s
  74. WHERE s.oSubscriptionPlan IN (
  75. SELECT sp.iId FROM ' . SubscriptionPlan::class . ' sp
  76. WHERE sp.oApplication IN (
  77. SELECT a.iId FROM ' . Application::class . ' a
  78. WHERE a.oMerchant = :owner_id
  79. )
  80. ) AND s.iId = :entity_id
  81. ');
  82. $oQuery->setParameters(['owner_id' => $iOwnerId, 'entity_id' => $iEntityId]);
  83. return $oQuery->getOneOrNullResult();
  84. }
  85. }