Skip to content

Commit c0d95cb

Browse files
Fix #8771 - Checking for authentication even if _ignore_model_permissions = True (#8772)
1 parent b87699c commit c0d95cb

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

rest_framework/permissions.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,15 +228,15 @@ def _queryset(self, view):
228228
return view.queryset
229229

230230
def has_permission(self, request, view):
231+
if not request.user or (
232+
not request.user.is_authenticated and self.authenticated_users_only):
233+
return False
234+
231235
# Workaround to ensure DjangoModelPermissions are not applied
232236
# to the root view when using DefaultRouter.
233237
if getattr(view, '_ignore_model_permissions', False):
234238
return True
235239

236-
if not request.user or (
237-
not request.user.is_authenticated and self.authenticated_users_only):
238-
return False
239-
240240
queryset = self._queryset(view)
241241
perms = self.get_required_permissions(request.method, queryset.model)
242242

tests/test_permissions.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,16 @@ class EmptyListView(generics.ListCreateAPIView):
5555
permission_classes = [permissions.DjangoModelPermissions]
5656

5757

58+
class IgnoredGetQuerySetListView(GetQuerySetListView):
59+
_ignore_model_permissions = True
60+
61+
5862
root_view = RootView.as_view()
5963
api_root_view = DefaultRouter().get_api_root_view()
6064
instance_view = InstanceView.as_view()
6165
get_queryset_list_view = GetQuerySetListView.as_view()
6266
empty_list_view = EmptyListView.as_view()
67+
ignored_get_queryset_list_view = IgnoredGetQuerySetListView.as_view()
6368

6469

6570
def basic_auth_header(username, password):
@@ -107,6 +112,27 @@ def test_api_root_view_discard_default_django_model_permission(self):
107112
response = api_root_view(request)
108113
self.assertEqual(response.status_code, status.HTTP_200_OK)
109114

115+
def test_ignore_model_permissions_with_unauthenticated_user(self):
116+
"""
117+
We check that the ``_ignore_model_permissions`` attribute
118+
doesn't ignore the authentication.
119+
"""
120+
request = factory.get('/', format='json')
121+
request.resolver_match = ResolverMatch('get', (), {})
122+
response = ignored_get_queryset_list_view(request)
123+
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
124+
125+
def test_ignore_model_permissions_with_authenticated_user(self):
126+
"""
127+
We check that the ``_ignore_model_permissions`` attribute
128+
with an authenticated user.
129+
"""
130+
request = factory.get('/', format='json',
131+
HTTP_AUTHORIZATION=self.permitted_credentials)
132+
request.resolver_match = ResolverMatch('get', (), {})
133+
response = ignored_get_queryset_list_view(request)
134+
self.assertEqual(response.status_code, status.HTTP_200_OK)
135+
110136
def test_get_queryset_has_create_permissions(self):
111137
request = factory.post('/', {'text': 'foobar'}, format='json',
112138
HTTP_AUTHORIZATION=self.permitted_credentials)

0 commit comments

Comments
 (0)