django-rest-framework icon indicating copy to clipboard operation
django-rest-framework copied to clipboard

permission_classes not working for ModelViewSet

Open learner010 opened this issue 11 years ago • 6 comments

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.

learner010 avatar Mar 10 '15 05:03 learner010

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

lovelydinosaur avatar Mar 10 '15 09:03 lovelydinosaur

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.

learner010 avatar Mar 12 '15 06:03 learner010

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?

hamzawaqas-10p avatar Jun 05 '15 11:06 hamzawaqas-10p

They inherit from apiview

xordoquy avatar Jun 05 '15 12:06 xordoquy

so why its not running permission_classes= ? Any idea?

PS: I do not have a serializer configured.

hamzawaqas-10p avatar Jun 05 '15 12:06 hamzawaqas-10p

Any idea?

The discussion group is the best place to take this discussion and other usage questions. Thanks!

kevin-brown avatar Jun 05 '15 13:06 kevin-brown