gpt4 book ai didi

django - 登录过程中带有 (user.is_active =False) 标志的用户的消息

转载 作者:行者123 更新时间:2023-12-03 23:28:21 26 4
gpt4 key购买 nike

我正在尝试在登录过程中添加消息,对于拥有帐户但已停用的用户,如果他想进入,他必须激活它。

我使用 LoginView Controller ,它使用称为 AuthenticationForm 的内置标准表单

AuthenticationForm 有以下方法:


def confirm_login_allowed(self, user):
"""
Controls whether the given User may log in. This is a policy setting,
independent of end-user authentication. This default behavior is to
allow login by active users, and reject login by inactive users.

If the given user cannot log in, this method should raise a
``forms.ValidationError``.

If the given user may log in, this method should return None.
"""
if not user.is_active:
raise forms.ValidationError(
self.error_messages['inactive'],
code='inactive',

# and list of error messages within this class

error_messages = {
'invalid_login': _(
"Please enter a correct %(username)s and password. Note that both "
"fields may be case-sensitive."
),
'inactive': _("This account is inactive."),
}

因此,从技术上讲,如果不是 user.is_active - 它应该显示消息“inactive”,但在我的情况下,对于具有 is_active = False DB 表的未激活用户,它会显示消息“invalid_login”。
我正在尝试 100% 正确的登录名和密码,但用户未处于事件状态,但它向我显示了“invalid_login”消息。然后我只需将 DB 中的 is_active 标志切换为 True,它就可以让我轻松进入。
你知道为什么会这样吗?

最终目标是向拥有帐户但已停用的用户显示此消息“'inactive': _("This account is inactive.")”。 (或自定义消息)
从技术上讲,它应该可以工作,但没有。
在此先感谢您,如果您发现这个问题很初级或很愚蠢,我们很抱歉。

尝试:

class AuthCustomForm(AuthenticationForm):
def clean(self):
AuthenticationForm.clean(self)
user = ExtraUser.objects.get(username=self.cleaned_data.get('username'))
if not user.is_active and user:
messages.warning(self.request, 'Please Activate your account',
extra_tags="", fail_silently=True)
# return HttpResponseRedirect(' your url'))


最后有什么帮助:


class AuthCustomForm(AuthenticationForm):

def get_invalid_login_error(self):

user = ExtraUser.objects.get(username=self.cleaned_data.get('username'))

if not user.is_active and user:
raise forms.ValidationError(
self.error_messages['inactive'],
code='inactive',)
else:
return forms.ValidationError(
self.error_messages['invalid_login'],
code='invalid_login',
params={'username': self.username_field.verbose_name},
)


这是一种奇怪的方法,因为 DJANGO 内置代码应该可以工作。我不确定我没有解决我自己的错误,在这里之前犯过。也许我让事情变得更糟。

最佳答案

这是一个很长的答案,但希望它会有用,并提供一些有关幕后工作方式的见解。

要了解为什么不为非事件用户引发 'inactive' ValidationError,我们必须首先查看 LoginView 是如何实现的,特别是它的 post 方法。

def post(self, request, *args, **kwargs):
"""
Handle POST requests: instantiate a form instance with the passed
POST variables and then check if it's valid.
"""
form = self.get_form()
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)

LoginView 收到包含表单数据的 POST 请求时调用此方法。 get_form 使用来自请求的 AuthenticationForm 数据填充 POST,然后检查表单,根据它是否有效返回不同的响应。我们关心表单检查,所以让我们深入研究 is_valid 方法在做什么。

Django docs 很好地解释了表单和字段验证的工作原理,所以我不会详细介绍。基本上,我们需要知道的是,当调用表单的 is_valid 方法时,该表单首先单独验证其所有字段,然后调用其 clean 方法进行任何表单范围的验证。

这里我们需要看看 AuthenticationForm 是如何实现的,因为它定义了自己的 clean 方法。
def clean(self):
username = self.cleaned_data.get('username')
password = self.cleaned_data.get('password')

if username is not None and password:
self.user_cache = authenticate(self.request, username=username, password=password)
if self.user_cache is None:
raise self.get_invalid_login_error()
else:
self.confirm_login_allowed(self.user_cache)

return self.cleaned_data

这就是您确定的 confirm_login_allowed 方法发挥作用的地方。我们看到用户名和密码被传递给 authenticate 函数。这将根据 AUTHENTICATION_BACKENDS 设置定义的所有身份验证后端检查给定的凭据(有关更多信息,请参阅 Django docs),如果成功则返回已验证用户的 User 模型,否则返回 None

然后检查 authenticate 的结果。如果是 None ,则用户无法通过身份验证,并且 'invalid_login' ValidationError 会按预期引发。如果没有,则用户已经通过身份验证,如果用户处于非事件状态, confirm_login_allowed 会引发 'inactive' ValidationError

那么为什么不提出 'inactive' ValidationError 呢?

这是因为非事件用户无法进行身份验证,因此 authenticate 返回 None ,这意味着调用 get_invalid_login_error 而不是 confirm_login_allowed

为什么非事件用户认证失败?

为了看到这一点,我将假设您没有使用自定义身份验证后端,这意味着您的 AUTHENTICATION_BACKENDS 设置被设置为默认值: ['django.contrib.auth.backends.ModelBackend'] 。这意味着 ModelBackend 是唯一使用的身份验证后端,我们可以查看它的 authenticate 方法,这是之前看到的 authenticate 函数在内部调用的方法。
def authenticate(self, request, username=None, password=None, **kwargs):
if username is None:
username = kwargs.get(UserModel.USERNAME_FIELD)
if username is None or password is None:
return
try:
user = UserModel._default_manager.get_by_natural_key(username)
except UserModel.DoesNotExist:
# Run the default password hasher once to reduce the timing
# difference between an existing and a nonexistent user (#20760).
UserModel().set_password(password)
else:
if user.check_password(password) and self.user_can_authenticate(user):
return user

我们对最后的 if 语句感兴趣。
if user.check_password(password) and self.user_can_authenticate(user):
return user

对于我们的非事件用户,我们知道密码是正确的,因此 check_password 将返回 True 。这意味着它必须是 user_can_authenticate 方法,它返回 False 并导致非事件用户无法通过身份验证。等等,因为我们快到了……
def user_can_authenticate(self, user):
"""
Reject users with is_active=False. Custom user models that don't have
that attribute are allowed.
"""
is_active = getattr(user, 'is_active', None)
return is_active or is_active is None

啊哈! user_can_authenticate 返回 False 如果 user.is_activeFalse 这会导致用户无法进行身份验证。

解决方案

我们可以继承 ModelBackend ,覆盖 user_can_authenticate ,并将 AUTHENTICATION_BACKENDS 设置指向这个新的子类。

应用程序/后端.py
from django.contrib.auth import backends


class CustomModelBackend(backends.ModelBackend):
def user_can_authenticate(self, user):
return True

设置.py
AUTHENTICATION_BACKENDS = [
'app.backends.CustomModelBackend',
]

我认为这个解决方案比改变 get_invalid_login_error 的逻辑更清晰。

然后,您可以通过子类化 'inactive' 、覆盖 ValidationError 并将 AuthenticationFormerror_messages 属性设置为这个新子类来覆盖 authentication_form LoginView 消息。
from django.contrib.auth import forms as auth_forms, views as auth_views
from django.utils.translation import gettext_lazy as _


class CustomAuthenticationForm(auth_forms.AuthenticationForm):
error_messages = {
'invalid_login': _(
"Please enter a correct %(username)s and password. Note that both "
"fields may be case-sensitive."
),
'inactive': _("CUSTOM INACTIVE MESSAGE."),
}


class LoginView(auth_views.LoginView):
authentication_form = CustomAuthenticationForm

关于django - 登录过程中带有 (user.is_active =False) 标志的用户的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55609772/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com