【问题标题】:django-auth-ldap - Display user information from LDAP in custom viewdjango-auth-ldap - 在自定义视图中显示来自 LDAP 的用户信息
【发布时间】:2023-03-04 19:28:01
【问题描述】:

我正在使用具有以下设置的 django-auth-ldap,因为 ldap 服务器上没有默认/全局“管理员”用户,它仅用于验证用户,用户自己可能会看到他们的用户信息。

AUTH_LDAP_BIND_AS_AUTHENTICATING_USER=True

django-ldap-debug.log 中,当(作为登录用户)在视图LDAPBackend().populate_user(request.user.username) 中调用时出现以下错误

search_s('uid=vchrizz,ou=Users,dc=funkfeuer,dc=at', 0, '(objectClass=*)') raised NO_SUCH_OBJECT({'desc': 'No such object'},)
search_s('uid=vchrizz,ou=Users,dc=funkfeuer,dc=at', 0, '(objectClass=*)') returned 0 objects: 

仅在登录时(使用来自django.contrib.auth.decoratorslogin_required)它似乎返回一个用户对象:

search_s('uid=vchrizz,ou=Users,dc=funkfeuer,dc=at', 0, '(objectClass=*)') returned 1 objects: uid=vchrizz,ou=users,dc=funkfeuer,dc=at

然后我注意到,我需要设置

AUTH_LDAP_BIND_DN
AUTH_LDAP_BIND_PASSWORD

要摆脱错误,但首先我不想指定一个带密码的用户(因为 bind_as_authenticating_user),第二个 populate_user() 仍然返回 NoneType ...

为什么? ldap如何获取返回的用户信息?

我的目标是在我的自定义 /userinfo/ 视图中显示来自 ldap-user 的所有 ldap 用户信息,例如 uidNumber。 http://pastebin.com/VqGiwzFE

谢谢,克里斯

【问题讨论】:

  • 我忘了让它工作:user = auth.authenticate(username="theuser", password="thepass") 自然我不想在这里硬编码用户凭据,所以我可以用request.user.username 替换“theuser”,但不是密码,因为request.user.password 是散列密码。由于密码在 POST 中的 /login/ 视图中,我想最好将(经过身份验证的)用户对象从 /login/ 视图获取到 /userinfo/ 视图中?但是我该怎么做呢?
  • 来自Django-AttributeError 'User' object has no attribute 'backend' (But…it does?) 我读到需要 auth.authenticate(),我想我需要这个:Re: Modify authentication backend 以某种方式将用户对象从登录视图获取到用户信息视图?

标签: python django ldap django-auth-ldap


【解决方案1】:

最后auth.authenticate 不见了,我不得不 rtfm:

Storing additional information about users 这是获取用户信息的首选方式,而不是我正在寻找的方式。

settings.py:

AUTH_LDAP_PROFILE_ATTR_MAP = {
    "uid": "uid",
    "cn": "cn",
    "sn": "sn",
    "givenName": "givenName",
    "userPassword": "userPassword",
    "shadowLastChange": "shadowLastChange",
    "shadowMax": "shadowMax",
    "shadowWarning": "shadowWarning",
    "loginShell": "loginShell",
    "uidNumber": "uidNumber",
    "gidNumber": "gidNumber",
    "homeDirectory": "homeDirectory",
    "gecos": "gecos",
    "mail": "mail",
    "l": "l",
    "telephoneNumber": "telephoneNumber",
}
AUTH_PROFILE_MODULE = 'myapp.UserProfile'

models.py:

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save

class UserProfile(models.Model):
    # This field is required.
    user = models.OneToOneField(User)
    # Other fields here
    uid = models.CharField(max_length=254)
    cn = models.CharField(max_length=254)
    sn = models.CharField(max_length=254)
    givenName = models.CharField(max_length=254)
    userPassword = models.CharField(max_length=254)
    shadowLastChange = models.IntegerField(null=True)
    shadowMax = models.IntegerField(null=True)
    shadowWarning = models.IntegerField(null=True)
    loginShell = models.CharField(max_length=254)
    uidNumber = models.IntegerField(null=True)
    gidNumber = models.IntegerField(null=True)
    homeDirectory = models.CharField(max_length=254)
    gecos = models.CharField(max_length=254)
    mail = models.EmailField(max_length=254)
    l = models.CharField(max_length=254)
    telephoneNumber = models.CharField(max_length=254)

def create_user_profile(sender, instance, created, **kwargs):
    #if created:
    #    UserProfile.objects.create(user=instance)
    UserProfile.objects.get_or_create(user=instance)

post_save.connect(create_user_profile, sender=User)

views.py:

from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect
from django.shortcuts import render
from myapp.models import UserProfile

@login_required
def userinfo(request):
    try:
        ldapuserprofile = UserProfile.objects.get(uid=request.user.username)
    except UserProfile.DoesNotExist:
        return HttpResponseRedirect('/login/')
    context = {'request': request, 'ldapuser': ldapuserprofile,}
    return render(request, 'myapp/userinfo.html', context)

然后在html模板中可以访问{{ ldapuser.givenName }}

也许它对某人有帮助。

谢谢,克里斯

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-23
    • 1970-01-01
    相关资源
    最近更新 更多