1

I'm using django-auth-ldap for my django auth, with on-the-fly user creation.

In my settings.py

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

AUTH_LDAP_USER_SEARCH = LDAPSearch(
    "ou=clients,dc=oasiswork,dc=fr",
    ldap.SCOPE_SUBTREE,
    "(mail=%(user)s)")

AUTH_LDAP_USER_ATTR_MAP = {
    "username"  : "mail",
    "id"        : "uid",
    "first_name": "givenName",
    "last_name" : "sn",
    "mail"      : "mail"
}

Now my test case is :

LDAPBackend().populate_user('test1@example.net')
LDAPBackend().populate_user('test2@example.net')

Yes, I do want to set the IDs from the ldap.

I have only one user, from standard user model, with an ID that has no possible colision with the LDAP's.

The thing is if I don't put the "id" : "uid", line in settings, my test case goes fine.

However, if I keep this line, it fails at first user creation:

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/jocelyn/.virtualenvs/zimsoap/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/home/jocelyn/.virtualenvs/zimsoap/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/jocelyn/.virtualenvs/zimsoap/local/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/jocelyn/.virtualenvs/zimsoap/local/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/home/jocelyn/dev/honcho/syncldap/management/commands/import-ldap-users.py", line 28, in handle
    user = backend.populate_user(attrs['mail'][0])
  File "/home/jocelyn/.virtualenvs/zimsoap/local/lib/python2.7/site-packages/django_auth_ldap/backend.py", line 190, in populate_user
    user = ldap_user.populate_user()
  File "/home/jocelyn/.virtualenvs/zimsoap/local/lib/python2.7/site-packages/django_auth_ldap/backend.py", line 365, in populate_user
    self._get_or_create_user(force_populate=True)
  File "/home/jocelyn/.virtualenvs/zimsoap/local/lib/python2.7/site-packages/django_auth_ldap/backend.py", line 549, in _get_or_create_user
    self._user.save()
  File "/home/jocelyn/.virtualenvs/zimsoap/local/lib/python2.7/site-packages/django/db/models/base.py", line 545, in save
    force_update=force_update, update_fields=update_fields)
  File "/home/jocelyn/.virtualenvs/zimsoap/local/lib/python2.7/site-packages/django/db/models/base.py", line 573, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/home/jocelyn/.virtualenvs/zimsoap/local/lib/python2.7/site-packages/django/db/models/base.py", line 654, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/home/jocelyn/.virtualenvs/zimsoap/local/lib/python2.7/site-packages/django/db/models/base.py", line 687, in _do_insert
    using=using, raw=raw)
  File "/home/jocelyn/.virtualenvs/zimsoap/local/lib/python2.7/site-packages/django/db/models/manager.py", line 232, in _insert
    return insert_query(self.model, objs, fields, **kwargs)
  File "/home/jocelyn/.virtualenvs/zimsoap/local/lib/python2.7/site-packages/django/db/models/query.py", line 1511, in insert_query
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/home/jocelyn/.virtualenvs/zimsoap/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 898, in execute_sql
    cursor.execute(sql, params)
  File "/home/jocelyn/.virtualenvs/zimsoap/local/lib/python2.7/site-packages/django/db/backends/util.py", line 69, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/jocelyn/.virtualenvs/zimsoap/local/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)
  File "/home/jocelyn/.virtualenvs/zimsoap/local/lib/python2.7/site-packages/django/db/utils.py", line 99, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/jocelyn/.virtualenvs/zimsoap/local/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)
  File "/home/jocelyn/.virtualenvs/zimsoap/local/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 450, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: column username is not unique

Which seems quite unrelated to what I'm trying to do (not username related, and users provisioning works without the "id" in AUTH_LDAP_USER_ATTR_MAP)

Jocelyn delalande
  • 5,123
  • 3
  • 30
  • 34

0 Answers0