From fec8d1bc2f1459018d248a277ee69853f1a2beb4 Mon Sep 17 00:00:00 2001 From: jeremystretch Date: Tue, 4 Oct 2022 15:26:52 -0400 Subject: [PATCH] Fixes #10423: Enforce object type validation when creating journal entries --- docs/release-notes/version-3.3.md | 1 + netbox/extras/models/models.py | 8 ++++++++ netbox/netbox/models/__init__.py | 3 +-- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/docs/release-notes/version-3.3.md b/docs/release-notes/version-3.3.md index 4b7b76270..92d93f5c8 100644 --- a/docs/release-notes/version-3.3.md +++ b/docs/release-notes/version-3.3.md @@ -12,6 +12,7 @@ * [#9497](https://github.com/netbox-community/netbox/issues/9497) - Adjust non-racked device filter on site and location detailed view * [#10408](https://github.com/netbox-community/netbox/issues/10408) - Fix validation when attempting to add redundant contact assignments +* [#10423](https://github.com/netbox-community/netbox/issues/10423) - Enforce object type validation when creating journal entries * [#10435](https://github.com/netbox-community/netbox/issues/10435) - Fix exception when filtering VLANs by virtual machine with no cluster assigned * [#10439](https://github.com/netbox-community/netbox/issues/10439) - Fix form widget styling for DeviceType airflow field * [#10445](https://github.com/netbox-community/netbox/issues/10445) - Avoid rounding virtual machine memory values diff --git a/netbox/extras/models/models.py b/netbox/extras/models/models.py index 0df34c146..1bcc91d62 100644 --- a/netbox/extras/models/models.py +++ b/netbox/extras/models/models.py @@ -463,6 +463,14 @@ class JournalEntry(CustomFieldsMixin, CustomLinksMixin, TagsMixin, WebhooksMixin def get_absolute_url(self): return reverse('extras:journalentry', args=[self.pk]) + def clean(self): + super().clean() + + # Prevent the creation of journal entries on unsupported models + permitted_types = ContentType.objects.filter(FeatureQuery('journaling').get_query()) + if self.assigned_object_type not in permitted_types: + raise ValidationError(f"Journaling is not supported for this object type ({self.assigned_object_type}).") + def get_kind_color(self): return JournalEntryKindChoices.colors.get(self.kind) diff --git a/netbox/netbox/models/__init__.py b/netbox/netbox/models/__init__.py index aefb733b4..1385dd585 100644 --- a/netbox/netbox/models/__init__.py +++ b/netbox/netbox/models/__init__.py @@ -20,7 +20,6 @@ class NetBoxFeatureSet( CustomLinksMixin, CustomValidationMixin, ExportTemplatesMixin, - JournalingMixin, TagsMixin, WebhooksMixin ): @@ -51,7 +50,7 @@ class ChangeLoggedModel(ChangeLoggingMixin, CustomValidationMixin, models.Model) abstract = True -class NetBoxModel(CloningMixin, NetBoxFeatureSet, models.Model): +class NetBoxModel(CloningMixin, JournalingMixin, NetBoxFeatureSet, models.Model): """ Primary models represent real objects within the infrastructure being modeled. """