-->

Use LoginRequiredMixin and UserPassesTestMixin at

2020-07-10 07:39发布

问题:

I want to have a TemplateView Class that uses LoginRequiredMixin and UserPassesTestMixin at the same time. Something like this:

from django.views.generic import TemplateView
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin

class FinanceOverview(LoginRequiredMixin, UserPassesTestMixin, TemplateMixin):
    login_url = '/login'
    redirect_field_name = 'next'

    def test_func(self):
        return self.request.user.groups.filter(name="FinanceGrp").exists()

    def get(self, request, *args, **kwargs):
        DO SOMETHING IF USER IS AUTHENTICATED AND ALSO MEMBER OF GROUP FinanceGrp

Basically as you can see above, what I want to achieve is the following:

  • If user is not authenticated, to redirect user to:

    https://website/login?next=financeoverview 
    
  • However what I can't figure out is how to redirect users who are authenticated but do not belong to group FinanceGrp to another page. For example:

    https://website.com/access_denied?previous_page=financeoverview
    

In my case users are always redirected to /login page when they fail the group test. How can I achieve two mixins used at the same time but at the same time both of them are clashing around variable login_url. Unfortunately UserPassesTestMixin is using the same login_url so it makes this trouble for me.

Thanks in advance

Milos

回答1:

I think you're better off subclassing AccessMixin and then performing these checks yourself. Something like this:

from django.contrib.auth.mixins import AccessMixin
from django.http import HttpResponseRedirect 

class FinanceOverview(AccessMixin, TemplateMixin):

    def dispatch(self, request, *args, **kwargs):
        if not request.user.is_authenticated:
            # This will redirect to the login view
            return self.handle_no_permission()
        if not self.request.user.groups.filter(name="FinanceGrp").exists():
            # Redirect the user to somewhere else - add your URL here
            return HttpResponseRedirect(...)

        # Checks pass, let http method handlers process the request
        return super().dispatch(request, *args, **kwargs)


回答2:

You should override get_login_url:

class FinanceOverview(LoginRequiredMixin, UserPassesTestMixin, TemplateMixin):
    login_url = '/login'
    redirect_field_name = 'next'

    def test_func(self):
        return self.request.user.groups.filter(name="FinanceGrp").exists()

    def get_login_url(self):
        if self.request.user.is_authenticated:
            return URL_FOR_AUTHENTICATED_USERS
        return super().get_login_url()

    def get(self, request, *args, **kwargs):
        DO SOMETHING IF USER IS AUTHENTICATED AND ALSO MEMBER OF GROUP FinanceGrp