Skip to content

Commit f655aea

Browse files
Log invalid model field in attribute mapping
If a field is declared in the attributes mapping but does not exist on the User model nor the Profile, log it instead of ignoring silently.
1 parent 6956d6d commit f655aea

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Changes
33

44
UNRELEASED
55
----------
6+
- Log when attribute_mapping maps to nonexistent User fields.
67
- Dropped compatibility for Python < 2.7 and Django < 1.8.
78

89
0.16.10 (2017-10-02)

djangosaml2/backends.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ def update_user(self, user, attributes, attribute_mapping,
226226
modified = self._set_attribute(user, attr, attr_value_list[0])
227227

228228
user_modified = user_modified or modified
229+
else:
230+
logger.debug(
231+
'Could not find attribute "%s" on user "%s"', attr, user)
229232

230233
logger.debug('Sending the pre_save signal')
231234
signal_modified = any(

tests/testprofiles/tests.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,26 @@ def test_update_user_empty_attribute(self):
108108
# empty attribute list: no update
109109
self.assertEqual(user.last_name, 'Smith')
110110

111+
def test_invalid_model_attribute_log(self):
112+
backend = Saml2Backend()
113+
114+
attribute_mapping = {
115+
'uid': ['username'],
116+
'cn': ['nonexistent'],
117+
}
118+
attributes = {
119+
'uid': ['john'],
120+
'cn': ['John'],
121+
}
122+
123+
with self.assertLogs('djangosaml2', level='DEBUG') as logs:
124+
backend.get_saml2_user(True, 'john', attributes, attribute_mapping)
125+
126+
self.assertIn(
127+
'DEBUG:djangosaml2:Could not find attribute "nonexistent" on user "john"',
128+
logs.output,
129+
)
130+
111131
def test_django_user_main_attribute(self):
112132
backend = Saml2Backend()
113133

0 commit comments

Comments
 (0)