Thursday, June 15, 2017

Re: Need help in calling dispatch on custom mixin for authorization

I was able to resolve the issue.
In mixins.py, .get_object() was unresolvable so I inherited from SingleObjectMixin also I had to use the id to check equality.

from django.contrib.auth.mixins import LoginRequiredMixin
from django.core.exceptions import PermissionDenied
from django.views.generic.detail import SingleObjectMixin

class UserAuthorMixin(LoginRequiredMixin, SingleObjectMixin):
    def dispatch(self, request, *args, **kwargs):
        if request.user.is_authenticated() and request.user.id is not self.get_object().author.user.id:
            raise PermissionDenied

        return super(UserAuthorMixin, self).dispatch(request, *args, **kwargs)


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b44cfdba-def4-4b2f-8b5d-669fc61520d0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment