1

我正在使用 django-auth-ldap 进行我的 django 身份验证,并即时创建用户。

在我的 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"
}

现在我的测试用例是:

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

是的,我确实想从 ldap 设置 ID。

我只有一个来自标准用户模型的用户,其 ID 与 LDAP 没有可能的冲突。

问题是,如果我不把这"id" : "uid",条线放在设置中,我的测试用例就会很好。

但是,如果我保留这条线,它会在第一次创建用户时失败:

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

这似乎与我正在尝试做的事情完全无关(与用户名无关,并且用户配置在没有“id”的情况下工作AUTH_LDAP_USER_ATTR_MAP

4

0 回答 0