django-rest-framework: add additional permission in ViewSet update method
Asked Answered
M

5

13

I have the following code:

class UsersViewSet(viewsets.ModelViewSet):
    model = Users
    permission_classes = (IsAuthenticated,)

    def update(self, request, *args, **kwargs):
        return super(UsersViewSet, self).update(request, *args, **kwargs)

The question is:

  • how can I add additional Permission only for update method? (need to get isAuthenticated + Permission)
  • overwrite permissions only for update method? (need to get only Permission without isAuthenticated) other methods in viewset should have IsAuthenticated permission

Can I make it with decorator?Or anything else?

Wanna get something like that:

@permission_classes((IsAuthenticated, AdditionalPermission ))
def update:
    pass

But if i write this code the second permission is not checked through request

Monkery answered 13/8, 2014 at 10:26 Comment(0)
P
37

LATER EDIT

As it seems that DRF decorators don't really work (at least not for me), this is the best solution I could come up with:

def get_permissions(self):
    # Your logic should be all here
    if self.request.method == 'GET':
        self.permission_classes = [DummyPermission, ]
    else:
        self.permission_classes = [IsAuthenticated, ]

    return super(UsersViewSet, self).get_permissions()

This actually works for both cases that you asked, but requires a bit more work. However, I've tested it and it does the job.

ORIGINAL ANSWER BELOW

There is a small mistake in the docs, you should be sending a list to the decorator (not a tuple). So it should be like this:

@permission_classes([IsAuthenticated, AdditionalPermission, ])
def update:
    pass

To answer your questions:

how can I add additional Permission only for update method?

First of all, you should know that DRF first checks for global permissions (those from the settings file), then for view permissions (declared in permission_classes -- if these exist, they will override global permissions) and only after that for method permissions (declared with the decorator @permission_classes). So another way to do the above is like this:

@permission_classes([AdditionalPermission, ])
def update:
    pass

Since ISAuthenticated is already set on the entire view, it will always be checked BEFORE any other permission.

overwrite permissions only for update method?

Well, this is hard(er), but not impossible. You can:

  • set the permissions for each method and remove it from the class
  • modify your AdditionalPermission class so that it also checks for user authentication if the method is not update.

Good luck.

Provo answered 13/8, 2014 at 15:31 Comment(8)
It doesn't work. From my code I see that only declared as class atr permissions work. And they overwrite default permissions. Permissions appended in decorator are not checked.Monkery
If you see decorators.permission_classes you'll find that it overwrites all permission_classes set on it before: func.permission_classes = permission_classesMonkery
Yup, you are right, this doesn't work for me either. Check my updated answer :)Provo
and I was hoping that there is a beautiful method but still thank you very much :)Monkery
I was hoping too, the decorators should work, but idk ... Maybe in version 3 of DRF (who is gonna come out soon) it will work :). Glad I could help.Provo
@Provo Good post, but actually, I think we should put the method that works in the TOP of the post....Swartz
@R.García, you are right, it makes more sense :) Thank you!Provo
When implemented in this way the options from the Header doesn't gets passed when testing, it sometime works and sometimes notJobi
L
12

You can also specify permissions for specific methods in the get_permissions() method:

class MyViewSet(viewsets.ModelViewSet):

    def get_permissions(self):
        if self.action in ('update', 'other_viewset_method'):
            self.permission_classes = [permissions.CustomPermissions,]
        return super(self.__class__, self).get_permissions()
Lapointe answered 3/6, 2015 at 15:31 Comment(1)
why use self.__class__ instead of MyViewSet? This will lead to infinite recursion if you ever inherit MyViewSetRive
G
2

@permission_classes didn't work for class based view. And I tried @detail_route(permission_classes=(permissions.CustomPermissions,)) for update view function, still not work.

so, my solution is:

 class MyViewSet(viewsets.ModelViewSet):

    def update(self, request, *args, **kwargs):
        self.methods=('put',)
        self.permission_classes = (permissions.CustomPermissions,)
        return super(self.__class__, self).update(request, *args, **kwargs)

Have a try. my drf is v3.1.1

Germiston answered 27/4, 2015 at 10:18 Comment(0)
V
1

For me, get_permissions worked but it did turn out that if you sending in Authorization in your header in your request rest framework will throw an error even if permission is set to AllowAny. If you are going to use both authorization and AllowAny you need to have to take this into consideration.

Vanquish answered 28/6, 2020 at 10:2 Comment(2)
How did you resolve this error, I facing it now. I have DjangoModelPermissions, and IsAuthenticated classes in self.permission_classes I am trying to change this on the basis of user authentication or query_params, this works well in the browsable API , but I try to test with thunder client, it raises permission denied or authoriaztion not provided.Jobi
Is it not possible to check if query_param "NoAutorization" for example exists. If it exists then you can remove it from the header before self.persmission_classes runs? If you using React or Angular you can probably check the query param before you post if user is authenticated if not then remove it from the header completely. Then DRF won't throw authoriaztion not provided error.Vanquish
P
0

Yes you can by adding annotation See this link for more information there are examples:

https://docs.djangoproject.com/en/1.6/topics/auth/default/#django.contrib.auth.decorators.permission_required

Pantheism answered 13/8, 2014 at 10:31 Comment(1)
Freelancer, I have permission like that django-rest-framework.org/api-guide/permissions, looking for django-rest-framework decisionMonkery

© 2022 - 2024 — McMap. All rights reserved.