src/Security/ECommerce/CartVoter.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Security\ECommerce;
  3. use App\Entity\ECommerce\Cart;
  4. use App\Entity\App\User;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. /**
  10. * Class CartVoter
  11. *
  12. * @package MDL\ECommerceBundle\Security
  13. */
  14. class CartVoter extends Voter
  15. {
  16. // these strings are just invented: you can use anything
  17. const CREATE = 'CART_CREATE';
  18. const VIEW = 'CART_VIEW';
  19. const EDIT = 'CART_EDIT';
  20. const DELETE = 'CART_DELETE';
  21. /**
  22. * @var AccessDecisionManagerInterface
  23. */
  24. private $decisionManager;
  25. /**
  26. * CartVoter constructor.
  27. *
  28. * @param AccessDecisionManagerInterface $decisionManager
  29. */
  30. public function __construct(AccessDecisionManagerInterface $decisionManager)
  31. {
  32. $this->decisionManager = $decisionManager;
  33. }
  34. /**
  35. * @param string $attribute
  36. * @param mixed $subject
  37. *
  38. * @return bool
  39. */
  40. protected function supports($attribute, $subject)
  41. {
  42. // if the attribute isn't one we support, return false
  43. if (!in_array($attribute, array(
  44. self::CREATE,
  45. self::VIEW,
  46. self::EDIT,
  47. self::DELETE,
  48. ))) {
  49. return false;
  50. }
  51. // only vote on Cart objects inside this voter
  52. if (!$subject instanceof Cart && $attribute !== self::CREATE) {
  53. return false;
  54. }
  55. return true;
  56. }
  57. /**
  58. * @param string $attribute
  59. * @param Cart $individualOrder
  60. * @param TokenInterface $token
  61. *
  62. * @return bool
  63. */
  64. protected function voteOnAttribute($attribute, $individualOrder, TokenInterface $token)
  65. {
  66. if ($this->decisionManager->decide($token, array('ROLE_SUPER_ADMIN'))) {
  67. return true;
  68. }
  69. $user = $token->getUser();
  70. if (!$user instanceof UserInterface) {
  71. return false;
  72. }
  73. switch ($attribute) {
  74. case self::CREATE:
  75. return $this->canCreate($user);
  76. case self::VIEW:
  77. return $this->canView($individualOrder, $user);
  78. case self::EDIT:
  79. return $this->canEdit($individualOrder, $user);
  80. case self::DELETE:
  81. return $this->canDelete($individualOrder, $user);
  82. }
  83. throw new \LogicException('This code should not be reached!');
  84. }
  85. /**
  86. * @param User $user
  87. *
  88. * @return bool
  89. */
  90. private function canCreate(User $user)
  91. {
  92. if ($user->hasRole('ROLE_GUEST')) {
  93. return false;
  94. }
  95. return true;
  96. }
  97. /**
  98. * @param Cart $cart
  99. * @param User $user
  100. *
  101. * @return bool
  102. */
  103. private function canView(Cart $cart, User $user)
  104. {
  105. if ($this->canEdit($cart, $user)) {
  106. return true;
  107. }
  108. return false;
  109. }
  110. /**
  111. * @param Cart $cart
  112. * @param UserInterface $user
  113. *
  114. * @return bool
  115. */
  116. private function canEdit(Cart $cart, UserInterface $user)
  117. {
  118. if (!$cart->isCheckedOut()) {
  119. return $user === $cart->getUser();
  120. }
  121. return false;
  122. }
  123. /**
  124. * @param Cart $cart
  125. * @param UserInterface $user
  126. *
  127. * @return bool
  128. */
  129. private function canDelete($cart, $user)
  130. {
  131. return false;
  132. }
  133. }