Skip to content

Commit a7809b1

Browse files
authored
Merge pull request #45 from joetsoi/master
restore #30 "if user attribute is callable then call it..."
2 parents 4e6ec62 + be53998 commit a7809b1

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

djangosaml2/backends.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,14 @@ def update_user(self, user, attributes, attribute_mapping,
233233
try:
234234
for attr in django_attrs:
235235
if hasattr(user, attr):
236-
modified = self._set_attribute(
237-
user, attr, attributes[saml_attr][0])
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+
238244
user_modified = user_modified or modified
239245

240246
elif profile is not None and hasattr(profile, attr):

tests/testprofiles/models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ class TestProfile(models.Model):
2323
user = models.OneToOneField('auth.User')
2424
age = models.CharField(max_length=100, blank=True)
2525

26+
def process_first_name(self, first_name):
27+
self.first_name = first_name[0]
2628
else:
2729
from django.contrib.auth.models import AbstractUser
2830
class TestUser(AbstractUser):
2931
age = models.CharField(max_length=100, blank=True)
32+
33+
def process_first_name(self, first_name):
34+
self.first_name = first_name[0]

tests/testprofiles/tests.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,24 @@ def test_update_user(self):
6868
self.assertEquals(user.get_profile().age, '22')
6969
else:
7070
self.assertEquals(user.age, '22')
71+
72+
def test_update_user_callable_attributes(self):
73+
user = User.objects.create(username='john')
74+
75+
backend = Saml2Backend()
76+
attribute_mapping = {
77+
'uid': ('username', ),
78+
'mail': ('email', ),
79+
'cn': ('process_first_name', ),
80+
'sn': ('last_name', ),
81+
}
82+
attributes = {
83+
'uid': ('john', ),
84+
'mail': ('[email protected]', ),
85+
'cn': ('John', ),
86+
'sn': ('Doe', ),
87+
}
88+
backend.update_user(user, attributes, attribute_mapping)
89+
self.assertEquals(user.email, '[email protected]')
90+
self.assertEquals(user.first_name, 'John')
91+
self.assertEquals(user.last_name, 'Doe')

0 commit comments

Comments
 (0)