【问题标题】:Pulling data from external Active Directory in Django从 Django 中的外部 Active Directory 中提取数据
【发布时间】:2016-05-06 02:54:11
【问题描述】:

我目前有一个使用 MySQL 后端的应用程序,我有一个客户端,其中存储了用户的个人资料信息,但他们也有 Active Directory,我想知道我是否也可以从中提取信息以检索信息从那里获得特定的个人资料。我知道您可以为多个 SQL 数据库连接配置 Django,或者将身份验证后端替换为 Active Directory。

https://docs.djangoproject.com/en/1.9/topics/db/multi-db/

https://pythonhosted.org/django-auth-ldap/

但是我想知道我是否可以同时执行 MySQL 和 Active Directory,还是只需要从外部连接到 Active Directory 并以这种方式检索信息?

这是可行的吗?如果可行,最好的方法是什么?

【问题讨论】:

  • 您可以编写一个管理命令,将存储在数据库中的配置文件与 Active Directory 中的数据同步。或将执行此操作的身份验证后端。或两者兼而有之。

标签: python django active-directory ldap


【解决方案1】:

我管理的一个 Django 站点也有类似的情况。这是我使用的 Django 应用程序:

https://github.com/etianen/django-python3-ldap

它允许我将 PostgreSQL 用于我的数据库,并通过映射字段​​将我需要的用户元数据从 Active Directory 中提取到用户记录中。这是我在几次错误开始后找到的最好的方法。

如果您只是想从 Active Directory 中提取数据而不是向 Django 用户中提取数据,这里是我发现可以工作的包和代码示例:

Python 3 包:git+https://github.com/rbarrois/python-ldap.git@py3

示例,您可以对其进行修改以使用 Django 的 ORM:

"""
This code provide an example of how to connect to LDAP (specifically, Active Directory)
using Python 3.

Requires python-ldap3, available via the following command:
pip install git+https://github.com/rbarrois/python-ldap.git@py3
"""

import ldap

LDAP_URI = 'ldap://ldap.server.com'
LDAP_DN = 'dc=server,dc=com'
LDAP_USERNAME = 'ldap_user@server.com'
LDAP_PASSWORD = ''
USER_NAME = 'username-to-test'
USER_IN_GROUP = 'CN=SomeGroup,DC=server,DC=com'
USER_NOT_IN_GROUP = 'CN=SomeGroupThatDoesNotExist,DC=server,DC=com'

try:
    # Connect to LDAP / Active Directory
    ldap_con = ldap.initialize(LDAP_URI)
    ldap_con.protocol_version = 3
    ldap_con.set_option(ldap.OPT_REFERRALS, 0)
    ldap_con.simple_bind_s(LDAP_USERNAME, LDAP_PASSWORD)

    # sAMAAccountName is Active Directory's 'username'
    user_filter='(&(objectCategory=person)(objectClass=user)(sAMAccountName=' + USER_NAME + '))'
    attrs = ['memberOf']

    # Perform the search.
    ldap_user = ldap_con.search_s(LDAP_DN, ldap.SCOPE_SUBTREE, user_filter, attrs)

    # Active Directory returns a list of byte literals. Convert them to strings in a more sensibly named list.
    ldap_groups = []
    for value in ldap_user[0][1]['memberOf']:
        ldap_groups.append(value.decode('utf-8'))

    # Print the LDAP groups the user above is a member of, one per line.
    for value in ldap_groups:
        print(value)

    # Perform check to see whether a user is in a group, or explicitly, a user it not in a group.
    if USER_IN_GROUP in ldap_groups:
         print(USER_NAME + " is a member of " + USER_IN_GROUP)
    else:
         print(USER_NAME + " is not a member of " + USER_IN_GROUP)

    if USER_NOT_IN_GROUP in ldap_groups:
         print(USER_NAME + " is a member of " + USER_NOT_IN_GROUP)
    else:
         print(USER_NAME + " is not a member of " + USER_NOT_IN_GROUP)

    # Unbind from LDAP / Active Directory.
    ldap_con.unbind()
except ldap.LDAPError:
    print(ldap.LDAPError)

在使用 LDAP 包连接到 Active Directory 时,这两行是必不可少的:

ldap_con.protocol_version = 3
ldap_con.set_option(ldap.OPT_REFERRALS, 0)

【讨论】:

  • 是的,我只需要提取数据并将其显示在视图中。就是这样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多