繁体   English   中英

如何“猴子补丁”或覆盖User.is_authenticated()? 使用django-lazysignup产生问题

[英]How do I 'monkey patch' or override User.is_authenticated()? Creates issues with using django-lazysignup

我安装了django-lazysignup,现在面临的挑战是User.is_authenticated()返回True,这不是真正经过身份验证的用户,而是延迟注册的用户。 我可以使用自己的函数更新代码中对User.is_authenticated()的所有检查。 但是,其他软件包(如django-allauth)会检查此方法以确定用户是否已登录,并从尝试重定向到登录页面。

class RedirectAuthenticatedUserMixin(object):
    def dispatch(self, request, *args, **kwargs):
        self.request = request
        if request.user.is_authenticated():
            redirect_to = self.get_authenticated_redirect_url()
            response = HttpResponseRedirect(redirect_to)
            return _ajax_response(request, response)
...

是否有任何建议不需要在我包括的每个软件包中替换is_authenticated()? 他们中的大多数人似乎希望它以一种特定的方式起作用,而django-lazysignup则将其变为现实。 我可以用新的is_authenticated()方法修补用户模型吗? 如果可能的话,我应该在哪里做,以便将其附加到页面请求中?

您正在使用的django-lazysignup允许您交付自定义LazyUser类( 此处 )。 您需要做的就是编写一个带有定义的is_authenticated方法的lazysignup.models.LazyUser子类, lazysignup.models.LazyUser其设置为settings.LAZYSIGNUP_USER_MODEL

但这并没有结束您的麻烦。 许多django应用程序都假定经过身份验证的用户具有某些属性。 主要是is_staffis_superuserpermissionsgroups 首先, django.contrib.admin需要他们检查是否可以让用户进入以及向用户显示什么。 查看django.contrib.auth.models.AnonymousUser如何django.contrib.auth.models.AnonymousUser它们并将其复制。 备注:看看AnonymousUser如何继承任何用户类或db.Model 提供的用户类别仅需发出嘎嘎声。

最终只需要替换对User.is_authenticated()的所有调用。

为了防止django-allauth从登录页面重定向惰性用户,最终看起来像这样:

from allauth.account.views import AjaxCapableProcessFormViewMixin

def _ajax_response(request, response, form=None):
    if request.is_ajax():
        if (isinstance(response, HttpResponseRedirect)
            or isinstance(response, HttpResponsePermanentRedirect)):
            redirect_to = response['Location']
        else:
            redirect_to = None
        response = get_adapter().ajax_response(request,
                                           response,
                                           form=form,
                                           redirect_to=redirect_to)
    return response


class RedirectUserWithAccountMixin(object):
    def dispatch(self, request, *args, **kwargs):
        self.request = request
        if user_has_account(request.user):
            redirect_to = self.get_authenticated_redirect_url()
            response = HttpResponseRedirect(redirect_to)
            return _ajax_response(request, response)
        else:
            response = super(RedirectUserWithAccountMixin,
                             self).dispatch(request,
                                            *args,
                                            **kwargs)
        return response

    def get_authenticated_redirect_url(self):
        redirect_field_name = self.redirect_field_name
        return get_login_redirect_url(self.request,
                                      url=self.get_success_url(),
                                      redirect_field_name=redirect_field_name)

class LoginView(RedirectUserWithAccountMixin,
            AjaxCapableProcessFormViewMixin,
            FormView):
...

其中user_has_account()是我自己的用于检查用户是否实际登录的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM