User permission checks for BOM actions (#13305)

* User permission checks for BOM actions

* bump API version
This commit is contained in:
Oliver authored and GitHub committed 2026-11-17 21:58:06 +11:00
1 parent a47db7ec18
commit ec23dcfdb8
4 files changed
+64 -2

No files matched your search

@@ -1,11 +1,14 @@
"""InvenTree API version information."""
# InvenTree API version
INVENTREE_API_VERSION = 580
INVENTREE_API_VERSION = 581
"""Increment this API version number whenever there is a significant change to the API that any clients need to know about."""
INVENTREE_API_TEXT = """
v581 -> 2026-11-17 : https://github.com/inventree/InvenTree/pull/13305
- Assign correct BOM permission role to part/copy-bom API endpoint
v580 -> 2026-11-14 : https://github.com/inventree/InvenTree/pull/13072
- Adds feature info to contenttype endpoint
+7 -1
View File
@@ -538,10 +538,16 @@ class PartSerialNumberDetail(RetrieveAPI):
class PartCopyBOM(CreateAPI):
"""API endpoint for duplicating a BOM."""
"""API endpoint for duplicating a BOM.
Requires the 'bom.add' permission.
If existing BOM items are to be removed, 'bom.delete' is also required
(this is enforced by the serializer).
"""
queryset = Part.objects.all()
serializer_class = part_serializers.PartCopyBOMSerializer
role_required = 'bom'
def get_serializer_context(self):
"""Add custom information to the serializer context for this endpoint."""
+17
View File
@@ -13,6 +13,7 @@ from django.utils.translation import gettext_lazy as _
import structlog
from rest_framework import serializers
from rest_framework.exceptions import PermissionDenied
from sql_util.utils import SubqueryCount
import common.filters
@@ -25,6 +26,7 @@ import part.filters as part_filters
import part.helpers as part_helpers
import stock.models
import users.models
import users.permissions
from data_exporter.mixins import DataExportSerializerMixin
from importer.registry import register_importer
from InvenTree.mixins import DataImportExportSerializerMixin
@@ -1807,6 +1809,21 @@ class PartCopyBOMSerializer(serializers.Serializer):
default=True,
)
def validate(self, data):
"""Ensure the user has permission to remove existing BOM items, if requested."""
data = super().validate(data)
if data.get('remove_existing', True):
request = self.context.get('request')
user = getattr(request, 'user', None)
if not users.permissions.check_user_role(user, 'bom', 'delete'):
raise PermissionDenied(
_('You do not have permission to remove existing BOM items')
)
return data
def save(self):
"""Actually duplicate the BOM."""
base_part = self.context['part']
+36
View File
@@ -3330,6 +3330,42 @@ class BomItemTest(InvenTreeAPITestCase):
# 9,000 stock directly available
self.assertEqual(data['available_stock'], 9000)
def test_bom_copy_permissions(self):
"""Test that copying a BOM requires the correct 'bom' role permissions."""
url = reverse('api-part-bom-copy', kwargs={'pk': 101})
target = Part.objects.get(pk=101)
n_target = target.bom_items.count()
n_total = BomItem.objects.count()
self.assertGreater(n_target, 0)
data = {'part': 100, 'skip_invalid': True}
# No 'bom' role permissions - request is rejected
self.post(url, data, expected_code=403)
self.post(url, {**data, 'remove_existing': False}, expected_code=403)
self.assertEqual(target.bom_items.count(), n_target)
self.assertEqual(BomItem.objects.count(), n_total)
# 'bom.add' allows copying, but not removal of existing BOM items
self.assignRole('bom.add')
self.post(url, data, expected_code=403)
self.assertEqual(target.bom_items.count(), n_target)
self.post(url, {**data, 'remove_existing': False}, expected_code=201)
self.assertGreater(target.bom_items.count(), n_target)
# 'bom.delete' allows existing BOM items to be removed
self.assignRole('bom.delete')
self.post(url, data, expected_code=201)
# Existing BOM item (sub_part=100) was removed
self.assertFalse(target.bom_items.filter(sub_part=100).exists())
def test_bom_item_uses(self):
"""Tests for the 'uses' field."""
url = reverse('api-bom-list')