Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions djangosaml2/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,14 @@ def update_user(self, user, attributes, attribute_mapping,
try:
for attr in django_attrs:
if hasattr(user, attr):
modified = self._set_attribute(
user, attr, attributes[saml_attr][0])
user_attr = getattr(user, attr)
if callable(user_attr):
modified = user_attr(
attributes[saml_attr])
else:
modified = self._set_attribute(
user, attr, attributes[saml_attr][0])

user_modified = user_modified or modified

elif profile is not None and hasattr(profile, attr):
Expand Down
5 changes: 5 additions & 0 deletions tests/testprofiles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ class TestProfile(models.Model):
user = models.OneToOneField('auth.User')
age = models.CharField(max_length=100, blank=True)

def process_first_name(self, first_name):
self.first_name = first_name[0]
else:
from django.contrib.auth.models import AbstractUser
class TestUser(AbstractUser):
age = models.CharField(max_length=100, blank=True)

def process_first_name(self, first_name):
self.first_name = first_name[0]
21 changes: 21 additions & 0 deletions tests/testprofiles/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,24 @@ def test_update_user(self):
self.assertEquals(user.get_profile().age, '22')
else:
self.assertEquals(user.age, '22')

def test_update_user_callable_attributes(self):
user = User.objects.create(username='john')

backend = Saml2Backend()
attribute_mapping = {
'uid': ('username', ),
'mail': ('email', ),
'cn': ('process_first_name', ),
'sn': ('last_name', ),
}
attributes = {
'uid': ('john', ),
'mail': ('[email protected]', ),
'cn': ('John', ),
'sn': ('Doe', ),
}
backend.update_user(user, attributes, attribute_mapping)
self.assertEquals(user.email, '[email protected]')
self.assertEquals(user.first_name, 'John')
self.assertEquals(user.last_name, 'Doe')