From 1f03d43b927b44ee7f73d7216e3b35fa1117dcd1 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 7 Sep 2021 01:19:44 +0200 Subject: [PATCH] settings for auto-fill on sso --- InvenTree/InvenTree/forms.py | 13 ++++++++++++- InvenTree/InvenTree/settings.py | 4 ++++ InvenTree/common/models.py | 6 ++++++ InvenTree/templates/InvenTree/settings/login.html | 5 +++++ 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/forms.py b/InvenTree/InvenTree/forms.py index 68e7f90290..dedb078623 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 +from allauth.socialaccount.adapter import DefaultSocialAccountAdapter from part.models import PartCategory from common.models import InvenTreeSetting @@ -208,7 +209,7 @@ class SettingCategorySelectForm(forms.ModelForm): ) -# override allauth forms +# override allauth class CustomSignupForm(SignupForm): """ Override to use dynamic settings @@ -216,3 +217,13 @@ class CustomSignupForm(SignupForm): def __init__(self, *args, **kwargs): kwargs['email_required'] = InvenTreeSetting.get_setting('LOGIN_MAIL_REQUIRED') super().__init__(*args, **kwargs) + + +class CustomSocialAccountAdapter(DefaultSocialAccountAdapter): + """ + Override of adapter to use dynamic settings + """ + def is_auto_signup_allowed(self, request, sociallogin): + if InvenTreeSetting.get_setting('LOGIN_SIGNUP_SSO_AUTO', True): + return super().is_auto_signup_allowed(request, sociallogin) + return False diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index 692a116f35..c04b550e34 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -664,6 +664,8 @@ for app in SOCIAL_BACKENDS: # settings for allauth ACCOUNT_LOGOUT_ON_PASSWORD_CHANGE = True + +# override forms / adapters ACCOUNT_FORMS = { 'login': 'allauth.account.forms.LoginForm', 'signup': 'InvenTree.forms.CustomSignupForm', @@ -674,3 +676,5 @@ ACCOUNT_FORMS = { 'reset_password_from_key': 'allauth.account.forms.ResetPasswordKeyForm', 'disconnect': 'allauth.socialaccount.forms.DisconnectForm', } + +SOCIALACCOUNT_ADAPTER = 'InvenTree.forms.CustomSocialAccountAdapter' diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index 25f360c0a0..a209c5d5ed 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -852,6 +852,12 @@ class InvenTreeSetting(BaseInvenTreeSetting): 'default': False, 'validator': bool, }, + 'LOGIN_SIGNUP_SSO_AUTO': { + 'name': _('Auto-fill SSO users'), + 'description': _('Automatically fill out user-details from SSO account-data'), + 'default': True, + 'validator': bool, + }, } class Meta: diff --git a/InvenTree/templates/InvenTree/settings/login.html b/InvenTree/templates/InvenTree/settings/login.html index c13155a5ae..471903554b 100644 --- a/InvenTree/templates/InvenTree/settings/login.html +++ b/InvenTree/templates/InvenTree/settings/login.html @@ -18,6 +18,11 @@ {% include "InvenTree/settings/setting.html" with key="LOGIN_ENABLE_SSO" icon="fa-info-circle" %} {% include "InvenTree/settings/setting.html" with key="LOGIN_ENABLE_PWD_FORGOT" icon="fa-info-circle" %} {% include "InvenTree/settings/setting.html" with key="LOGIN_MAIL_REQUIRED" icon="fa-info-circle" %} + + {% trans 'Signup' %} + + + {% include "InvenTree/settings/setting.html" with key="LOGIN_SIGNUP_SSO_AUTO" icon="fa-info-circle" %}