Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Restrict detaching customer from checkout
Marcin Gębala2ec86986f4c7
diff --git a/saleor/graphql/checkout/mutations.py b/saleor/graphql/checkout/mutations.pyindex f2c38d737b..bebda3f437 100644--- a/saleor/graphql/checkout/mutations.py+++ b/saleor/graphql/checkout/mutations.py@@ -36,7 +36,7 @@ from ...payment.utils import store_customer_idfrom ...product import models as product_modelsfrom ...warehouse.availability import check_stock_quantity, get_available_quantityfrom ..account.i18n import I18nMixin-from ..account.types import AddressInput, User+from ..account.types import AddressInputfrom ..core.mutations import (BaseMutation,ClearMetaBaseMutation,@@ -408,8 +408,9 @@ class CheckoutCustomerAttach(BaseMutation):customer_id = graphene.ID(required=False,description=(- "The ID of the customer. DEPRECATED: This field is deprecated. "- "To identify a customer you should authenticate with JWT token."+ "The ID of the customer. DEPRECATED: This field is deprecated and will "+ "be removed in Saleor 2.11. To identify a customer you should "+ "authenticate with JWT token."),)@@ -434,10 +435,8 @@ class CheckoutCustomerAttach(BaseMutation):# error if it doesn't. This part can be removed when `customer_id` field is# removed.if customer_id:- customer = cls.get_node_or_error(- info, customer_id, only_type=User, field="customer_id"- )- if customer and customer != info.context.user:+ current_user_id = graphene.Node.to_global_id("User", info.context.user.id)+ if current_user_id != customer_id:raise PermissionDenied()checkout.user = info.context.user@@ -461,10 +460,16 @@ class CheckoutCustomerDetach(BaseMutation):return context.user.is_authenticated@classmethod- def perform_mutation(cls, _root, info, checkout_id):+ def perform_mutation(cls, _root, info, **data):+ checkout_id = data.get("checkout_id")checkout = cls.get_node_or_error(info, checkout_id, only_type=Checkout, field="checkout_id")++ # Raise error if the current user doesn't own the checkout of the given ID.+ if checkout.user and checkout.user != info.context.user:+ raise PermissionDenied()+checkout.user = Nonecheckout.save(update_fields=["user", "last_change"])return CheckoutCustomerDetach(checkout=checkout)diff --git a/tests/api/test_checkout.py b/tests/api/test_checkout.pyindex b6ac3c85b7..1a0c842459 100644--- a/tests/api/test_checkout.py+++ b/tests/api/test_checkout.py@@ -917,16 +917,26 @@ def test_checkout_customer_detach(user_api_client, checkout_with_item, customer_checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)variables = {"checkoutId": checkout_id}++ # Mutation should succeed if the user owns this checkout.response = user_api_client.post_graphql(MUTATION_CHECKOUT_CUSTOMER_DETACH, variables)content = get_graphql_content(response)-data = content["data"]["checkoutCustomerDetach"]assert not data["errors"]checkout.refresh_from_db()assert checkout.user is None+ # Mutation should fail when user calling it doesn't own the checkout.+ other_user = User.objects.create_user("[email protected]", "password")+ checkout.user = other_user+ checkout.save()+ response = user_api_client.post_graphql(+ MUTATION_CHECKOUT_CUSTOMER_DETACH, variables+ )+ assert_no_permission(response)+MUTATION_CHECKOUT_SHIPPING_ADDRESS_UPDATE = """mutation checkoutShippingAddressUpdate(