src/Security/ECommerce/InvoiceVoter.php line 16

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