gpt4 book ai didi

python - 在 Django 中从外部 Active Directory 提取数据

转载 作者:太空宇宙 更新时间:2023-11-03 16:37:54 24 4
gpt4 key购买 nike

我目前有一个使用 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 并以这种方式检索信息?

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

最佳答案

我管理的 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)

关于python - 在 Django 中从外部 Active Directory 提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37063476/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com