23
23
24
24
from djangosaml2 .signals import pre_user_save
25
25
26
- from . import settings as saml_settings
27
-
28
26
try :
29
27
from django .contrib .auth .models import SiteProfileNotAvailable
30
28
except ImportError :
@@ -85,8 +83,7 @@ def authenticate(self, request, session_info=None, attribute_mapping=None,
85
83
use_name_id_as_username = getattr (
86
84
settings , 'SAML_USE_NAME_ID_AS_USERNAME' , False )
87
85
88
- django_user_main_attribute = saml_settings .SAML_DJANGO_USER_MAIN_ATTRIBUTE
89
- django_user_main_attribute_lookup = saml_settings .SAML_DJANGO_USER_MAIN_ATTRIBUTE_LOOKUP
86
+ django_user_main_attribute = self .get_django_user_main_attribute ()
90
87
91
88
logger .debug ('attributes: %s' , attributes )
92
89
saml_user = None
@@ -137,15 +134,17 @@ def clean_user_main_attribute(self, main_attribute):
137
134
"""
138
135
return main_attribute
139
136
140
- def get_user_query_args (self , main_attribute ):
141
- django_user_main_attribute = getattr (
137
+ def get_django_user_main_attribute (self ):
138
+ return getattr (
142
139
settings , 'SAML_DJANGO_USER_MAIN_ATTRIBUTE' , 'username' )
143
- django_user_main_attribute_lookup = getattr (
144
- settings , 'SAML_DJANGO_USER_MAIN_ATTRIBUTE_LOOKUP' , '' )
145
140
146
- return {
147
- django_user_main_attribute + django_user_main_attribute_lookup : main_attribute
148
- }
141
+ def get_django_user_main_attribute_lookup (self ):
142
+ return getattr (settings , 'SAML_DJANGO_USER_MAIN_ATTRIBUTE_LOOKUP' , '' )
143
+
144
+ def get_user_query_args (self , main_attribute ):
145
+ lookup = (self .get_django_user_main_attribute () +
146
+ self .get_django_user_main_attribute_lookup ())
147
+ return {lookup : main_attribute }
149
148
150
149
def get_saml2_user (self , create , main_attribute , attributes , attribute_mapping ):
151
150
if create :
@@ -156,8 +155,7 @@ def get_saml2_user(self, create, main_attribute, attributes, attribute_mapping):
156
155
def _get_or_create_saml2_user (self , main_attribute , attributes , attribute_mapping ):
157
156
logger .debug ('Check if the user "%s" exists or create otherwise' ,
158
157
main_attribute )
159
- django_user_main_attribute = saml_settings .SAML_DJANGO_USER_MAIN_ATTRIBUTE
160
- django_user_main_attribute_lookup = saml_settings .SAML_DJANGO_USER_MAIN_ATTRIBUTE_LOOKUP
158
+ django_user_main_attribute = self .get_django_user_main_attribute ()
161
159
user_query_args = self .get_user_query_args (main_attribute )
162
160
user_create_defaults = {django_user_main_attribute : main_attribute }
163
161
@@ -180,7 +178,7 @@ def _get_or_create_saml2_user(self, main_attribute, attributes, attribute_mappin
180
178
181
179
def _get_saml2_user (self , main_attribute , attributes , attribute_mapping ):
182
180
User = get_saml_user_model ()
183
- django_user_main_attribute = saml_settings . SAML_DJANGO_USER_MAIN_ATTRIBUTE
181
+ django_user_main_attribute = self . get_django_user_main_attribute ()
184
182
user_query_args = self .get_user_query_args (main_attribute )
185
183
186
184
logger .debug ('Retrieving existing user "%s"' , main_attribute )
0 commit comments