bundles/BackofficeBundle/Security/Authorization/Voter/OrderVoter.php line 37

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * OrderVoter.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\Order;
  29. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  30. /**
  31. * Voter for order entity
  32. */
  33. class OrderVoter extends AbstractVoter
  34. {
  35. /**
  36. * @param string $sAttribute attribute
  37. * @param mixed $oSubject subject
  38. * @return bool
  39. */
  40. #[\Override]
  41. public function supports($sAttribute, $oSubject)
  42. {
  43. return $oSubject instanceof Order && parent::supports($sAttribute, $oSubject);
  44. }
  45. /**
  46. * @param string $sAttr attr
  47. * @param mixed $mEntity entity
  48. * @param TokenInterface $oToken token
  49. *
  50. * @return bool
  51. */
  52. protected function voteOnAttribute($sAttr, $mEntity, TokenInterface $oToken)
  53. {
  54. $oUser = $oToken->getUser();
  55. if (!$oUser instanceof Merchant) {
  56. return false;
  57. }
  58. return (bool) $this->findByOwner($oUser->getId(), $mEntity->getId());
  59. }
  60. /**
  61. * Find entity by owner id
  62. *
  63. * @param int $iOwnerId owner id
  64. * @param int $iEntityId entity id
  65. *
  66. * @return mixed
  67. * @throws \Doctrine\ORM\NonUniqueResultException
  68. */
  69. protected function findByOwner($iOwnerId, $iEntityId)
  70. {
  71. $oQuery = $this->oEm->createQuery('
  72. SELECT o FROM ' . Order::class . ' o
  73. WHERE o.oApplication IN (
  74. SELECT a.iId FROM ' . Application::class . ' a
  75. WHERE a.oMerchant = :owner_id
  76. ) AND o.iId = :entity_id
  77. ');
  78. $oQuery->setParameters(['owner_id' => $iOwnerId, 'entity_id' => $iEntityId]);
  79. return $oQuery->getOneOrNullResult();
  80. }
  81. }