Turn off user social registration in django-allauth?
Asked Answered
I

3

12

I noticed looking through the django-allauth templates there's a signup_closed.html users can be redirected to when user registration is closed or disabled. Does anyone who's familiar with that module know if there's a pre-configured setting that can be set in settings.py to turn off new user registration via existing social apps? Or do I need to configure that myself? I've read the full docs for allauth and I don't see any mention of it. Thanks.

Inutility answered 29/7, 2013 at 12:12 Comment(0)
T
13

Looks like you need to override is_open_for_signup on your adapter.

See the code.

Tingaling answered 29/7, 2013 at 12:22 Comment(0)
T
8

There is no pre-configured setting but it's easy to make one (this is what I do).

# settings.py

# Point to custom account adapter.
ACCOUNT_ADAPTER = 'myproject.myapp.adapter.CustomAccountAdapter'

# A custom variable we created to tell the CustomAccountAdapter whether to
# allow signups.
ACCOUNT_ALLOW_SIGNUPS = False
# myapp/adapter.py

from django.conf import settings

from allauth.account.adapter import DefaultAccountAdapter


class CustomAccountAdapter(DefaultAccountAdapter):

    def is_open_for_signup(self, request):
        """
        Whether to allow sign ups.
        """
        allow_signups = super(
            CustomAccountAdapter, self).is_open_for_signup(request)
        # Override with setting, otherwise default to super.
        return getattr(settings, 'ACCOUNT_ALLOW_SIGNUPS', allow_signups)

This is flexible, especially if you have multiple environments (e.g. staging) and want to allow user registration in staging before setting it live in production.

Troat answered 13/8, 2020 at 17:13 Comment(1)
This code does the trick. However, let me add a warning label: Implementing this custom adapter can prevent initial logins on social accounts, which can require operating signup methods to create the user's social account. Users with pre-existing accounts will not be affected, but if you add a user to Azure AD (for example) and let them sign in with their microsoft ID, they'll be redirected to the non-working signup views, since their account can't be created automatically.Proximal
E
5

More information at http://django-allauth.readthedocs.io/en/latest/advanced.html#custom-redirects.

You need to subclass allauth.account.adapter.DefaultAccountAdapter to override is_open_for_signup, and then set ACCOUNT_ADAPTER to your class in settings.py

Elisaelisabet answered 5/3, 2018 at 1:58 Comment(1)
In case this helps others. I followed instructions above but named my custom adapter file account_adapter.py which is seems to be reserved name so it was giving ModuleNotFoundError. I had to rename it to something else eg my_account_adapter.py and then this worked.Phosphorylase

© 2022 - 2024 — McMap. All rights reserved.