-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow permission classes to customize the failure message. #2539
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -134,12 +134,14 @@ def http_method_not_allowed(self, request, *args, **kwargs): | |
""" | ||
raise exceptions.MethodNotAllowed(request.method) | ||
|
||
def permission_denied(self, request): | ||
def permission_denied(self, request, message=None): | ||
""" | ||
If request is not permitted, determine what kind of exception to raise. | ||
""" | ||
if not request.successful_authenticator: | ||
raise exceptions.NotAuthenticated() | ||
if message is not None: | ||
raise exceptions.PermissionDenied(message) | ||
raise exceptions.PermissionDenied() | ||
|
||
def throttled(self, request, wait): | ||
|
@@ -280,6 +282,8 @@ def check_permissions(self, request): | |
""" | ||
for permission in self.get_permissions(): | ||
if not permission.has_permission(request, self): | ||
if hasattr(permission, 'message'): | ||
self.permission_denied(request, permission.message) | ||
self.permission_denied(request) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we simplify this to |
||
|
||
def check_object_permissions(self, request, obj): | ||
|
@@ -289,6 +293,8 @@ def check_object_permissions(self, request, obj): | |
""" | ||
for permission in self.get_permissions(): | ||
if not permission.has_object_permission(request, self, obj): | ||
if hasattr(permission, 'message'): | ||
self.permission_denied(request, permission.message) | ||
self.permission_denied(request) | ||
|
||
def check_throttles(self, request): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -310,3 +310,89 @@ def test_cannot_read_list_permissions(self): | |
response = object_permissions_list_view(request) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertListEqual(response.data, []) | ||
|
||
|
||
class BasicPerm(permissions.BasePermission): | ||
def has_permission(self, request, view): | ||
return False | ||
|
||
|
||
class BasicPermWithDetail(permissions.BasePermission): | ||
message = 'Custom: You cannot post to this resource' | ||
|
||
def has_permission(self, request, view): | ||
return False | ||
|
||
|
||
class BasicObjectPerm(permissions.BasePermission): | ||
def has_object_permission(self, request, view, obj): | ||
return False | ||
|
||
|
||
class BasicObjectPermWithDetail(permissions.BasePermission): | ||
message = 'Custom: You cannot post to this resource' | ||
|
||
def has_object_permission(self, request, view, obj): | ||
return False | ||
|
||
|
||
class PermissionInstanceView(generics.RetrieveUpdateDestroyAPIView): | ||
queryset = BasicModel.objects.all() | ||
serializer_class = BasicSerializer | ||
|
||
|
||
class DeniedView(PermissionInstanceView): | ||
permission_classes = (BasicPerm,) | ||
|
||
|
||
class DeniedViewWithDetail(PermissionInstanceView): | ||
permission_classes = (BasicPermWithDetail,) | ||
|
||
|
||
class DeniedObjectView(PermissionInstanceView): | ||
permission_classes = (BasicObjectPerm,) | ||
|
||
|
||
class DeniedObjectViewWithDetail(PermissionInstanceView): | ||
permission_classes = (BasicObjectPermWithDetail,) | ||
|
||
denied_view = DeniedView.as_view() | ||
|
||
denied_view_with_detail = DeniedViewWithDetail.as_view() | ||
|
||
denied_object_view = DeniedObjectView.as_view() | ||
|
||
denied_object_view_with_detail = DeniedObjectViewWithDetail.as_view() | ||
|
||
|
||
class CustomPermissionsTests(TestCase): | ||
def setUp(self): | ||
BasicModel(text='foo').save() | ||
User.objects.create_user('username', '[email protected]', 'password') | ||
credentials = basic_auth_header('username', 'password') | ||
self.request = factory.get('/1', format='json', HTTP_AUTHORIZATION=credentials) | ||
self.custom_message = 'Custom: You cannot post to this resource' | ||
|
||
def test_permission_denied(self): | ||
response = denied_view(self.request, pk=1) | ||
detail = response.data.get('detail') | ||
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
self.assertNotEqual(detail, self.custom_message) | ||
|
||
def test_permission_denied_with_custom_detail(self): | ||
response = denied_view_with_detail(self.request, pk=1) | ||
detail = response.data.get('detail') | ||
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
self.assertEqual(detail, self.custom_message) | ||
|
||
def test_permission_denied_for_object(self): | ||
response = denied_object_view(self.request, pk=1) | ||
detail = response.data.get('detail') | ||
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
self.assertNotEqual(detail, self.custom_message) | ||
|
||
def test_permission_denied_for_object_with_custom_detail(self): | ||
response = denied_object_view_with_detail(self.request, pk=1) | ||
detail = response.data.get('detail') | ||
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
self.assertEqual(detail, self.custom_message) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we just simplify this to
raise exceptions.PermissionDenied(message=message)
- That'll still work fine if message isNone
, right?