Lucterios2 / django_auth_ldap3_ad

Simple LDAP/AD auth module for django
https://pypi.python.org/pypi/django-auth-ldap3-ad
GNU General Public License v3.0
45 stars 25 forks source link

Duplicate entry 'USERS.NAME' for key 'username' #12

Closed jeffclay closed 7 years ago

jeffclay commented 7 years ago

I'm getting the error about a duplicate entry (Duplicate entry 'USERS.NAME' for key 'username') if I try to login after having already logged in once and populated the db with my user name. If I delete the user row from the auto_user table then it will let me log in again.

I've also noticed that none of the AD groups are being created in the django db. I'm not sure if this is expected behavior or not; I'm very new to django.

Here's my config:

LDAP_ENGINE = 'AD'

LDAP_BIND_USER = "DN FOR ACCOUNT"
LDAP_BIND_PWD = "PASSWORD"

LDAP_SEARCH_BASE = "DN FOR LOCATION"
LDAP_USER_SEARCH_FILTER = "(&(sAMAccountName=%s)(objectClass=user))"

LDAP_ATTRIBUTES_MAP = {
    'username': 'sAMAccountName',
    'first_name': 'givenName',
    'last_name': 'sn',
    'email': 'mail',
}

LDAP_STORE_USER_DN = True

LDAP_SERVERS = [
    {
        'host': 'IP.ADDR',
        'port': 389,
        'use_ssl': False,
    },
    {
        'host': 'IP.ADDR',
        'port': 389,
        'use_ssl': False,
    },
]

LDAP_USE_LDAP_GROUPS = True
LDAP_GROUPS_SEARCH_BASE = "DN FOR LOCATION"
LDAP_GROUPS_SEARCH_FILTER = "(&(objectClass=group))"
LDAP_GROUP_MEMBER_ATTRIBUTE = "member"
LDAP_SUPERUSER_GROUPS = ["DN FOR ACCOUNT", ]
LDAP_STAFF_GROUPS = ["DN FOR ACCOUNT", ]
LDAP_GROUPS_MAP = {
    'my_admins': "DN FOR ACCOUNT",
    'my_users': "DN FOR ACCOUNT",
}
weissglut-dev commented 7 years ago

Hi,

this could be solved in the latest master version.

With https://github.com/Lucterios2/django_auth_ldap3_ad/commit/45e5e9519908e5ea2f1b87942a8751fa4f9ea79f I changed the user lookup to use the username field from LDAP_ATTRIBUTES_MAP['username'] as value.

But additionally you should set the LDAP_USER_MODEL_USERNAME_FIELD to identify the field to use for lookup.

hth Seyhbold

povtux commented 7 years ago

Hi,

The last version has be updated in github & pypi. You can now update you module via pip and apply the advice of Seyhbold.

Then, please tell us if that solves you problem or not.

Regards.

R4PS commented 6 years ago

Ive got the same error but i fixed it by converting the list to a string

https://github.com/Lucterios2/django_auth_ldap3_ad/blob/c5ef08c309e4ba8c6f22fef167c38124dd83a963/django_auth_ldap3_ad/auth.py#L163-L164

lookup_username = user_attribs[settings.LDAP_ATTRIBUTES_MAP[username_field]]
lookup_username = "".join(lookup_username)
usr = user_model.objects.get(**{"{0}__iexact".format(username_field): lookup_username})