mirror of
https://github.com/inventree/InvenTree.git
synced 2026-12-03 04:41:52 +00:00
Hide instance info if user does not have required view permission (#13427)
This commit is contained in:
1 parent
449e1117d1
commit
5fea43f39c
2 files changed
+16
-16
No files matched your search
@@ -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)
|
||||
|
||||
|
||||
@@ -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):
|
||||
|
||||
Reference in new issue
Block a user