2

我正在尝试在我的项目中实现django-ldap-auth,一切似乎都运行良好。问题是,该软件包不支持user profile1.7 之后的 Django 版本的字段填充。

来自文档:

注意 Django 1.7 及更高版本不直接支持用户配置文件。在这些版本中,LDAPBackend 将忽略与配置文件相关的设置。

我已将此添加到我的settings.py但没有任何反应(如预期的那样):
AUTH_LDAP_PROFILE_ATTR_MAP = {"description": "description"}

我的问题是:如何AUTH_LDAP_PROFILE_ATTR_MAP在较新的 django 版本中启用?

编辑:我正在考虑使用自定义用户模型,但我不确定这是否是最好的方法..

4

1 回答 1

5

我使用one-to-one User profile modelpopulate_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)
于 2016-10-24T22:01:54.377 回答