Skip to content

Commit 34e3cfa

Browse files
committed
fix(models): Ensure interface host matches access list host
Move validation to the `save` method to enforce that the assigned interface's host matches the access list's host before saving. This replaces validation in the `clean` method for better data integrity enforcement.
1 parent 4bc2d70 commit 34e3cfa

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

netbox_acls/models/access_lists.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -157,20 +157,20 @@ def get_absolute_url(self):
157157
args=[self.pk],
158158
)
159159

160-
def clean(self):
161-
super().clean()
162-
163-
# Get the model type of the assigned interface.
164-
if self.assigned_object_type.model_class() == VMInterface:
165-
interface_host = self.assigned_object.virtual_machine
166-
elif self.assigned_object_type.model_class() == Interface:
167-
interface_host = self.assigned_object.device
168-
# Check if the assigned interface's host is the same as the host assigned to the access list.
169-
if interface_host != self.access_list.assigned_object:
160+
def save(self, *args, **kwargs):
161+
"""Saves the current instance to the database."""
162+
# Ensure the assigned interface's host matches the host assigned to the access list.
163+
if self.assigned_object.parent_object != self.access_list.assigned_object:
170164
raise ValidationError(
171-
{"assigned_object": _("The assigned object must be the same as the device assigned to it.")}
165+
{
166+
"assigned_object": _(
167+
"The assigned interface must belong to the same device or virtual machine as the access list."
168+
)
169+
},
172170
)
173171

172+
super().save(*args, **kwargs)
173+
174174
def get_direction_color(self):
175175
return ACLAssignmentDirectionChoices.colors.get(self.direction)
176176

0 commit comments

Comments
 (0)