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