1

我正在使用 Django-auth-LDAP 进行身份验证,现在我想获取以下字段的数据:

AUTH_LDAP_BIND_DN、AUTH_LDAP_BIND_PASSWORD、AUTH_LDAP_SERVER_URI

从数据库,但我得到一个错误。

设置.py

import subprocess
from django.apps import apps
from django.core.exceptions import ObjectDoesNotExist
ldap = False
Config = apps.get_model('config', 'Config')
Config = Config()
try:
    ldap = Config.objects.get(name="ldap")
except ObjectDoesNotExist:
    pass
if ldap and check_ldap_connection():
    import ldap
    from django_auth_ldap.config import LDAPSearch
    AUTH_LDAP_SERVER_URI = Config.objects.get(name="ldap_server_uri")

    AUTH_LDAP_BIND_DN = Config.objects.get(name="ldap_bind_dn")
    AUTH_LDAP_BIND_PASSWORD = Config.objects.get(name="ldap_bind_password")
    ldap_search = Config.objects.get(name="ldap_search")
    AUTH_LDAP_USER_SEARCH = LDAPSearch(
        ldap_search , ldap.SCOPE_SUBTREE, "(uid=%(user)s)"
    )

def check_ldap_connection():
    try:
        ldap_server_uri = Config.objects.get(name="ldap_server_uri")
        ldap_bind_dn = Config.objects.get(name="ldap_bind_dn")
        ldap_search = Config.objects.get(name="ldap_search")
        ldap_bind_password = Config.objects.get(name="ldap_bind_password")
    except ObjectDoesNotExist:
        return False
    cmd = "ldapsearch -H \"" + ldap_server_uri + "\" -D \"" + ldap_bind_dn + "\" -w \"" +  ldap_bind_password \
          + "\" -b \"" + ldap_search + "\" | " + "grep result"
    try:
        connection = ""
        connection = subprocess.check_output(cmd, shell=True).decode()
    except Exception as e:
        return False
        connection = connection.split()
        if "0" and "Success" in connection:
            return True
        return False

错误:

raise AppRegistryNotReady("模型尚未加载。") django.core.exceptions.AppRegistryNotReady:模型尚未加载。

4

0 回答 0