|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\Quote\Plugin; |
| 10 | + |
| 11 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 12 | +use Magento\Framework\Webapi\Rest\Request as RestRequest; |
| 13 | +use Magento\Quote\Api\CartRepositoryInterface; |
| 14 | +use Magento\Quote\Api\Data\CartInterface; |
| 15 | +use Magento\Sales\Api\OrderRepositoryInterface; |
| 16 | + |
| 17 | +/** |
| 18 | + * Validate order id from request param |
| 19 | + */ |
| 20 | +class ValidateQuoteOrigOrder |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var OrderRepositoryInterface |
| 24 | + */ |
| 25 | + private $orderRepository; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var RestRequest $request |
| 29 | + */ |
| 30 | + private $request; |
| 31 | + |
| 32 | + /** |
| 33 | + * @param RestRequest $request |
| 34 | + * @param OrderRepositoryInterface $orderRepository |
| 35 | + */ |
| 36 | + public function __construct(RestRequest $request, OrderRepositoryInterface $orderRepository) |
| 37 | + { |
| 38 | + $this->request = $request; |
| 39 | + $this->orderRepository = $orderRepository; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Validate the user authorization to order |
| 44 | + * |
| 45 | + * @param CartRepositoryInterface $cartRepository |
| 46 | + * @param CartInterface $quote |
| 47 | + * @return void |
| 48 | + * @throws NoSuchEntityException |
| 49 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 50 | + */ |
| 51 | + public function beforeSave( |
| 52 | + CartRepositoryInterface $cartRepository, |
| 53 | + CartInterface $quote |
| 54 | + ): void { |
| 55 | + $params = $this->request->getBodyParams(); |
| 56 | + if (!empty($params) && isset($params['quote']['orig_order_id'])) { |
| 57 | + $orderId = $params['quote']['orig_order_id']; |
| 58 | + $order = $this->orderRepository->get($orderId); |
| 59 | + $orderCustomer = (int)$order->getCustomerId(); |
| 60 | + if ($quote->getCustomerId() !== $orderCustomer) { |
| 61 | + throw new NoSuchEntityException(__('Please check input parameters.')); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments