【问题标题】:django-ldap-auth user profile in django > 1.7django > 1.7 中的 django-ldap-auth 用户配置文件
【发布时间】:2016-10-23 23:31:45
【问题描述】:
我正在尝试在我的项目中实现django-ldap-auth,一切似乎都运行良好。问题是,该软件包不支持 1.7 之后的 Django 版本的 user profile 字段填充。
来自文档:
注意 Django 1.7 及更高版本不直接支持用户配置文件。在这些版本中,LDAPBackend 将忽略与配置文件相关的设置。
我已将此添加到我的 settings.py,但没有任何反应(如预期的那样):
AUTH_LDAP_PROFILE_ATTR_MAP = {"description": "description"}
我的问题是:如何在较新的 django 版本中启用 AUTH_LDAP_PROFILE_ATTR_MAP?
编辑:我正在考虑使用自定义用户模型,但我不确定这是否是最好的方法..
【问题讨论】:
标签:
python
django
django-auth-ldap
【解决方案1】:
我使用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)