Authorization.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\ResourceModel\Order\Plugin;
  7. use Magento\Authorization\Model\UserContextInterface;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. use Magento\Sales\Model\Order;
  10. use Magento\Sales\Model\ResourceModel\Order as ResourceOrder;
  11. class Authorization
  12. {
  13. /**
  14. * @var UserContextInterface
  15. */
  16. protected $userContext;
  17. /**
  18. * @param UserContextInterface $userContext
  19. */
  20. public function __construct(
  21. UserContextInterface $userContext
  22. ) {
  23. $this->userContext = $userContext;
  24. }
  25. /**
  26. * @param ResourceOrder $subject
  27. * @param ResourceOrder $result
  28. * @param \Magento\Framework\Model\AbstractModel $order
  29. * @return ResourceOrder
  30. * @throws NoSuchEntityException
  31. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  32. */
  33. public function afterLoad(
  34. ResourceOrder $subject,
  35. ResourceOrder $result,
  36. \Magento\Framework\Model\AbstractModel $order
  37. ) {
  38. if ($order instanceof Order) {
  39. if (!$this->isAllowed($order)) {
  40. throw NoSuchEntityException::singleField('orderId', $order->getId());
  41. }
  42. }
  43. return $result;
  44. }
  45. /**
  46. * Checks if order is allowed for current customer
  47. *
  48. * @param \Magento\Sales\Model\Order $order
  49. * @return bool
  50. */
  51. protected function isAllowed(Order $order)
  52. {
  53. return $this->userContext->getUserType() == UserContextInterface::USER_TYPE_CUSTOMER
  54. ? $order->getCustomerId() == $this->userContext->getUserId()
  55. : true;
  56. }
  57. }