10
10
use Magento \Framework \Exception \LocalizedException ;
11
11
use Magento \Framework \Exception \NoSuchEntityException ;
12
12
use Magento \Framework \GraphQl \Config \Element \Field ;
13
+ use Magento \Framework \GraphQl \Exception \GraphQlAuthorizationException ;
13
14
use Magento \Framework \GraphQl \Exception \GraphQlNoSuchEntityException ;
14
15
use Magento \Framework \GraphQl \Query \ResolverInterface ;
15
16
use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
16
17
use Magento \Quote \Api \CartRepositoryInterface ;
17
18
use Magento \Quote \Model \MaskedQuoteIdToQuoteId ;
19
+ use Magento \QuoteGraphQl \Model \Authorization \IsCartMutationAllowedForCurrentUser ;
18
20
use Magento \QuoteGraphQl \Model \Resolver \Address \AddressDataProvider ;
19
21
20
22
/**
@@ -27,6 +29,11 @@ class CartAddress implements ResolverInterface
27
29
*/
28
30
private $ addressDataProvider ;
29
31
32
+ /**
33
+ * @var IsCartMutationAllowedForCurrentUser
34
+ */
35
+ private $ isCartMutationAllowedForCurrentUser ;
36
+
30
37
/**
31
38
* @var CartRepositoryInterface
32
39
*/
@@ -43,36 +50,49 @@ class CartAddress implements ResolverInterface
43
50
* @param MaskedQuoteIdToQuoteId $maskedQuoteIdToQuoteId
44
51
* @param CartRepositoryInterface $cartRepository
45
52
* @param AddressDataProvider $addressDataProvider
53
+ * @param IsCartMutationAllowedForCurrentUser $isCartMutationAllowedForCurrentUser
46
54
*/
47
55
public function __construct (
48
56
MaskedQuoteIdToQuoteId $ maskedQuoteIdToQuoteId ,
49
57
CartRepositoryInterface $ cartRepository ,
50
- AddressDataProvider $ addressDataProvider
58
+ AddressDataProvider $ addressDataProvider ,
59
+ IsCartMutationAllowedForCurrentUser $ isCartMutationAllowedForCurrentUser
51
60
) {
52
61
$ this ->maskedQuoteIdToQuoteId = $ maskedQuoteIdToQuoteId ;
53
62
$ this ->cartRepository = $ cartRepository ;
54
63
$ this ->addressDataProvider = $ addressDataProvider ;
64
+ $ this ->isCartMutationAllowedForCurrentUser = $ isCartMutationAllowedForCurrentUser ;
55
65
}
56
66
57
67
/**
58
68
* @inheritdoc
59
69
*/
60
70
public function resolve (Field $ field , $ context , ResolveInfo $ info , array $ value = null , array $ args = null )
61
71
{
72
+ /* The cart_id is used instead of the model because some parent resolvers do not work
73
+ with cart model */
62
74
if (!isset ($ value ['cart_id ' ])) {
63
- // TODO: consider the possibility to pass quote model instead od quote ID
64
75
throw new LocalizedException (__ ('"cart_id" value should be specified ' ));
65
76
}
66
77
78
+ $ maskedCartId = $ value ['cart_id ' ];
79
+
67
80
try {
68
- $ quoteId = $ this ->maskedQuoteIdToQuoteId ->execute ($ value [ ' cart_id ' ] );
81
+ $ quoteId = $ this ->maskedQuoteIdToQuoteId ->execute ($ maskedCartId );
69
82
} catch (NoSuchEntityException $ exception ) {
70
83
throw new GraphQlNoSuchEntityException (
71
- __ ('Could not find a cart with ID "%masked_cart_id" ' , ['masked_cart_id ' => $ value [ ' cart_id ' ] ])
84
+ __ ('Could not find a cart with ID "%masked_cart_id" ' , ['masked_cart_id ' => $ maskedCartId ])
72
85
);
73
86
}
74
87
75
- // TODO: should we check customer permissions here as well?
88
+ if (false === $ this ->isCartMutationAllowedForCurrentUser ->execute ($ quoteId )) {
89
+ throw new GraphQlAuthorizationException (
90
+ __ (
91
+ 'The current user cannot perform operations on cart "%masked_cart_id" ' ,
92
+ ['masked_cart_id ' => $ maskedCartId ]
93
+ )
94
+ );
95
+ }
76
96
77
97
try {
78
98
$ quote = $ this ->cartRepository ->get ($ quoteId );
0 commit comments