Python-3.4 Django v-1.9: Ldap issue - module object has no attribute SCOPE_SUBTREE

1.7k views Asked by At

I have the following configuration for LDAP, if I use ldap.SCOPE_SUBTREE, it shows:

ldap3.SCOPE_SUBTREE, "(uid=%(user)s)")
AttributeError: 'module' object has no attribute 'SCOPE_SUBTREE'

If I use ldap3.SUBTREE - the error that appears:

File "C:\Python34\lib\site-packages\django_auth_ldap-1.2.13-py3.4.eg\django_auth_ldap\config.py", line 65, in get_ldap
ImportError: No module named 'ldap.filter' in 

[LDAP-Error.docx](https://github.com/cannatag/ldap3/files/1280858/LDAP-Error.docx)

Code:

#Specify User Profile Model if profile needs to be populated (Optional)
#AUTH_PROFILE_MODULE = 'account.UserProfile'

#Baseline configuration.
AUTH_LDAP_SERVER_URI = "ldap://server ip"

AUTH_LDAP_BIND_DN = "[email protected]"
AUTH_LDAP_BIND_PASSWORD = "xxxxxxxxxx"
AUTH_LDAP_USER_SEARCH = LDAPSearch("ou=users,dc=example,dc=com",
    ldap3.SUBTREE, "(uid=%(user)s)")

#AUTH_LDAP_USER_DN_TEMPLATE = "uid=%(user)s,ou=users,dc=example,dc=com"

# Set up the basic group parameters.

#AUTH_LDAP_GROUP_SEARCH = LDAPSearch("ou=users,dc=example",
#       ldap3.SUBTREE,"(objectClass=posixGroup)"

#)
#LDAP_AUTH_OBJECT_CLASS = "inetOrgPerson"
#AUTH_LDAP_GROUP_TYPE = PosixGroupType()
#group = PUN-GLOBALSUPPORT
# Simple group restrictions
#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"

#Populate the Django user from the LDAP directory.
AUTH_LDAP_USER_ATTR_MAP = {
    "first_name": "givenName",
    "last_name": "sn",
    "email": "mail"
}

AUTH_LDAP_PROFILE_ATTR_MAP = {
    "employee_number": "employeeNumber"
}

AUTH_LDAP_USER_FLAGS_BY_GROUP = {
    "is_active": "cn=active,ou=groups,dc=PUN",
    "is_staff": "cn=staff,ou=groups,dc=PUN",
    "is_superuser": "cn=superuser,ou=groups,dc=PUN"
}

AUTH_LDAP_PROFILE_FLAGS_BY_GROUP = {
    "is_awesome": ["cn=awesome,ou=groups,dc=PUN"]
}

#AUTH_LDAP_USER_SEARCH = LDAPSearch("ou=users,dc=PUN",
#    ldap.SCOPE_SUBTREE, "(cn=%(user)s)")
# This is the default, but I like to be explicit.
AUTH_LDAP_ALWAYS_UPDATE_USER = True

# Use LDAP group membership to calculate group permissions.
#AUTH_LDAP_FIND_GROUP_PERMS = True

# Cache group memberships for an hour to minimize LDAP traffic
AUTH_LDAP_CACHE_GROUPS = True
AUTH_LDAP_GROUP_CACHE_TIMEOUT = 3600


# Keep ModelBackend around for per-user permissions and maybe a local
#Configuration for LDAP
# superuser.
AUTHENTICATION_BACKENDS = (
    'django_python3_ldap.auth.LDAPBackend',
    'django_auth_ldap.backend.LDAPBackend',
    'django.contrib.auth.backends.ModelBackend',
)

#Enable Logging method 1
LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "handlers": {
        "console": {
            "class": "logging.StreamHandler",
        },
    },
    "loggers": {
        "django_python3_ldap": {
            "handlers": ["console"],
            "level": "INFO",
        },
    },
}
##Enable Logging method 2. Enable debug for ldap server connection
#logger = logging.getLogger('django_auth_ldap')
#logger.addHandler(logging.StreamHandler())
#logger.setLevel(logging.DEBUG)
1

There are 1 answers

1
SmallChess On

I had the same problem but was able to fix it perfectly.

You need to copy the __init__.py of the ldap package for python2 into the package for python3.

Find where your ldap package for python2 is, let's say /site-packages/python2/ldap. Let's also say the same package for python3 is /site-packages/python3/ldap

Do this:

cp /site-packages/python2/ldap/__init__.py /site-packages/python3/ldap