adapters: truncate attributes assigned to user fields (fixes #7907)

This commit is contained in:
Benjamin Dauvergne 2015-07-22 16:22:59 +02:00
parent 7ff1969bf5
commit b1b0494ccc
2 changed files with 16 additions and 0 deletions

View File

@ -87,6 +87,9 @@ class DefaultAdapter(object):
log.warning('invalid reference in attribute mapping template %r: %s', tpl, e)
else:
attribute_set = True
model_field = user._meta.get_field(field)
if hasattr(model_field, 'max_length'):
value = value[:model_field.max_length]
setattr(user, field, value)
if attribute_set:
user.save()

View File

@ -77,3 +77,16 @@ def test_provision(settings):
adapter.provision(user, idp, local_saml_attributes)
assert not user.email
User.objects.all().delete()
local_saml_attributes = saml_attributes.copy()
settings.MELLON_ATTRIBUTE_MAPPING = {
'email': '{attributes[email][0]}',
'first_name': '{attributes[first_name][0]}',
'last_name': '{attributes[last_name][0]}',
}
local_saml_attributes['first_name'] = [('y' * 32)]
user = User(username='xx')
user.save()
adapter.provision(user, idp, local_saml_attributes)
assert user.first_name == 'y' * 30
User.objects.all().delete()