Allauth - 如何禁用社交帐户的重置密码?

时间:2015-05-10 15:25:47

标签: django django-allauth

我希望社交用户只能在没有密码的情况下登录。因此,我想在/ accounts / password / reset /为社交用户禁用密码重置。

我知道我需要在此代码中添加条件:allauth.accounts.forms.py

class ResetPasswordForm(forms.Form):

    email = forms.EmailField(
        label=_("E-mail"),
        required=True,
        widget=forms.TextInput(attrs={"type": "email", "size": "30"}))

    def clean_email(self):
        email = self.cleaned_data["email"]
        email = get_adapter().clean_email(email)
        self.users = filter_users_by_email(email)
        if not self.users.exists():
            raise forms.ValidationError(_("The e-mail address is not assigned"
                                          " to any user account"))
        return self.cleaned_data["email"]

我正在考虑这个解决方案,但我不知道如何正确地做到这一点:

  elif SocialAccount.objects.filter(provider='facebook'):
      raise forms.ValidationError(_("You are using a social account.")

1 个答案:

答案 0 :(得分:0)

@bellum的答案是正确的,但是现在Allauth返回filter_users_by_email(email)上的列表

所以这对我有用

from allauth.account.forms import SignupForm, LoginForm, ResetPasswordForm
from allauth.account.adapter import get_adapter
from django.utils.translation import ugettext_lazy as _
from allauth.account.utils import filter_users_by_email

class MyResetPasswordForm(ResetPasswordForm):
    def clean_email(self):
        email = self.cleaned_data["email"]
        email = get_adapter().clean_email(email)
        self.users = filter_users_by_email(email)
        if not self.users:
            raise forms.ValidationError(_("Unfortunately no account with that"
                                          " email was found"))
        # custom code start
        elif self.users[0].socialaccount_set.filter(provider='google').exists():
            raise forms.ValidationError(_("Looks like the email/password combination was not used. Maybe try Social?"))
            # custom code end
        return self.cleaned_data["email"]

注意:如果您使用的是Facebook,那么我将使用google社交身份验证,然后将其更改为facebook。