-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Handle composition of unimplemented permission methods correctly #7601
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
Closed
Closed
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2864e74
Add failing tests for unimplemented permission method composition
sparkyb 95f9727
Handle composition of unimplemented permission methods correctly
sparkyb 0edc4d7
add failing test for OR with unimplemented has_object_permission
sparkyb 91f1236
ORing object permissions falls back to has_permission if no object pe…
sparkyb a224c6c
object-level only permissions must pass has_permission
sparkyb 23aa876
Custom Deferred truthy value to replace NotImplemented
sparkyb 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -528,6 +528,12 @@ def setUp(self): | |
| self.password | ||
| ) | ||
| self.client.login(username=self.username, password=self.password) | ||
| self.admin_user = User.objects.create_user( | ||
| 'paul', | ||
| '[email protected]', | ||
| 'password', | ||
| is_staff=True, | ||
| ) | ||
|
|
||
| def test_and_false(self): | ||
| request = factory.get('/1', format='json') | ||
|
|
@@ -677,3 +683,30 @@ def test_object_and_lazyness(self): | |
| assert hasperm is False | ||
| assert mock_deny.call_count == 1 | ||
| mock_allow.assert_not_called() | ||
|
|
||
| def test_has_permission_not_implemented(self): | ||
| request = factory.get('/1', format='json') | ||
| request.user = self.user | ||
| composed_perm = ~BasicObjectPerm | ||
| assert composed_perm().has_permission(request, None) is NotImplemented | ||
| assert composed_perm().has_object_permission(request, None, None) is True | ||
|
|
||
| def test_has_object_permission_not_implemented_false(self): | ||
| request = factory.get('/1', format='json') | ||
| request.user = self.user | ||
| composed_perm = ( | ||
| permissions.IsAdminUser | | ||
| BasicObjectPerm | ||
| ) | ||
| assert composed_perm().has_permission(request, None) is NotImplemented | ||
| assert composed_perm().has_object_permission(request, None, None) is False | ||
|
|
||
| def test_has_object_permission_not_implemented_true(self): | ||
| request = factory.get('/1', format='json') | ||
| request.user = self.admin_user | ||
| composed_perm = ( | ||
| permissions.IsAdminUser | | ||
| BasicObjectPerm | ||
| ) | ||
| assert composed_perm().has_permission(request, None) is True | ||
| assert composed_perm().has_object_permission(request, None, None) is True | ||
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.
What would happen if these lines are removed?
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.
TL;DR If I wrote my tests right, if you remove these two lines and run the tests, you'll see exactly why they are needed.
Long version: If
has_object_permissionreturnsNotImplementedit should fall back onhas_permission, but this is only necessary in the OR case. With a non-composed permission or an AND case, callinghas_permissionis redundant because you know it must have returned True in the originalhas_permissioncheck to make it to check the object. But with OR, it's possible thathas_permissionis False and the original check passed only because of the other side of the OR. We don't want thehas_object_permissiontest to pass for the wrong reason.