Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
* Add proper permission check for product attributes
akjanik8eb8b4057f74
diff --git a/saleor/dashboard/product/views.py b/saleor/dashboard/product/views.pyindex 4591095123..270188e65c 100644--- a/saleor/dashboard/product/views.py+++ b/saleor/dashboard/product/views.py@@ -15,7 +15,7 @@ from ...product.models import (from ...product.utils import (get_availability, get_product_costs_data, get_variant_costs_data)from ...settings import DASHBOARD_PAGINATE_BY-from ..views import staff_member_required, superuser_required+from ..views import staff_member_requiredfrom . import forms@@ -457,7 +457,8 @@ def variant_delete(request, product_pk, variant_pk):ctx)-@superuser_required+@staff_member_required+@permission_required('product.view_attributes')def attribute_list(request):attributes = [(attribute.pk, attribute.name, attribute.values.all())@@ -471,7 +472,8 @@ def attribute_list(request):ctx)-@superuser_required+@staff_member_required+@permission_required('product.view_attributes')def attribute_detail(request, pk):attributes = ProductAttribute.objects.prefetch_related('values').all()attribute = get_object_or_404(attributes, pk=pk)@@ -481,7 +483,8 @@ def attribute_detail(request, pk):{'attribute': attribute})-@superuser_required+@staff_member_required+@permission_required('product.edit_attributes')def attribute_edit(request, pk=None):if pk:attribute = get_object_or_404(ProductAttribute, pk=pk)@@ -505,7 +508,8 @@ def attribute_edit(request, pk=None):ctx)-@superuser_required+@staff_member_required+@permission_required('product.edit_attributes')def attribute_delete(request, pk):attribute = get_object_or_404(ProductAttribute, pk=pk)if request.method == 'POST':diff --git a/saleor/product/models.py b/saleor/product/models.pyindex 9c9936b403..94b7eaa684 100644--- a/saleor/product/models.py+++ b/saleor/product/models.py@@ -166,7 +166,13 @@ class Product(models.Model, ItemRange, index.Indexed):'Can view product types')),('edit_classes',pgettext_lazy('Permission description',- 'Can edit product types')))+ 'Can edit product types')),+ ('view_attributes',+ pgettext_lazy('Permission description',+ 'Can view product attributes')),+ ('edit_attributes',+ pgettext_lazy('Permission description',+ 'Can edit product attributes')))def __iter__(self):if not hasattr(self, '__variants'):diff --git a/templates/dashboard/base.html b/templates/dashboard/base.htmlindex b9b463567f..642ea17d71 100644--- a/templates/dashboard/base.html+++ b/templates/dashboard/base.html@@ -194,14 +194,14 @@{% trans "Configuration" context "Dashboard configuration" %}</p><ul>- {% if perms.userprofile.view_class %}+ {% if perms.product.view_class %}<li class="{% block menu_product_classes_class %}{% endblock %}"><a href="{% url 'dashboard:product-class-list' %}">{% trans "Product types" context "Dashboard product types list" %}</a></li>{% endif %}- {% if perms.userprofile.view_attributes %}+ {% if perms.product.view_attributes %}<li class="{% block menu_attributes_class %}{% endblock %}"><a href="{% url 'dashboard:product-attributes' %}">{% trans "Attributes" context "Dashboard attributes list" %}diff --git a/templates/dashboard/product/product_attribute/detail.html b/templates/dashboard/product/product_attribute/detail.htmlindex 284cd4f0a6..b61a479bc4 100644--- a/templates/dashboard/product/product_attribute/detail.html+++ b/templates/dashboard/product/product_attribute/detail.html@@ -43,6 +43,7 @@{{ attribute.name }}</span></div>+ {% if perms.product.edit_attributes %}<div class="card-action"><a class="btn btn-flat" href="{% url "dashboard:product-attribute-update" attribute.pk %}">{% trans "Edit attribute" context "Attribute detail action" %}@@ -52,6 +53,7 @@{% trans "Remove attribute" context "Attribute detail action" %}</a></div>+ {% endif %}</div></div><div class="col m12 l9">diff --git a/templates/dashboard/product/product_attribute/list.html b/templates/dashboard/product/product_attribute/list.htmlindex d56c4e71c9..96f73200e0 100644--- a/templates/dashboard/product/product_attribute/list.html+++ b/templates/dashboard/product/product_attribute/list.html@@ -13,11 +13,13 @@{% block menu_attributes_class %}active{% endblock %}{% block header_extra %}+ {% if perms.product.edit_attributes %}<div class="fixed-action-btn"><a class="btn-fab btn-floating btn-large waves-effect waves-light" href="{% url 'dashboard:product-attribute-add' %}" title="{% trans "Add attribute" context "Attribute list add button text" %}"><svg data-src="{% static "dashboard/images/add.svg" %}" /></a></div>+ {% endif %}{% endblock %}{% block breadcrumbs %}diff --git a/templates/dashboard/product/product_class/form.html b/templates/dashboard/product/product_class/form.htmlindex f69e58b97b..7008907f13 100644--- a/templates/dashboard/product/product_class/form.html+++ b/templates/dashboard/product/product_class/form.html@@ -53,12 +53,14 @@<svg data-src="{% static "dashboard/images/more_vert.svg" %}" height="24" width="24" /></a><ul id="context-menu" class="dropdown-content">+ {% if perms.product.edit_attributes %}<li><a href="#base-modal" data-href="{% url 'dashboard:product-class-delete' pk=product_class.pk %}"class="modal-trigger-custom">{% trans "Remove product type" context "Product class action" %}</a></li>+ {% endif %}</ul>{% endif %}{% endblock %}@@ -92,6 +94,7 @@</div></div></div>+ {% if perms.product.edit_classes %}<div class="card-action right-align">{% if product_class.pk %}<a href="{% url 'dashboard:product-class-list' %}" class="btn btn-flat">@@ -109,6 +112,7 @@</button>{% endif %}</div>+ {% endif %}</form></div></div>diff --git a/templates/dashboard/product/product_class/list.html b/templates/dashboard/product/product_class/list.htmlindex 035742f92f..d305bf1956 100644--- a/templates/dashboard/product/product_class/list.html+++ b/templates/dashboard/product/product_class/list.html@@ -14,11 +14,13 @@{% block menu_product_classes_class %}active{% endblock %}{% block header_extra %}+{% if perms.product.edit_classes %}<div class="fixed-action-btn"><a class="btn-fab btn-floating btn-large waves-effect waves-light" title="{% trans "Add product type" context "Product type list add button text" %}" href="{% url 'dashboard:product-class-add' %}"><svg data-src="{% static "dashboard/images/add.svg" %}" /></a></div>+{% endif %}{% endblock %}{% block breadcrumbs %}diff --git a/tests/conftest.py b/tests/conftest.pyindex 790c7e16f9..b0fdb5034b 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -493,3 +493,12 @@ def permission_view_classes():def permission_edit_classes():return Permission.objects.get(codename='edit_classes')++def permission_view_attributes():+ return Permission.objects.get(codename='view_attributes')+++def permission_edit_attributes():+ return Permission.objects.get(codename='edit_attributes')diff --git a/tests/dashboard/test_permissions.py b/tests/dashboard/test_permissions.pyindex 2f25d85357..3a616b0661 100644--- a/tests/dashboard/test_permissions.py+++ b/tests/dashboard/test_permissions.py@@ -801,3 +801,49 @@ def test_staff_with_permissions_can_edit_add_and_delete_products_classes_list(assert response.status_code == 200response = staff_client.get(reverse('dashboard:product-class-add'))assert response.status_code == 200+++def test_staff_with_permissions_can_view_products_attributes_list(+ staff_client, staff_user, staff_group, permission_view_attributes,+ color_attribute):+ assert not staff_user.has_perm("product.view_attributes")+ response = staff_client.get(reverse('dashboard:product-attributes'))+ assert response.status_code == 302+ response = staff_client.get(reverse('dashboard:product-attribute-detail',+ args=[color_attribute.pk]))+ assert response.status_code == 302+ staff_group.permissions.add(permission_view_attributes)+ staff_user.groups.add(staff_group)+ staff_user = User.objects.get(pk=staff_user.pk)+ assert staff_user.has_perm("product.view_attributes")+ response = staff_client.get(reverse('dashboard:product-attributes'))+ assert response.status_code == 200+ response = staff_client.get(reverse('dashboard:product-attribute-detail',+ args=[color_attribute.pk]))+ assert response.status_code == 200+++def test_staff_with_permissions_can_update_add_and_delete_products_attributes(+ staff_client, staff_user, staff_group, permission_edit_attributes,+ color_attribute):+ assert not staff_user.has_perm("product.edit_attributes")+ response = staff_client.get(reverse('dashboard:product-attribute-update',+ args=[color_attribute.pk]))+ assert response.status_code == 302+ response = staff_client.get(reverse('dashboard:product-attribute-delete',+ args=[color_attribute.pk]))+ assert response.status_code == 302+ response = staff_client.get(reverse('dashboard:product-attribute-add'))+ assert response.status_code == 302+ staff_group.permissions.add(permission_edit_attributes)+ staff_user.groups.add(staff_group)+ staff_user = User.objects.get(pk=staff_user.pk)+ assert staff_user.has_perm("product.edit_attributes")+ response = staff_client.get(reverse('dashboard:product-attribute-update',+ args=[color_attribute.pk]))+ assert response.status_code == 200+ response = staff_client.get(reverse('dashboard:product-attribute-delete',+ args=[color_attribute.pk]))+ assert response.status_code == 200+ response = staff_client.get(reverse('dashboard:product-attribute-add'))+ assert response.status_code == 200