Skip to content

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

Require manage_order when fetching order with externalReference (#16184)

Maciej Korycinski888aee97aed9
diff --git a/saleor/graphql/order/schema.py b/saleor/graphql/order/schema.py
index f697531c33..7de15b4357 100644
--- a/saleor/graphql/order/schema.py
+++ b/saleor/graphql/order/schema.py
@@ -4,8 +4,10 @@ import graphene
from django.core.exceptions import ValidationError
from graphql import GraphQLError
+from ...core.exceptions import PermissionDenied
from ...order import models
from ...permission.enums import OrderPermissions
+from ...permission.utils import has_one_of_permissions
from ..core import ResolveInfo
from ..core.connection import create_connection_slice, filter_connection_queryset
from ..core.context import get_database_connection_name
@@ -22,6 +24,7 @@ from ..core.scalars import UUID
from ..core.types import FilterInputObjectType, TaxedMoney
from ..core.utils import ext_ref_to_global_id_or_error, from_global_id_or_error
from ..core.validators import validate_one_of_args_is_in_query
+from ..utils import get_user_or_app_from_context
from .bulk_mutations.draft_orders import DraftOrderBulkDelete, DraftOrderLinesBulkDelete
from .bulk_mutations.order_bulk_cancel import OrderBulkCancel
from .bulk_mutations.order_bulk_create import OrderBulkCreate
@@ -106,7 +109,11 @@ class OrderQueries(graphene.ObjectType):
description="Look up an order by ID or external reference.",
id=graphene.Argument(graphene.ID, description="ID of an order."),
external_reference=graphene.Argument(
- graphene.String, description=f"External ID of an order. {ADDED_IN_310}"
+ graphene.String,
+ description=(
+ f"External ID of an order. {ADDED_IN_310}."
+ "\n\nRequires one of the following permissions: MANAGE_ORDERS."
+ ),
),
doc_category=DOC_CATEGORY_ORDERS,
)
@@ -167,6 +174,10 @@ class OrderQueries(graphene.ObjectType):
)
database_connection_name = get_database_connection_name(info.context)
if not id:
+ requester = get_user_or_app_from_context(info.context)
+ permissions = [OrderPermissions.MANAGE_ORDERS]
+ if not has_one_of_permissions(requester, permissions):
+ raise PermissionDenied(permissions=permissions)
try:
id = ext_ref_to_global_id_or_error(
models.Order, external_reference, database_connection_name
diff --git a/saleor/graphql/order/tests/queries/test_order.py b/saleor/graphql/order/tests/queries/test_order.py
index 38e81ca50f..a026a280c2 100644
--- a/saleor/graphql/order/tests/queries/test_order.py
+++ b/saleor/graphql/order/tests/queries/test_order.py
@@ -1147,7 +1147,7 @@ QUERY_ORDER_BY_EXTERNAL_REFERENCE = """
"""
-def test_query_order_by_external_reference(user_api_client, order):
+def test_query_order_by_external_reference_missing_permission(user_api_client, order):
# given
query = QUERY_ORDER_BY_EXTERNAL_REFERENCE
ext_ref = "test-ext-ref"
@@ -1157,6 +1157,29 @@ def test_query_order_by_external_reference(user_api_client, order):
# when
response = user_api_client.post_graphql(query, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_order_by_external_reference(
+ staff_api_client, order, permission_manage_orders
+):
+ # given
+ query = QUERY_ORDER_BY_EXTERNAL_REFERENCE
+ ext_ref = "test-ext-ref"
+ order.external_reference = ext_ref
+ order.save(update_fields=["external_reference"])
+ variables = {"externalReference": ext_ref}
+
+ # when
+ response = staff_api_client.post_graphql(
+ query,
+ variables,
+ permissions=[
+ permission_manage_orders,
+ ],
+ )
content = get_graphql_content(response)
# then
@@ -1168,7 +1191,7 @@ def test_query_order_by_external_reference(user_api_client, order):
@pytest.mark.parametrize("external_reference", ['" "', "not-existing"])
def test_query_order_by_not_existing_external_reference(
- external_reference, user_api_client, order
+ external_reference, staff_api_client, order, permission_manage_orders
):
# given
query = QUERY_ORDER_BY_EXTERNAL_REFERENCE
@@ -1177,7 +1200,13 @@ def test_query_order_by_not_existing_external_reference(
variables = {"externalReference": external_reference}
# when
- response = user_api_client.post_graphql(query, variables)
+ response = staff_api_client.post_graphql(
+ query,
+ variables,
+ permissions=[
+ permission_manage_orders,
+ ],
+ )
content = get_graphql_content(response)
# then
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 8a5dc7a02e..b48efa6efc 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -790,7 +790,9 @@ type Query {
"""
External ID of an order.
- Added in Saleor 3.10.
+ Added in Saleor 3.10..
+
+ Requires one of the following permissions: MANAGE_ORDERS.
"""
externalReference: String
): Order @doc(category: "Orders")