Skip to content

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

Fix permission check for attributeUpdate and attributeDelete mutations (#19527)

Marcin Wcisło44c7495ca9bf
diff --git a/saleor/graphql/attribute/mutations/attribute_delete.py b/saleor/graphql/attribute/mutations/attribute_delete.py
index 1f9af40baa..b46c1d3cc1 100644
--- a/saleor/graphql/attribute/mutations/attribute_delete.py
+++ b/saleor/graphql/attribute/mutations/attribute_delete.py
@@ -2,11 +2,13 @@ import graphene
from django.db import transaction
from django.db.models import Exists, OuterRef
+from ....attribute import AttributeType
from ....attribute import models as models
from ....attribute.lock_objects import attribute_value_qs_select_for_update
+from ....core.exceptions import PermissionDenied
from ....page import models as page_models
from ....page.utils import mark_pages_search_vector_as_dirty_in_batches
-from ....permission.enums import ProductTypePermissions
+from ....permission.enums import PageTypePermissions, ProductTypePermissions
from ....product import models as product_models
from ....product.utils.search_helpers import (
mark_products_search_vector_as_dirty_in_batches,
@@ -32,8 +34,12 @@ class AttributeDelete(ModelDeleteMutation, ModelWithExtRefMutation):
class Meta:
model = models.Attribute
object_type = Attribute
- description = "Deletes an attribute."
- permissions = (ProductTypePermissions.MANAGE_PRODUCT_TYPES_AND_ATTRIBUTES,)
+ description = (
+ "Deletes an attribute.\n\nRequires one of the following permissions, "
+ "depending on the attribute type: "
+ "MANAGE_PRODUCT_TYPES_AND_ATTRIBUTES for `PRODUCT_TYPE` attributes, "
+ "MANAGE_PAGE_TYPES_AND_ATTRIBUTES for `PAGE_TYPE` attributes."
+ )
error_type_class = AttributeError
error_type_field = "attribute_errors"
webhook_events_info = [
@@ -59,7 +65,25 @@ class AttributeDelete(ModelDeleteMutation, ModelWithExtRefMutation):
cls, _root, info: ResolveInfo, /, *, external_reference=None, id=None
):
"""Perform a mutation that deletes a model instance."""
+ # Concrete permission is checked after instance is resolved.
+ type_permissions = (
+ ProductTypePermissions.MANAGE_PRODUCT_TYPES_AND_ATTRIBUTES,
+ PageTypePermissions.MANAGE_PAGE_TYPES_AND_ATTRIBUTES,
+ )
+ if not cls.check_permissions(info.context, type_permissions):
+ raise PermissionDenied(permissions=type_permissions)
+
instance = cls.get_instance(info, external_reference=external_reference, id=id)
+
+ # Check permissions based on attribute type
+ permissions: tuple[ProductTypePermissions] | tuple[PageTypePermissions]
+ if instance.type == AttributeType.PRODUCT_TYPE:
+ permissions = (ProductTypePermissions.MANAGE_PRODUCT_TYPES_AND_ATTRIBUTES,)
+ else:
+ permissions = (PageTypePermissions.MANAGE_PAGE_TYPES_AND_ATTRIBUTES,)
+ if not cls.check_permissions(info.context, permissions):
+ raise PermissionDenied(permissions=permissions)
+
product_ids = cls.get_product_ids_to_search_index_update(instance)
page_ids = cls.get_page_ids_to_search_index_update(instance)
diff --git a/saleor/graphql/attribute/mutations/attribute_update.py b/saleor/graphql/attribute/mutations/attribute_update.py
index cf872d5b71..bcd3015f53 100644
--- a/saleor/graphql/attribute/mutations/attribute_update.py
+++ b/saleor/graphql/attribute/mutations/attribute_update.py
@@ -1,10 +1,12 @@
import graphene
from django.core.exceptions import ValidationError
+from ....attribute import AttributeType
from ....attribute import models as models
from ....attribute.error_codes import AttributeErrorCode
+from ....core.exceptions import PermissionDenied
from ....page.utils import mark_pages_search_vector_as_dirty_in_batches
-from ....permission.enums import ProductTypePermissions
+from ....permission.enums import PageTypePermissions, ProductTypePermissions
from ....product.utils.search_helpers import (
mark_products_search_vector_as_dirty_in_batches,
)
@@ -121,8 +123,12 @@ class AttributeUpdate(AttributeMixin, ModelWithExtRefMutation):
class Meta:
model = models.Attribute
object_type = Attribute
- description = "Updates attribute."
- permissions = (ProductTypePermissions.MANAGE_PRODUCT_TYPES_AND_ATTRIBUTES,)
+ description = (
+ "Updates attribute.\n\nRequires one of the following permissions, "
+ "depending on the attribute type: "
+ "MANAGE_PRODUCT_TYPES_AND_ATTRIBUTES for `PRODUCT_TYPE` attributes, "
+ "MANAGE_PAGE_TYPES_AND_ATTRIBUTES for `PAGE_TYPE` attributes."
+ )
error_type_class = AttributeError
error_type_field = "attribute_errors"
webhook_events_info = [
@@ -158,8 +164,25 @@ class AttributeUpdate(AttributeMixin, ModelWithExtRefMutation):
def perform_mutation( # type: ignore[override]
cls, _root, info: ResolveInfo, /, *, external_reference=None, id=None, input
):
+ # Concrete permission is checked after instance is resolved.
+ type_permissions = (
+ ProductTypePermissions.MANAGE_PRODUCT_TYPES_AND_ATTRIBUTES,
+ PageTypePermissions.MANAGE_PAGE_TYPES_AND_ATTRIBUTES,
+ )
+ if not cls.check_permissions(info.context, type_permissions):
+ raise PermissionDenied(permissions=type_permissions)
+
instance = cls.get_instance(info, external_reference=external_reference, id=id)
+ # Check permissions based on attribute type
+ permissions: tuple[ProductTypePermissions] | tuple[PageTypePermissions]
+ if instance.type == AttributeType.PRODUCT_TYPE:
+ permissions = (ProductTypePermissions.MANAGE_PRODUCT_TYPES_AND_ATTRIBUTES,)
+ else:
+ permissions = (PageTypePermissions.MANAGE_PAGE_TYPES_AND_ATTRIBUTES,)
+ if not cls.check_permissions(info.context, permissions):
+ raise PermissionDenied(permissions=permissions)
+
cls.validate_reference_types_limit(input)
# Do cleaning and uniqueness checks
cleaned_input = cls.clean_input(info, instance, input)
diff --git a/saleor/graphql/attribute/tests/mutations/test_attribute_delete.py b/saleor/graphql/attribute/tests/mutations/test_attribute_delete.py
index acb250feff..3a10d5734f 100644
--- a/saleor/graphql/attribute/tests/mutations/test_attribute_delete.py
+++ b/saleor/graphql/attribute/tests/mutations/test_attribute_delete.py
@@ -12,7 +12,7 @@ from .....attribute.tests.model_helpers import (
from .....core.utils.json_serializer import CustomJsonEncoder
from .....webhook.event_types import WebhookEventAsyncType
from .....webhook.payloads import generate_meta, generate_requestor
-from ....tests.utils import get_graphql_content
+from ....tests.utils import assert_no_permission, get_graphql_content
ATTRIBUTE_DELETE_MUTATION = """
mutation deleteAttribute($id: ID!) {
@@ -159,7 +159,7 @@ def test_delete_attribute_update_search_index_dirty_in_page(
staff_api_client,
page,
page_type,
- permission_manage_product_types_and_attributes,
+ permission_manage_page_types_and_attributes,
):
# given
attribute = page_type.page_attributes.first()
@@ -170,7 +170,7 @@ def test_delete_attribute_update_search_index_dirty_in_page(
staff_api_client.post_graphql(
ATTRIBUTE_DELETE_MUTATION,
variables,
- permissions=[permission_manage_product_types_and_attributes],
+ permissions=[permission_manage_page_types_and_attributes],
)
# then
@@ -261,3 +261,72 @@ def test_delete_attribute_by_external_reference_not_existing(
# then
errors = content["data"]["attributeDelete"]["errors"]
assert errors[0]["message"] == f"Couldn't resolve to a node: {ext_ref}"
+
+
+def test_delete_page_attribute_with_page_type_permission(
+ staff_api_client,
+ size_page_attribute,
+ permission_manage_page_types_and_attributes,
+):
+ # given
+ attribute = size_page_attribute
+ node_id = graphene.Node.to_global_id("Attribute", attribute.id)
+ variables = {"id": node_id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ ATTRIBUTE_DELETE_MUTATION,
+ variables,
+ permissions=[permission_manage_page_types_and_attributes],
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["attributeDelete"]
+ assert data["attribute"]["id"] == variables["id"]
+ with pytest.raises(attribute._meta.model.DoesNotExist):
+ attribute.refresh_from_db()
+
+
+def test_delete_page_attribute_without_page_type_permission(
+ staff_api_client,
+ size_page_attribute,
+ permission_manage_product_types_and_attributes,
+):
+ # given
+ attribute = size_page_attribute
+ node_id = graphene.Node.to_global_id("Attribute", attribute.id)
+ variables = {"id": node_id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ ATTRIBUTE_DELETE_MUTATION,
+ variables,
+ permissions=[permission_manage_product_types_and_attributes],
+ )
+
+ # then
+ assert_no_permission(response)
+ assert attribute._meta.model.objects.filter(pk=attribute.pk).exists()
+
+
+def test_delete_product_attribute_without_product_type_permission(
+ staff_api_client,
+ color_attribute,
+ permission_manage_page_types_and_attributes,
+):
+ # given
+ attribute = color_attribute
+ node_id = graphene.Node.to_global_id("Attribute", attribute.id)
+ variables = {"id": node_id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ ATTRIBUTE_DELETE_MUTATION,
+ variables,
+ permissions=[permission_manage_page_types_and_attributes],
+ )
+
+ # then
+ assert_no_permission(response)
+ assert attribute._meta.model.objects.filter(pk=attribute.pk).exists()
diff --git a/saleor/graphql/attribute/tests/mutations/test_attribute_update.py b/saleor/graphql/attribute/tests/mutations/test_attribute_update.py
index 7d687983b6..6c44bd7881 100644
--- a/saleor/graphql/attribute/tests/mutations/test_attribute_update.py
+++ b/saleor/graphql/attribute/tests/mutations/test_attribute_update.py
@@ -17,7 +17,7 @@ from .....core.utils.json_serializer import CustomJsonEncoder
from .....webhook.event_types import WebhookEventAsyncType
from .....webhook.payloads import generate_meta, generate_requestor
from ....core.enums import MeasurementUnitsEnum
-from ....tests.utils import get_graphql_content
+from ....tests.utils import assert_no_permission, get_graphql_content
UPDATE_ATTRIBUTE_MUTATION = """
mutation updateAttribute(
@@ -1214,7 +1214,7 @@ def test_update_attribute_add_value_does_not_mark_product_search_index_dirty(
def test_update_attribute_remove_value_marks_page_search_index_dirty(
- staff_api_client, page, permission_manage_product_types_and_attributes
+ staff_api_client, page, permission_manage_page_types_and_attributes
):
# given
value = page.attributevalues.first().value
@@ -1236,7 +1236,7 @@ def test_update_attribute_remove_value_marks_page_search_index_dirty(
response = staff_api_client.post_graphql(
UPDATE_ATTRIBUTE_MUTATION,
variables,
- permissions=[permission_manage_product_types_and_attributes],
+ permissions=[permission_manage_page_types_and_attributes],
)
# then
@@ -1249,7 +1249,7 @@ def test_update_attribute_remove_value_marks_page_search_index_dirty(
def test_update_attribute_add_value_does_not_mark_page_search_index_dirty(
- staff_api_client, page, permission_manage_product_types_and_attributes
+ staff_api_client, page, permission_manage_page_types_and_attributes
):
# given
attribute = page.attributevalues.first().value.attribute
@@ -1268,7 +1268,7 @@ def test_update_attribute_add_value_does_not_mark_page_search_index_dirty(
response = staff_api_client.post_graphql(
UPDATE_ATTRIBUTE_MUTATION,
variables,
- permissions=[permission_manage_product_types_and_attributes],
+ permissions=[permission_manage_page_types_and_attributes],
)
# then
@@ -1278,3 +1278,70 @@ def test_update_attribute_add_value_does_not_mark_page_search_index_dirty(
page.refresh_from_db()
assert page.search_index_dirty is False
+
+
+def test_update_page_attribute_with_page_type_permission(
+ staff_api_client, size_page_attribute, permission_manage_page_types_and_attributes
+):
+ # given
+ attribute = size_page_attribute
+ name = "Updated page attribute name"
+ node_id = graphene.Node.to_global_id("Attribute", attribute.id)
+ variables = {"id": node_id, "input": {"name": name}}
+
+ # when
+ response = staff_api_client.post_graphql(
+ UPDATE_ATTRIBUTE_MUTATION,
+ variables,
+ permissions=[permission_manage_page_types_and_attributes],
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["attributeUpdate"]
+ assert not data["errors"]
+ attribute.refresh_from_db()
+ assert data["attribute"]["name"] == name
+ assert attribute.name == name
+
+
+def test_update_page_attribute_without_page_type_permission(
+ staff_api_client,
+ size_page_attribute,
+ permission_manage_product_types_and_attributes,
+):
+ # given
+ attribute = size_page_attribute
+ name = "Updated page attribute name"
+ node_id = graphene.Node.to_global_id("Attribute", attribute.id)
+ variables = {"id": node_id, "input": {"name": name}}
+
+ # when
+ response = staff_api_client.post_graphql(
+ UPDATE_ATTRIBUTE_MUTATION,
+ variables,
+ permissions=[permission_manage_product_types_and_attributes],
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_update_product_attribute_without_product_type_permission(
+ staff_api_client, color_attribute, permission_manage_page_types_and_attributes
+):
+ # given
+ attribute = color_attribute
+ name = "Updated product attribute name"
+ node_id = graphene.Node.to_global_id("Attribute", attribute.id)
+ variables = {"id": node_id, "input": {"name": name}}
+
+ # when
+ response = staff_api_client.post_graphql(
+ UPDATE_ATTRIBUTE_MUTATION,
+ variables,
+ permissions=[permission_manage_page_types_and_attributes],
+ )
+
+ # then
+ assert_no_permission(response)
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index e22d64b260..13fab7ac7f 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -21262,9 +21262,9 @@ type Mutation {
): AttributeCreate @doc(category: "Attributes") @webhookEventsInfo(asyncEvents: [ATTRIBUTE_CREATED], syncEvents: [])
"""
- Deletes an attribute.
+ Deletes an attribute.
- Requires one of the following permissions: MANAGE_PRODUCT_TYPES_AND_ATTRIBUTES.
+ Requires one of the following permissions, depending on the attribute type: MANAGE_PRODUCT_TYPES_AND_ATTRIBUTES for `PRODUCT_TYPE` attributes, MANAGE_PAGE_TYPES_AND_ATTRIBUTES for `PAGE_TYPE` attributes.
Triggers the following webhook events:
- ATTRIBUTE_DELETED (async): An attribute was deleted.
@@ -21278,9 +21278,9 @@ type Mutation {
): AttributeDelete @doc(category: "Attributes") @webhookEventsInfo(asyncEvents: [ATTRIBUTE_DELETED], syncEvents: [])
"""
- Updates attribute.
+ Updates attribute.
- Requires one of the following permissions: MANAGE_PRODUCT_TYPES_AND_ATTRIBUTES.
+ Requires one of the following permissions, depending on the attribute type: MANAGE_PRODUCT_TYPES_AND_ATTRIBUTES for `PRODUCT_TYPE` attributes, MANAGE_PAGE_TYPES_AND_ATTRIBUTES for `PAGE_TYPE` attributes.
Triggers the following webhook events:
- ATTRIBUTE_UPDATED (async): An attribute was updated.
@@ -31728,9 +31728,9 @@ input AttributeValueCreateInput @doc(category: "Attributes") {
}
"""
-Deletes an attribute.
+Deletes an attribute.
-Requires one of the following permissions: MANAGE_PRODUCT_TYPES_AND_ATTRIBUTES.
+Requires one of the following permissions, depending on the attribute type: MANAGE_PRODUCT_TYPES_AND_ATTRIBUTES for `PRODUCT_TYPE` attributes, MANAGE_PAGE_TYPES_AND_ATTRIBUTES for `PAGE_TYPE` attributes.
Triggers the following webhook events:
- ATTRIBUTE_DELETED (async): An attribute was deleted.
@@ -31742,9 +31742,9 @@ type AttributeDelete @doc(category: "Attributes") @webhookEventsInfo(asyncEvents
}
"""
-Updates attribute.
+Updates attribute.
-Requires one of the following permissions: MANAGE_PRODUCT_TYPES_AND_ATTRIBUTES.
+Requires one of the following permissions, depending on the attribute type: MANAGE_PRODUCT_TYPES_AND_ATTRIBUTES for `PRODUCT_TYPE` attributes, MANAGE_PAGE_TYPES_AND_ATTRIBUTES for `PAGE_TYPE` attributes.
Triggers the following webhook events:
- ATTRIBUTE_UPDATED (async): An attribute was updated.