diff --git a/cookbook/helper/AllAuthCustomAdapter.py b/cookbook/helper/AllAuthCustomAdapter.py new file mode 100644 index 00000000..ba76bfca --- /dev/null +++ b/cookbook/helper/AllAuthCustomAdapter.py @@ -0,0 +1,15 @@ +from django.conf import settings + +from allauth.account.adapter import DefaultAccountAdapter + + +class AllAuthCustomAdapter(DefaultAccountAdapter): + + def is_open_for_signup(self, request): + """ + Whether to allow sign ups. + """ + allow_signups = super( + AllAuthCustomAdapter, self).is_open_for_signup(request) + # Override with setting, otherwise default to super. + return getattr(settings, 'ACCOUNT_ALLOW_SIGNUPS', allow_signups) diff --git a/cookbook/helper/__init__.py b/cookbook/helper/__init__.py index 4f07006d..c1cb3788 100644 --- a/cookbook/helper/__init__.py +++ b/cookbook/helper/__init__.py @@ -1,4 +1,5 @@ import cookbook.helper.dal +from cookbook.helper.AllAuthCustomAdapter import AllAuthCustomAdapter __all__ = [ 'dal', diff --git a/recipes/settings.py b/recipes/settings.py index bfda01d9..121ccddb 100644 --- a/recipes/settings.py +++ b/recipes/settings.py @@ -27,9 +27,6 @@ DEMO = bool(int(os.getenv('DEMO', False))) INTERNAL_IPS = os.getenv('INTERNAL_IPS').split(',') if os.getenv('INTERNAL_IPS') else ['127.0.0.1'] -# django allauth site id -SITE_ID = int(os.getenv('ALLAUTH_SITE_ID', 1)) - # allow djangos wsgi server to server mediafiles GUNICORN_MEDIA = bool(int(os.getenv('GUNICORN_MEDIA', True))) @@ -101,15 +98,42 @@ MIDDLEWARE = [ 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] +# Auth related settings AUTHENTICATION_BACKENDS = [ 'django.contrib.auth.backends.ModelBackend', 'allauth.account.auth_backends.AuthenticationBackend', ] +# django allauth site id +SITE_ID = int(os.getenv('ALLAUTH_SITE_ID', 1)) + +ACCOUNT_ADAPTER = 'cookbook.helper.AllAuthCustomAdapter' + +# disable account creation using allauth +ACCOUNT_ALLOW_SIGNUPS = bool(int(os.getenv('ACCOUNT_ALLOW_SIGNUPS', False))) + if REVERSE_PROXY_AUTH: MIDDLEWARE.append('recipes.middleware.CustomRemoteUser') AUTHENTICATION_BACKENDS.append('django.contrib.auth.backends.RemoteUserBackend') +# Password validation +# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') REST_FRAMEWORK = { @@ -157,24 +181,6 @@ DATABASES = { } } -# Password validation -# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators - -AUTH_PASSWORD_VALIDATORS = [ - { - 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', - }, - { - 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', - }, - { - 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', - }, - { - 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', - }, -] - # Vue webpack settings VUE_DIR = os.path.join(BASE_DIR, 'vue')