Allow create for staff, update for staff or owner
See original GitHub issueI’m adding a user resource to an API.
I want the following permission logic:
- Staff may do anything
- Users may read and update themselves
So I start out with this:
@staticmethod
def has_write_permission(request):
return request.user.is_staff
Now staff users can create other users, but regular users can’t.
Next, I want to allow users to update themselves, so I add the following:
def has_object_write_permission(self, request):
return request.user.is_staff or request.user == self
However, the method is never called because the write permission is already denied globally.
So I update the code as follows:
@staticmethod
def has_write_permission(request):
return True
def has_object_write_permission(self, request):
return request.user.is_staff or request.user == self
Now the code first calls has_write_permission
, then has_object_write_permission
. This is fine.
But now any user has the create permission. I want to limit creation to staff. So I add the following method:
@staticmethod
def has_create_permission(request):
return request.user.is_staff
However, this method is never queried by dry-rest-permissions. Only has_write_permission
is called, which already grants the permission.
Am I mis-understanding the concept of dry-rest-permissions?
Issue Analytics
- State:
- Created 7 years ago
- Comments:6 (2 by maintainers)
Top GitHub Comments
Actually something like this helped already:
But a clear note in the documentation would probably help.
I was using
GenericAPIView
’s and also had the problem of exceptions when I didn’t includehas_write_permission
, nor was the precedence ofhas_object_update_permission
over the globalhas_write_permission
being followed.Using the mixin that @dbrgn provided with slight modification to
super()
(I’m using Python 2.7), I was able to get it to work properly with/using APIViews instead of ViewSets.Here’s my modified mixin in case anyone needs it: