Skip to content

Commit 3ad9322

Browse files
fix (backend): finer grained registration control for Single Sign On (#11190) (#11192)
* finer grained registration control for Single Sign On Fixes #11162 * fix for python 3.11 (cherry picked from commit 060e917) Co-authored-by: Matthias Mair <code@mjmair.com>
1 parent ec8c2d0 commit 3ad9322

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/backend/InvenTree/InvenTree/auth_overrides.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Overrides for allauth and adjacent packages to enforce InvenTree specific auth settings and restirctions."""
22

3+
from typing import Literal
4+
35
from django import forms
46
from django.conf import settings
57
from django.contrib.auth.models import Group
@@ -14,7 +16,6 @@
1416
from allauth.headless.adapter import DefaultHeadlessAdapter
1517
from allauth.socialaccount.adapter import DefaultSocialAccountAdapter
1618

17-
import InvenTree.sso
1819
from common.settings import get_global_setting
1920
from InvenTree.exceptions import log_error
2021

@@ -85,12 +86,12 @@ def clean(self):
8586
return cleaned_data
8687

8788

88-
def registration_enabled():
89+
RegistrationKeys = Literal['LOGIN_ENABLE_REG', 'LOGIN_ENABLE_SSO_REG']
90+
91+
92+
def registration_enabled(setting_name: RegistrationKeys = 'LOGIN_ENABLE_REG'):
8993
"""Determine whether user registration is enabled."""
90-
if (
91-
get_global_setting('LOGIN_ENABLE_REG')
92-
or InvenTree.sso.sso_registration_enabled()
93-
):
94+
if get_global_setting(setting_name):
9495
if is_email_configured():
9596
return True
9697
else:
@@ -103,12 +104,14 @@ def registration_enabled():
103104
class RegistrationMixin:
104105
"""Mixin to check if registration should be enabled."""
105106

107+
REGISTRATION_SETTING: RegistrationKeys = 'LOGIN_ENABLE_REG'
108+
106109
def is_open_for_signup(self, request, *args, **kwargs):
107110
"""Check if signup is enabled in settings.
108111
109112
Configure the class variable `REGISTRATION_SETTING` to set which setting should be used, default: `LOGIN_ENABLE_REG`.
110113
"""
111-
if registration_enabled():
114+
if registration_enabled(self.REGISTRATION_SETTING):
112115
return super().is_open_for_signup(request, *args, **kwargs)
113116
return False
114117

@@ -187,6 +190,8 @@ def send_password_reset_mail(self, user, email, context):
187190
class CustomSocialAccountAdapter(RegistrationMixin, DefaultSocialAccountAdapter):
188191
"""Override of adapter to use dynamic settings."""
189192

193+
REGISTRATION_SETTING = 'LOGIN_ENABLE_SSO_REG'
194+
190195
def is_auto_signup_allowed(self, request, sociallogin):
191196
"""Check if auto signup is enabled in settings."""
192197
if get_global_setting('LOGIN_SIGNUP_SSO_AUTO', True):

0 commit comments

Comments
 (0)