permission_classes not working for ModelViewSet
I know only the bugs are allowed to be posted here. But I have no idea whether this is bug or something I am overlooking. I did post the problem at SO but got no help. So I am posting it here.
At urls.py
# contents
## detail, update, remove
url(
r'^api/campaign/(?P<campaign>[a-z0-9-]+)/content/(?P<content>\d+)/$',
ContentAPI.as_view({'get' : 'retrieve', 'put' : 'update', 'delete' : 'destroy'}),
name = "content-detail"
),
## toggle content verification
url(
r'^api/campaign/(?P<campaign>[a-z0-9-]+)/content/(?P<content>\d+)/toggle_status/$',
ContentAPI.as_view(
{'post' : 'toggle_status'},
permission_classes = [Or(IsContentManager, IsContentModerator)]
),
name = "content-toggle-status"
),
At views.py
class ContentAPI(viewsets.ModelViewSet):
permission_classes = [Or(IsContentManager)]
... # actions and methods here
The permission here doesn't seem to work. I have replaced the permission with IsAuthenticated yet an anonymous user could access the url and actions.
So I am posting it here.
This certainly comes across as a usage issue rather than a bug or feature request. You might want to try posting to the discussion group: https://groups.google.com/forum/#!forum/django-rest-framework
The error was on my side. As I was generating nested route using router.register('api/(?P<domain>[a-z0-9]+)/sub-domain/(?P<sub_domain>[a-z0-9]+), SubdomainAPI), I had to filter domain at SubdomainAPI.
So I did this at initial():
def initial(self, request, *args, **kwargs):
self.domain = self._get_domain()
super(SubdomainAPI, self).initial(request, *args, **kwargs)
self._get_domain() checks if domain exists and raises 404 error. Since 404 error was being raised before permission checks my tests were failing.
Which should have been:
def initial(self, request, *args, **kwargs):
super(SubdomainAPI, self).initial(request, *args, **kwargs)
self.domain = self._get_domain()
Hope this will be helpful if others stumbleupon similar problem.
I personally found in core that ModelViewSet doesn't support permission_classes= and if they so - didn't found it is actually getting implemented.
On the other hand - I found relevant implementation working in APIView.
Comments?
They inherit from apiview
so why its not running permission_classes= ? Any idea?
PS: I do not have a serializer configured.
Any idea?
The discussion group is the best place to take this discussion and other usage questions. Thanks!