Skip to content

Commit

Permalink
Closes #10718: Optimize object-based permissions enforcement
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Oct 21, 2022
1 parent 96c4696 commit 3d687a6
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
5 changes: 2 additions & 3 deletions netbox/netbox/api/viewsets/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,5 @@ def _validate_objects(self, instance):
conforming_count = self.queryset.filter(pk__in=[obj.pk for obj in instance]).count()
if conforming_count != len(instance):
raise ObjectDoesNotExist
else:
# Check that the instance is matched by the view's queryset
self.queryset.get(pk=instance.pk)
elif not self.queryset.filter(pk=instance.pk).exists():
raise ObjectDoesNotExist
4 changes: 2 additions & 2 deletions netbox/netbox/views/generic/object_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def _create_object(self, model_form):
obj = model_form.save()

# Enforce object-level permissions
if not self.queryset.filter(pk=obj.pk).first():
if not self.queryset.filter(pk=obj.pk).exists():
raise PermissionsViolation()

# Iterate through the related object forms (if any), validating and saving each instance.
Expand Down Expand Up @@ -390,7 +390,7 @@ def post(self, request, *args, **kwargs):
obj = form.save()

# Check that the new object conforms with any assigned object-level permissions
if not self.queryset.filter(pk=obj.pk).first():
if not self.queryset.filter(pk=obj.pk).exists():
raise PermissionsViolation()

msg = '{} {}'.format(
Expand Down

0 comments on commit 3d687a6

Please sign in to comment.