Skip to content

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

Allow to use checkoutCustomerAttach.customerID by customer (#10793)

Maciej Korycinski3513064157fa
diff --git a/saleor/graphql/checkout/mutations/checkout_customer_attach.py b/saleor/graphql/checkout/mutations/checkout_customer_attach.py
index 0ccbd54ad8..e00defb961 100644
--- a/saleor/graphql/checkout/mutations/checkout_customer_attach.py
+++ b/saleor/graphql/checkout/mutations/checkout_customer_attach.py
@@ -35,8 +35,9 @@ class CheckoutCustomerAttach(BaseMutation):
customer_id = graphene.ID(
required=False,
description=(
- "ID of customer to attach to checkout. Can be used to attach customer "
- "to checkout by staff or app. Requires IMPERSONATE_USER permission."
+ "ID of customer to attach to checkout. "
+ "Requires IMPERSONATE_USER permission when customerId is different "
+ "than the logged-in user."
),
)
@@ -72,7 +73,13 @@ class CheckoutCustomerAttach(BaseMutation):
)
)
- if customer_id:
+ user_id_from_request = None
+ if not info.context.user.is_anonymous:
+ user_id_from_request = graphene.Node.to_global_id(
+ "User", info.context.user.id
+ )
+
+ if customer_id and customer_id != user_id_from_request:
requestor = get_user_or_app_from_context(info.context)
if not requestor.has_perm(AccountPermissions.IMPERSONATE_USER):
raise PermissionDenied(
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_customer_attach.py b/saleor/graphql/checkout/tests/mutations/test_checkout_customer_attach.py
index 1919809b5b..a984784314 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_customer_attach.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_customer_attach.py
@@ -22,7 +22,7 @@ MUTATION_CHECKOUT_CUSTOMER_ATTACH = """
def test_checkout_customer_attach(
- user_api_client, checkout_with_item, customer_user, permission_impersonate_user
+ user_api_client, checkout_with_item, customer_user2, permission_impersonate_user
):
checkout = checkout_with_item
checkout.email = "[email protected]"
@@ -31,7 +31,7 @@ def test_checkout_customer_attach(
previous_last_change = checkout.last_change
query = MUTATION_CHECKOUT_CUSTOMER_ATTACH
- customer_id = graphene.Node.to_global_id("User", customer_user.pk)
+ customer_id = graphene.Node.to_global_id("User", customer_user2.pk)
variables = {"id": to_global_id_or_none(checkout), "customerId": customer_id}
response = user_api_client.post_graphql(
@@ -39,6 +39,33 @@ def test_checkout_customer_attach(
)
content = get_graphql_content(response)
+ data = content["data"]["checkoutCustomerAttach"]
+ assert not data["errors"]
+ checkout.refresh_from_db()
+ assert checkout.user == customer_user2
+ assert checkout.email == customer_user2.email
+ assert checkout.last_change != previous_last_change
+
+
+def test_checkout_customer_attach_with_customer_id_same_as_in_request(
+ user_api_client, checkout_with_item, customer_user, permission_impersonate_user
+):
+ checkout = checkout_with_item
+ checkout.email = "[email protected]"
+ checkout.save()
+ assert checkout.user is None
+ previous_last_change = checkout.last_change
+
+ query = MUTATION_CHECKOUT_CUSTOMER_ATTACH
+ customer_id = graphene.Node.to_global_id("User", customer_user.pk)
+ variables = {"id": to_global_id_or_none(checkout), "customerId": customer_id}
+
+ response = user_api_client.post_graphql(
+ query,
+ variables,
+ )
+ content = get_graphql_content(response, ignore_errors=True)
+
data = content["data"]["checkoutCustomerAttach"]
assert not data["errors"]
checkout.refresh_from_db()
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 086b94e6f5..1df6b74857 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -13322,7 +13322,7 @@ type Mutation {
checkoutId: ID
"""
- ID of customer to attach to checkout. Can be used to attach customer to checkout by staff or app. Requires IMPERSONATE_USER permission.
+ ID of customer to attach to checkout. Requires IMPERSONATE_USER permission when customerId is different than the logged-in user.
"""
customerId: ID