From 5fea43f39cb390cee28b5899202d4381d9c16e9e Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 1 Dec 2026 21:14:58 +1100 Subject: [PATCH] Hide instance info if user does not have required view permission (#13427) --- src/backend/InvenTree/common/api.py | 12 +++++++----- src/backend/InvenTree/common/test_api.py | 20 +++++++++----------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/backend/InvenTree/common/api.py b/src/backend/InvenTree/common/api.py index ce9cd83b60..942ee184d0 100644 --- a/src/backend/InvenTree/common/api.py +++ b/src/backend/InvenTree/common/api.py @@ -1554,7 +1554,11 @@ class InstanceInfoView(APIView): } if model_class: - if issubclass(model_class, InvenTreeAttachmentMixin): + # Check that the user has the required view permission for this model type + # If not - we do not provide any information about the instance + can_view = check_user_permission(request.user, model_class, 'view') + + if can_view and issubclass(model_class, InvenTreeAttachmentMixin): data['attachment_count'] = common.models.Attachment.objects.filter( model_type=model_class.__name__.lower(), model_id=model_id ).count() @@ -1571,14 +1575,12 @@ class InstanceInfoView(APIView): .count() ) - if issubclass(model_class, InvenTreeParameterMixin): + if can_view and issubclass(model_class, InvenTreeParameterMixin): data['parameter_count'] = common.models.Parameter.objects.filter( model_type=content_type, model_id=model_id ).count() - if issubclass( - model_class, InvenTreeLockableMixin - ) and check_user_permission(request.user, model_class, 'view'): + if can_view and issubclass(model_class, InvenTreeLockableMixin): mdl_cls_instance = model_class() data['locked'] = mdl_cls_instance.check_pk_locked(model_id) diff --git a/src/backend/InvenTree/common/test_api.py b/src/backend/InvenTree/common/test_api.py index 1bad98a19e..a8fd48592f 100644 --- a/src/backend/InvenTree/common/test_api.py +++ b/src/backend/InvenTree/common/test_api.py @@ -1385,14 +1385,8 @@ class InstanceInfoAPITests(InvenTreeAPITestCase): self.assertEqual(response.data['note_count'], 1) self.assertEqual(response.data['parameter_count'], 1) - def test_note_count_respects_view_permission(self): - """note_count is gated by 'view' permission on the target model, matching NoteList. - - attachment_count / parameter_count are *not* gated (matching AttachmentList / - ParameterList, neither of which apply view-permission filtering today) - this - pins down that intentional asymmetry rather than accidentally widening or - narrowing either behaviour. - """ + def test_counts_respect_view_permission(self): + """Check that the user's view permission on the target model affects the counts returned.""" from django.contrib.contenttypes.models import ContentType from common.models import Note, Parameter, ParameterTemplate @@ -1418,14 +1412,18 @@ class InstanceInfoAPITests(InvenTreeAPITestCase): url, params = self._url('part', self.part.pk) response = self.get(url, data=params, expected_code=200) - self.assertEqual(response.data['attachment_count'], 1) + self.assertEqual(response.data['attachment_count'], 0) self.assertEqual(response.data['note_count'], 0) - self.assertEqual(response.data['parameter_count'], 1) + self.assertEqual(response.data['parameter_count'], 0) + self.assertIsNone(response.data['locked']) - # Once granted view permission, the note becomes visible too + # Once granted view permission, all counts become visible self.assignRole('part.view') response = self.get(url, data=params, expected_code=200) + self.assertEqual(response.data['attachment_count'], 1) self.assertEqual(response.data['note_count'], 1) + self.assertEqual(response.data['parameter_count'], 1) + self.assertIsNotNone(response.data['locked']) class NoteAPITests(InvenTreeAPITestCase):