Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Fix permission for checkoutCustomerAttach mutation

Marcin Gębalaa70cd66201e0
diff --git a/saleor/graphql/checkout/mutations.py b/saleor/graphql/checkout/mutations.py
index 21fe03e371..f2c38d737b 100644
--- a/saleor/graphql/checkout/mutations.py
+++ b/saleor/graphql/checkout/mutations.py
@@ -5,6 +5,7 @@ from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.db import transaction
from django.db.models import Prefetch
+from graphql_jwt.exceptions import PermissionDenied
from ...account.error_codes import AccountErrorCode
from ...checkout import models
@@ -404,7 +405,13 @@ class CheckoutCustomerAttach(BaseMutation):
class Arguments:
checkout_id = graphene.ID(required=True, description="ID of the checkout.")
- customer_id = graphene.ID(required=True, description="The ID of the customer.")
+ 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."
+ ),
+ )
class Meta:
description = "Sets the customer as the owner of the checkout."
@@ -412,14 +419,28 @@ class CheckoutCustomerAttach(BaseMutation):
error_type_field = "checkout_errors"
@classmethod
- def perform_mutation(cls, _root, info, checkout_id, customer_id):
+ def check_permissions(cls, context):
+ return context.user.is_authenticated
+
+ @classmethod
+ def perform_mutation(cls, _root, info, **data):
+ checkout_id = data.get("checkout_id")
+ customer_id = data.get("customer_id")
checkout = cls.get_node_or_error(
info, checkout_id, only_type=Checkout, field="checkout_id"
)
- customer = cls.get_node_or_error(
- info, customer_id, only_type=User, field="customer_id"
- )
- checkout.user = customer
+
+ # Check if provided customer_id matches with the authenticated user and raise
+ # 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:
+ raise PermissionDenied()
+
+ checkout.user = info.context.user
checkout.save(update_fields=["user", "last_change"])
return CheckoutCustomerAttach(checkout=checkout)
@@ -435,6 +456,10 @@ class CheckoutCustomerDetach(BaseMutation):
error_type_class = CheckoutError
error_type_field = "checkout_errors"
+ @classmethod
+ def check_permissions(cls, context):
+ return context.user.is_authenticated
+
@classmethod
def perform_mutation(cls, _root, info, checkout_id):
checkout = cls.get_node_or_error(
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index ad376a3485..2a7c0a7a4e 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -2288,7 +2288,7 @@ type Mutation {
checkoutBillingAddressUpdate(billingAddress: AddressInput!, checkoutId: ID!): CheckoutBillingAddressUpdate
checkoutComplete(checkoutId: ID!, redirectUrl: String, storeSource: Boolean = false): CheckoutComplete
checkoutCreate(input: CheckoutCreateInput!): CheckoutCreate
- checkoutCustomerAttach(checkoutId: ID!, customerId: ID!): CheckoutCustomerAttach
+ checkoutCustomerAttach(checkoutId: ID!, customerId: ID): CheckoutCustomerAttach
checkoutCustomerDetach(checkoutId: ID!): CheckoutCustomerDetach
checkoutEmailUpdate(checkoutId: ID, email: String!): CheckoutEmailUpdate
checkoutLineDelete(checkoutId: ID!, lineId: ID): CheckoutLineDelete
diff --git a/tests/api/test_checkout.py b/tests/api/test_checkout.py
index d31704908d..b6ac3c85b7 100644
--- a/tests/api/test_checkout.py
+++ b/tests/api/test_checkout.py
@@ -8,6 +8,7 @@ import pytest
from django.core.exceptions import ValidationError
from prices import Money, TaxedMoney
+from saleor.account.models import User
from saleor.checkout import calculations
from saleor.checkout.error_codes import CheckoutErrorCode
from saleor.checkout.models import Checkout
@@ -25,7 +26,7 @@ from saleor.payment.interface import GatewayResponse
from saleor.shipping import ShippingMethodType
from saleor.shipping.models import ShippingMethod
from saleor.warehouse.models import Stock
-from tests.api.utils import get_graphql_content
+from tests.api.utils import assert_no_permission, get_graphql_content
@pytest.fixture
@@ -851,7 +852,9 @@ def test_checkout_line_delete_by_zero_quantity(
mocked_update_shipping_method.assert_called_once_with(checkout, mock.ANY)
-def test_checkout_customer_attach(user_api_client, checkout_with_item, customer_user):
+def test_checkout_customer_attach(
+ api_client, user_api_client, checkout_with_item, customer_user
+):
checkout = checkout_with_item
assert checkout.user is None
@@ -871,16 +874,26 @@ def test_checkout_customer_attach(user_api_client, checkout_with_item, customer_
"""
checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
customer_id = graphene.Node.to_global_id("User", customer_user.pk)
-
variables = {"checkoutId": checkout_id, "customerId": customer_id}
+
+ # Mutation should fail for unauthenticated customers
+ response = api_client.post_graphql(query, variables)
+ assert_no_permission(response)
+
+ # Mutation should succeed for authenticated customer
response = user_api_client.post_graphql(query, variables)
content = get_graphql_content(response)
-
data = content["data"]["checkoutCustomerAttach"]
assert not data["errors"]
checkout.refresh_from_db()
assert checkout.user == customer_user
+ # Mutation with ID of a different user should fail as well
+ other_customer = User.objects.create_user("[email protected]", "password")
+ variables["customerId"] = graphene.Node.to_global_id("User", other_customer.pk)
+ response = user_api_client.post_graphql(query, variables)
+ assert_no_permission(response)
+
MUTATION_CHECKOUT_CUSTOMER_DETACH = """
mutation checkoutCustomerDetach($checkoutId: ID!) {