Authorization.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Quote\Model\QuoteRepository\Plugin;
  8. use Magento\Authorization\Model\UserContextInterface;
  9. use Magento\Framework\Exception\NoSuchEntityException;
  10. class Authorization
  11. {
  12. /**
  13. * @var \Magento\Authorization\Model\UserContextInterface
  14. */
  15. protected $userContext;
  16. /**
  17. * @param \Magento\Authorization\Model\UserContextInterface $userContext
  18. */
  19. public function __construct(
  20. \Magento\Authorization\Model\UserContextInterface $userContext
  21. ) {
  22. $this->userContext = $userContext;
  23. }
  24. /**
  25. * Check if quote is allowed
  26. *
  27. * @param \Magento\Quote\Api\CartRepositoryInterface $subject
  28. * @param \Magento\Quote\Model\Quote $quote
  29. * @return \Magento\Quote\Model\Quote
  30. * @throws \Magento\Framework\Exception\NoSuchEntityException
  31. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  32. */
  33. public function afterGetActive(
  34. \Magento\Quote\Api\CartRepositoryInterface $subject,
  35. \Magento\Quote\Model\Quote $quote
  36. ) {
  37. if (!$this->isAllowed($quote)) {
  38. throw NoSuchEntityException::singleField('cartId', $quote->getId());
  39. }
  40. return $quote;
  41. }
  42. /**
  43. * Check if quote is allowed
  44. *
  45. * @param \Magento\Quote\Api\CartRepositoryInterface $subject
  46. * @param \Magento\Quote\Model\Quote $quote
  47. * @return \Magento\Quote\Model\Quote
  48. * @throws \Magento\Framework\Exception\NoSuchEntityException
  49. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  50. */
  51. public function afterGetActiveForCustomer(
  52. \Magento\Quote\Api\CartRepositoryInterface $subject,
  53. \Magento\Quote\Model\Quote $quote
  54. ) {
  55. if (!$this->isAllowed($quote)) {
  56. throw NoSuchEntityException::singleField('cartId', $quote->getId());
  57. }
  58. return $quote;
  59. }
  60. /**
  61. * Check whether quote is allowed for current user context
  62. *
  63. * @param \Magento\Quote\Model\Quote $quote
  64. * @return bool
  65. */
  66. protected function isAllowed(\Magento\Quote\Model\Quote $quote)
  67. {
  68. return $this->userContext->getUserType() == UserContextInterface::USER_TYPE_CUSTOMER
  69. ? $quote->getCustomerId() === null || $quote->getCustomerId() == $this->userContext->getUserId()
  70. : true;
  71. }
  72. }