querysets.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from django.db.models import Q, QuerySet
  2. from utilities.permissions import permission_is_exempt
  3. class RestrictedQuerySet(QuerySet):
  4. def restrict(self, user, action='view'):
  5. """
  6. Filter the QuerySet to return only objects on which the specified user has been granted the specified
  7. permission.
  8. :param user: User instance
  9. :param action: The action which must be permitted (e.g. "view" for "dcim.view_site"); default is 'view'
  10. """
  11. # Resolve the full name of the required permission
  12. app_label = self.model._meta.app_label
  13. model_name = self.model._meta.model_name
  14. permission_required = f'{app_label}.{action}_{model_name}'
  15. # Bypass restriction for superusers and exempt views
  16. if user.is_superuser or permission_is_exempt(permission_required):
  17. qs = self
  18. # User is anonymous or has not been granted the requisite permission
  19. elif not user.is_authenticated or permission_required not in user.get_all_permissions():
  20. qs = self.none()
  21. # Filter the queryset to include only objects with allowed attributes
  22. else:
  23. attrs = Q()
  24. for perm_attrs in user._object_perm_cache[permission_required]:
  25. if type(perm_attrs) is list:
  26. for p in perm_attrs:
  27. attrs |= Q(**p)
  28. elif perm_attrs:
  29. attrs |= Q(**perm_attrs)
  30. else:
  31. # Any permission with null constraints grants access to _all_ instances
  32. attrs = Q()
  33. break
  34. qs = self.filter(attrs)
  35. return qs