Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could one disable new account creation with django-allauth, but still allow existing users to sign in? [duplicate]

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
like image 886
Mark Longair Avatar asked Apr 22 '15 09:04

Mark Longair


1 Answers

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'
like image 75
Mark Longair Avatar answered Oct 20 '22 18:10

Mark Longair