Skip to content

Commit c37f4e0

Browse files
authored
Merge pull request #63 from cranti/master
Fix user update when attribute list is empty
2 parents a3bfdf2 + b92ed28 commit c37f4e0

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

djangosaml2/backends.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -230,27 +230,23 @@ def update_user(self, user, attributes, attribute_mapping,
230230
user_modified = False
231231
profile_modified = False
232232
for saml_attr, django_attrs in attribute_mapping.items():
233-
try:
234-
for attr in django_attrs:
235-
if hasattr(user, attr):
236-
user_attr = getattr(user, attr)
237-
if callable(user_attr):
238-
modified = user_attr(
239-
attributes[saml_attr])
240-
else:
241-
modified = self._set_attribute(
242-
user, attr, attributes[saml_attr][0])
243-
244-
user_modified = user_modified or modified
245-
246-
elif profile is not None and hasattr(profile, attr):
247-
modified = self._set_attribute(
248-
profile, attr, attributes[saml_attr][0])
249-
profile_modified = profile_modified or modified
250-
251-
except KeyError:
252-
# the saml attribute is missing
253-
pass
233+
attr_value_list = attributes.get(saml_attr)
234+
if not attr_value_list:
235+
continue
236+
237+
for attr in django_attrs:
238+
if hasattr(user, attr):
239+
user_attr = getattr(user, attr)
240+
if callable(user_attr):
241+
modified = user_attr(attr_value_list)
242+
else:
243+
modified = self._set_attribute(user, attr, attr_value_list[0])
244+
245+
user_modified = user_modified or modified
246+
247+
elif profile is not None and hasattr(profile, attr):
248+
modified = self._set_attribute(profile, attr, attr_value_list[0])
249+
profile_modified = profile_modified or modified
254250

255251
logger.debug('Sending the pre_save signal')
256252
signal_modified = any(

tests/testprofiles/tests.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,25 @@ def test_update_user_callable_attributes(self):
8989
self.assertEquals(user.email, '[email protected]')
9090
self.assertEquals(user.first_name, 'John')
9191
self.assertEquals(user.last_name, 'Doe')
92+
93+
def test_update_user_empty_attribute(self):
94+
user = User.objects.create(username='john', last_name='Smith')
95+
96+
backend = Saml2Backend()
97+
attribute_mapping = {
98+
'uid': ('username', ),
99+
'mail': ('email', ),
100+
'cn': ('first_name', ),
101+
'sn': ('last_name', ),
102+
}
103+
attributes = {
104+
'uid': ('john', ),
105+
'mail': ('[email protected]', ),
106+
'cn': ('John', ),
107+
'sn': (),
108+
}
109+
backend.update_user(user, attributes, attribute_mapping)
110+
self.assertEquals(user.email, '[email protected]')
111+
self.assertEquals(user.first_name, 'John')
112+
# empty attribute list: no update
113+
self.assertEquals(user.last_name, 'Smith')

0 commit comments

Comments
 (0)