From a6c6b5c2488bda690b9bb0d19f6c8c27afc149c5 Mon Sep 17 00:00:00 2001
From: Matthias <matthias.mair@oewf.org>
Date: Thu, 7 Oct 2021 14:08:19 +0200
Subject: [PATCH] check if registration should be open in python

---
 InvenTree/InvenTree/forms.py    | 19 ++++++++++++++++++-
 InvenTree/InvenTree/settings.py |  1 +
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/InvenTree/InvenTree/forms.py b/InvenTree/InvenTree/forms.py
index b6905fd85a..809f126069 100644
--- a/InvenTree/InvenTree/forms.py
+++ b/InvenTree/InvenTree/forms.py
@@ -14,6 +14,7 @@ from crispy_forms.layout import Layout, Field
 from crispy_forms.bootstrap import PrependedText, AppendedText, PrependedAppendedText, StrictButton, Div
 
 from allauth.account.forms import SignupForm, set_form_field_order
+from allauth.account.adapter import DefaultAccountAdapter
 from allauth.socialaccount.adapter import DefaultSocialAccountAdapter
 
 from part.models import PartCategory
@@ -251,7 +252,23 @@ class CustomSignupForm(SignupForm):
         return cleaned_data
 
 
-class CustomSocialAccountAdapter(DefaultSocialAccountAdapter):
+class RegistratonMixin:
+    """
+    Mixin to check if registration should be enabled
+    """
+    def is_open_for_signup(self, request):
+        if InvenTreeSetting.get_setting('LOGIN_ENABLE_REG', True):
+            return super().is_open_for_signup(request)
+        return False
+
+
+class CustomAccountAdapter(RegistratonMixin, DefaultAccountAdapter):
+    """
+    Override of adapter to use dynamic settings
+    """
+
+
+class CustomSocialAccountAdapter(RegistratonMixin, DefaultSocialAccountAdapter):
     """
     Override of adapter to use dynamic settings
     """
diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py
index f9b9887036..a07324ec84 100644
--- a/InvenTree/InvenTree/settings.py
+++ b/InvenTree/InvenTree/settings.py
@@ -682,3 +682,4 @@ ACCOUNT_FORMS = {
 }
 
 SOCIALACCOUNT_ADAPTER = 'InvenTree.forms.CustomSocialAccountAdapter'
+ACCOUNT_ADAPTER = 'InvenTree.forms.CustomAccountAdapter'