querysets.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from django.db.models import QuerySet
  2. from users.constants import CONSTRAINT_TOKEN_USER
  3. from utilities.permissions import permission_is_exempt, qs_filter_from_constraints
  4. class RestrictedQuerySet(QuerySet):
  5. def restrict(self, user, action='view'):
  6. """
  7. Filter the QuerySet to return only objects on which the specified user has been granted the specified
  8. permission.
  9. :param user: User instance
  10. :param action: The action which must be permitted (e.g. "view" for "dcim.view_site"); default is 'view'
  11. """
  12. # Resolve the full name of the required permission
  13. app_label = self.model._meta.app_label
  14. model_name = self.model._meta.model_name
  15. permission_required = f'{app_label}.{action}_{model_name}'
  16. # Bypass restriction for superusers and exempt views
  17. if user.is_superuser or permission_is_exempt(permission_required):
  18. qs = self
  19. # User is anonymous or has not been granted the requisite permission
  20. elif not user.is_authenticated or permission_required not in user.get_all_permissions():
  21. qs = self.none()
  22. # Filter the queryset to include only objects with allowed attributes
  23. else:
  24. tokens = {
  25. CONSTRAINT_TOKEN_USER: user,
  26. }
  27. attrs = qs_filter_from_constraints(user._object_perm_cache[permission_required], tokens)
  28. # #8715: Avoid duplicates when JOIN on many-to-many fields without using DISTINCT.
  29. # DISTINCT acts globally on the entire request, which may not be desirable.
  30. allowed_objects = self.model.objects.filter(attrs)
  31. qs = self.filter(pk__in=allowed_objects)
  32. return qs