2016-06-05 2 views
-1

몇 명의 사용자와 2 개의 그룹으로 ModelBackend (기본값)로 Django-1.7.7을 실행하고 있습니다.Django ldap 사용자를 Django ModelBackend 그룹에 추가하는 방법

이제 모델 백엔드에서도 Ldap Backend를 구현했습니다. 하지만 그 후에는 사용자가 Authenticated 할 때 모든 Ldap Authenticated 사용자를 Model 그룹 중 하나에 자동으로 추가해야합니다.

이것을 달성 할 방법이 있습니까? 이 패키지에서

답변

0

당신은 장고 LDAP 기능을 사용하여 모델에서 사용자를 저장 피할 수 있습니다. 해당 사용자의 자격 증명이 올 바르고 올바른 작업을 수행 할 수 있다면 LDAP 서버에 "요청"해야합니다. 사용자 이름을 변수 세션에 저장하고 모든 페이지의 특정 페이지로 리디렉션하고 사용자 이름과의 가변 세션

--SETTINGS.PY--

# LDAP Configuration. 
import ldap 
from django_auth_ldap.config import LDAPSearch 

AUTHENTICATION_BACKENDS = (
    'django_auth_ldap.backend.LDAPBackend', 
    'django.contrib.auth.backends.ModelBackend', 
) 

# Binding and connection options. 

# Address (by IP or Hostname) of LDAP Server (Directory Active) 
AUTH_LDAP_SERVER_URI = "ldap://xxx.xxx.x.xxx" 
# DN of user through we bind to LDAP Server. 
AUTH_LDAP_BIND_DN = "CN=xxx, CN=xxx, DC=xxx, DC=xxx" 
# Password of user through we bind to LDAP Server. 
AUTH_LDAP_BIND_PASSWORD = "xxxxxx" 
# Node where we start to search users. Use to be DN (of random user) without the last one parameter. 
AUTH_LDAP_USER_SEARCH = LDAPSearch("CN=xxx, DC=xxx, DC=xxx", ldap.SCOPE_SUBTREE, "(samAccountName=%(user)s)") 

그럼 당신은 귀하의 의견에서 사용할 수 ..., 등등 정확하고, 검사를위한 특정 사용자가 존재 :

- -VIEWS.PY--

con = ldap.initialize("ldap://ldapserver") 
con.simple_bind_s(userDN, passwordUser) 
filter = '(sAMAccountName=' + "loginName" + ')' 
user = con.search_s(base_dn, ldap.SCOPE_SUBTREE, filter, attrs) 
con.unbind()