|
18 | 18 | from django.conf import settings |
19 | 19 | from django.contrib import auth |
20 | 20 | from django.contrib.auth.backends import ModelBackend |
21 | | -from django.core.exceptions import ( |
22 | | - MultipleObjectsReturned, ImproperlyConfigured, |
23 | | -) |
24 | | - |
25 | | -from djangosaml2.signals import pre_user_save |
| 21 | +from django.core.exceptions import (ImproperlyConfigured, |
| 22 | + MultipleObjectsReturned) |
26 | 23 |
|
| 24 | +from .signals import pre_user_save |
27 | 25 |
|
28 | 26 | logger = logging.getLogger('djangosaml2') |
29 | 27 |
|
30 | 28 |
|
31 | 29 | def get_model(model_path): |
| 30 | + from django.apps import apps |
32 | 31 | try: |
33 | | - from django.apps import apps |
34 | 32 | return apps.get_model(model_path) |
35 | | - except ImportError: |
36 | | - # Django < 1.7 (cannot use the new app loader) |
37 | | - from django.db.models import get_model as django_get_model |
38 | | - try: |
39 | | - app_label, model_name = model_path.split('.') |
40 | | - except ValueError: |
41 | | - raise ImproperlyConfigured("SAML_USER_MODEL must be of the form " |
42 | | - "'app_label.model_name'") |
43 | | - user_model = django_get_model(app_label, model_name) |
44 | | - if user_model is None: |
45 | | - raise ImproperlyConfigured("SAML_USER_MODEL refers to model '%s' " |
46 | | - "that has not been installed" % model_path) |
47 | | - return user_model |
| 33 | + except LookupError: |
| 34 | + raise ImproperlyConfigured("SAML_USER_MODEL refers to model '%s' that has not been installed" % model_path) |
| 35 | + except ValueError: |
| 36 | + raise ImproperlyConfigured("SAML_USER_MODEL must be of the form 'app_label.model_name'") |
48 | 37 |
|
49 | 38 |
|
50 | 39 | def get_saml_user_model(): |
51 | | - try: |
52 | | - # djangosaml2 custom user model |
| 40 | + if hasattr(settings, 'SAML_USER_MODEL'): |
53 | 41 | return get_model(settings.SAML_USER_MODEL) |
54 | | - except AttributeError: |
55 | | - try: |
56 | | - # Django 1.5 Custom user model |
57 | | - return auth.get_user_model() |
58 | | - except AttributeError: |
59 | | - return auth.models.User |
| 42 | + return auth.get_user_model() |
60 | 43 |
|
61 | 44 |
|
62 | 45 | class Saml2Backend(ModelBackend): |
@@ -164,19 +147,21 @@ def _get_or_create_saml2_user(self, main_attribute, attributes, attribute_mappin |
164 | 147 | main_attribute) |
165 | 148 | django_user_main_attribute = self.get_django_user_main_attribute() |
166 | 149 | user_query_args = self.get_user_query_args(main_attribute) |
167 | | - user_create_defaults = {django_user_main_attribute: main_attribute} |
168 | 150 |
|
169 | 151 | User = get_saml_user_model() |
| 152 | + built = False |
170 | 153 | try: |
171 | | - user, created = User.objects.get_or_create( |
172 | | - defaults=user_create_defaults, **user_query_args) |
| 154 | + user = User.objects.get(**user_query_args) |
| 155 | + except User.DoesNotExist: |
| 156 | + user = User(**{django_user_main_attribute: main_attribute}) |
| 157 | + built = True |
173 | 158 | except MultipleObjectsReturned: |
174 | 159 | logger.error("There are more than one user with %s = %s", |
175 | 160 | django_user_main_attribute, main_attribute) |
176 | 161 | return None |
177 | 162 |
|
178 | | - if created: |
179 | | - logger.debug('New user created') |
| 163 | + if built: |
| 164 | + logger.debug('Configuring new user "%s"', main_attribute) |
180 | 165 | user = self.configure_user(user, attributes, attribute_mapping) |
181 | 166 | else: |
182 | 167 | logger.debug('User updated') |
|
0 commit comments