# 示例配置
下面是`settings.py`中一個完整的示例配置,它可以執行幾乎所有的功能。 在這個例子中,我們正在對目錄中的全局用戶池進行身份驗證,但是我們為`Django`組(`ou = django,ou = groups,dc = example,dc = com`)留出了一個特殊區域。 請記住,如果您只需要簡單的身份驗證,則大部分都是可選的。 包括一些默認設置和參數的完整性。
~~~
import ldap
from django_auth_ldap.config import LDAPSearch, GroupOfNamesType
# 基準配置。
AUTH_LDAP_SERVER_URI = "ldap://ldap.example.com"
AUTH_LDAP_BIND_DN = "cn=django-agent,dc=example,dc=com"
AUTH_LDAP_BIND_PASSWORD = "phlebotinum"
AUTH_LDAP_USER_SEARCH = LDAPSearch("ou=users,dc=example,dc=com",
ldap.SCOPE_SUBTREE, "(uid=%(user)s)")
# 也許:
# AUTH_LDAP_USER_DN_TEMPLATE = "uid=%(user)s,ou=users,dc=example,dc=com"
# 設置基本組參數。
AUTH_LDAP_GROUP_SEARCH = LDAPSearch("ou=django,ou=groups,dc=example,dc=com",
ldap.SCOPE_SUBTREE, "(objectClass=groupOfNames)"
)
AUTH_LDAP_GROUP_TYPE = GroupOfNamesType(name_attr="cn")
# 簡單的組限制
AUTH_LDAP_REQUIRE_GROUP = "cn=enabled,ou=django,ou=groups,dc=example,dc=com"
AUTH_LDAP_DENY_GROUP = "cn=disabled,ou=django,ou=groups,dc=example,dc=com"
# 從LDAP目錄填充Django用戶。
AUTH_LDAP_USER_ATTR_MAP = {
"first_name": "givenName",
"last_name": "sn",
"email": "mail"
}
AUTH_LDAP_USER_FLAGS_BY_GROUP = {
"is_active": "cn=active,ou=django,ou=groups,dc=example,dc=com",
"is_staff": "cn=staff,ou=django,ou=groups,dc=example,dc=com",
"is_superuser": "cn=superuser,ou=django,ou=groups,dc=example,dc=com"
}
# 這是默認的,但我喜歡明確。
AUTH_LDAP_ALWAYS_UPDATE_USER = True
# 使用LDAP組成員資格來計算組權限。
AUTH_LDAP_FIND_GROUP_PERMS = True
# 緩存一小時的組成員身份以最大限度地減少LDAP流量
AUTH_LDAP_CACHE_GROUPS = True
AUTH_LDAP_GROUP_CACHE_TIMEOUT = 3600
# 保持ModelBackend為每個用戶的權限,也許本地超級用戶。
AUTHENTICATION_BACKENDS = (
'django_auth_ldap.backend.LDAPBackend',
'django.contrib.auth.backends.ModelBackend',
)
~~~