我使用one-to-one
User profile model
和populate_user
发出的信号解决了这个问题django-ldap-auth
。
代码
from __future__ import unicode_literals
import django_auth_ldap.backend
from fences.models import Profile
from django.db import models
def populate_user_profile(sender, user=None, ldap_user=None, **kwargs):
temp_profile = None
bucket = {}
try:
temp_profile = user.profile
except:
temp_profile = Profile.objects.create(user=user)
bucket['street_address'] = ldap_user.attrs.get('streetAddress')
bucket['telephone_number'] = ldap_user.attrs.get('telephoneNumber')
bucket['title'] = ldap_user.attrs.get('title')
for key, value in bucket.items():
if value:
setattr(user.profile, key, value[0].encode('utf-8'))
user.profile.save()
django_auth_ldap.backend.populate_user.connect(populate_user_profile)