Skip to content

Commit 81b739d

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 01d114d commit 81b739d

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

netbox_acls/models/access_lists.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,22 +156,18 @@ def get_absolute_url(self):
156156
args=[self.pk],
157157
)
158158

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

169+
super().save(*args, **kwargs)
170+
175171
def get_direction_color(self):
176172
return ACLAssignmentDirectionChoices.colors.get(self.direction)
177173

0 commit comments

Comments
 (0)