2016-05-06 5 views
1

私は現在、MySQLバックエンドを使用するアプリケーションを持っていますが、そのユーザーにはプロファイル情報が格納されているクライアントがありますが、Active Directoryもあり、そこから情報を取得して特定のプロファイルの情報を取得します。 Djangoに複数のSQLデータベース接続を設定したり、認証バックエンドをActive Directoryに置き換えたりすることができます。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に接続し、情報をそのように取得する必要がある場合、私は思っていました?

これは可能なのですか?もしそうなら、それに近づける最善の方法はありますか?

+0

データベースに格納されているプロファイルをActive Directoryのデータと同期させる管理コマンドを記述できます。それを行う認証バックエンド。または両方。 –

答えて

1

私は管理するDjangoサイトと同様の状況があります。ここで私が使用してDjangoのアプリケーションがあります:

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

それは私が私のデータベースのためのPostgreSQLを使用し、マッピングフィールドでのActive Directoryから出て、ユーザレコードに私が必要とするユーザメタデータをプルすることができます。それは私がいくつかの誤った開始後に見つけた最良の方法です。

のPython 3のパッケージ:あなただけのDjangoのユーザーにActive Directoryからデータを取得していないために探している場合は

は、ここでは、パッケージと、私は仕事に見つけたコードサンプルですgitのを+ https://github.com/rbarrois/[email protected]

:LDAPパッケージを使用してActive Directoryに接続するとき

""" 
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/[email protected] 
""" 

import ldap 

LDAP_URI = 'ldap://ldap.server.com' 
LDAP_DN = 'dc=server,dc=com' 
LDAP_USERNAME = '[email protected]' 
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) 

この2行が不可欠です:DjangoのORMで動作するように変更することができます

例、

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

さて、私はデータをプルして、それをビューで表示するだけです。それでおしまい。 – nastyn8

関連する問題