问题
We've been running a site for a while which uses django-allauth for authentication using any of:
- Traditional email-based sign-up
- Google login
- Twitter login
- Facebook login
... but now we want to stop anyone creating a new account, while still allowing people who've previously created an account using any of those methods to be able to log in. Is there a setting that will let us do this? It's not clear to me that any of these documented settings would allow us to configure that.
The current settings relating to django-allauth are:
INSTALLED_APPS = (
'django.contrib.auth',
...
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.google',
'allauth.socialaccount.providers.facebook',
'allauth.socialaccount.providers.twitter',
...
)
AUTHENTICATION_BACKENDS = (
# Needed to login by username in Django admin, regardless of `allauth`
"django.contrib.auth.backends.ModelBackend",
# `allauth` specific authentication methods, such as login by e-mail
"allauth.account.auth_backends.AuthenticationBackend",
)
SOCIALACCOUNT_PROVIDERS = {
'google': {'SCOPE': ['https://www.googleapis.com/auth/userinfo.profile'],
'AUTH_PARAMS': {'access_type': 'online'}},
'facebook': {'SCOPE': ['email',]},
}
LOGIN_REDIRECT_URL = '/'
ACCOUNT_EMAIL_VERIFICATION = "mandatory"
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = True
SOCIALACCOUNT_AUTO_SIGNUP = True
回答1:
The question rnevius linked to fixed this for me. To add a bit more detail, I created a file mysite/account_adapter.py containing:
from allauth.account.adapter import DefaultAccountAdapter
class NoNewUsersAccountAdapter(DefaultAccountAdapter):
def is_open_for_signup(self, request):
"""
Checks whether or not the site is open for signups.
Next to simply returning True/False you can also intervene the
regular flow by raising an ImmediateHttpResponse
(Comment reproduced from the overridden method.)
"""
return False
And then added this to mysite/settings.py:
ACCOUNT_ADAPTER = 'mysite.account_adapter.NoNewUsersAccountAdapter'
来源:https://stackoverflow.com/questions/29794052/how-could-one-disable-new-account-creation-with-django-allauth-but-still-allow