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

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * ContractVoter.php File.
  5. * This file is part of the Payment.net Project.
  6. *
  7. * PHP version 5
  8. *
  9. * @category Contract
  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. * Class specifies rules to access Contract entities
  17. *
  18. * TODO-LIST :
  19. * ===========
  20. * Is granted should be changed when multiple contracts will come foe merchant
  21. *
  22. * HISTORY :
  23. * =========
  24. * 20150821 - Helen Tochko: class creation
  25. **/
  26. namespace Bdm\BackofficeBundle\Security\Authorization\Voter;
  27. use Bdm\AdminBundle\Entity\User;
  28. use Bdm\BackofficeBundle\Entity\Contract;
  29. use Bdm\BackofficeBundle\Entity\Merchant;
  30. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  31. /**
  32. * Voter for contract entity
  33. */
  34. class ContractVoter 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 Contract && 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 (true === $oUser instanceof User
  57. && true === $oUser->hasRole(User::ROLE_SUPER_ADMIN)
  58. && true === in_array($sAttr, ['VIEW', 'EDIT'])
  59. ) {
  60. return true;
  61. }
  62. if (true === $oUser instanceof Merchant
  63. && $sAttr === 'VIEW'
  64. && $oUser->getContracts()->contains($mEntity)) {
  65. return true;
  66. }
  67. return false;
  68. }
  69. }