Skip to content

Commit 754e462

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 4246da9 commit 754e462

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
@@ -155,20 +155,20 @@ def get_absolute_url(self):
155155
args=[self.pk],
156156
)
157157

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

170+
super().save(*args, **kwargs)
171+
172172
def get_direction_color(self):
173173
return ACLAssignmentDirectionChoices.colors.get(self.direction)
174174

0 commit comments

Comments
 (0)