3

我正在尝试使 django_auth_ldap 与自定义用户模型一起使用。经过一番挣扎后,我用一个模型创建了新应用程序:

class MyUser(django.contrib.auth.models.AbstractUser):
    uidNumber = models.IntegerField(unique=True)

在 settings.py 我有:

AUTH_USER_MODEL = 'test_ldap.MyUser' 
AUTH_LDAP_ALWAYS_UPDATE_USER = False
AUTH_LDAP_USER_ATTR_MAP = {"first_name": "firstName", "last_name": "sn", "email": "mail", "uidNumber": "uidNumber"}
AUTH_LDAP_PROFILE_ATTR_MAP = {"home_directory": "homeDirectory", "uidNumber": "uidNumber"}
AUTH_LDAP_BIND_DN = ""
AUTH_LDAP_BIND_PASSWORD = ""
AUTH_LDAP_USER_SEARCH = LDAPSearch("ou=People,ou=Main,o=TECH",
    ldap.SCOPE_SUBTREE, "(uid=%(user)s)")

作为登录页面:

urlpatterns += patterns('', 
     url(r'^login/$', 'django.contrib.auth.views.login', {
    'template_name': 'login.tmpl'
     }),
    (r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/'}),
)

manage.py syncdb 成功。

登录后,我的NOT NULL 约束失败: test_ldap_myuser.uidNumber。看起来 LDAP 后端正在尝试仅使用默认类用户数据保存用户。

Environment:


Request Method: POST
Request URL: http://localhost:8000/login/

Django Version: 1.6.5
Python Version: 2.7.5
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'test_ldap')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/home/moonwolf/Dokumenty/projects/python/virtualenv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
112.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/moonwolf/Dokumenty/projects/python/virtualenv/lib/python2.7/site-packages/django/views/decorators/debug.py" in sensitive_post_parameters_wrapper
75.             return view(request, *args, **kwargs)
File "/home/moonwolf/Dokumenty/projects/python/virtualenv/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
99.                     response = view_func(request, *args, **kwargs)
File "/home/moonwolf/Dokumenty/projects/python/virtualenv/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
52.         response = view_func(request, *args, **kwargs)
File "/home/moonwolf/Dokumenty/projects/python/virtualenv/lib/python2.7/site-packages/django/contrib/auth/views.py" in login
36.         if form.is_valid():
File "/home/moonwolf/Dokumenty/projects/python/virtualenv/lib/python2.7/site-packages/django/forms/forms.py" in is_valid
129.         return self.is_bound and not bool(self.errors)
File "/home/moonwolf/Dokumenty/projects/python/virtualenv/lib/python2.7/site-packages/django/forms/forms.py" in errors
121.             self.full_clean()
File "/home/moonwolf/Dokumenty/projects/python/virtualenv/lib/python2.7/site-packages/django/forms/forms.py" in full_clean
274.         self._clean_form()
File "/home/moonwolf/Dokumenty/projects/python/virtualenv/lib/python2.7/site-packages/django/forms/forms.py" in _clean_form
300.             self.cleaned_data = self.clean()
File "/home/moonwolf/Dokumenty/projects/python/virtualenv/lib/python2.7/site-packages/django/contrib/auth/forms.py" in clean
189.                                            password=password)
File "/home/moonwolf/Dokumenty/projects/python/virtualenv/lib/python2.7/site-packages/django/contrib/auth/__init__.py" in authenticate
49.             user = backend.authenticate(**credentials)
File "/home/moonwolf/Dokumenty/projects/python/virtualenv/lib/python2.7/site-packages/django_auth_ldap/backend.py" in authenticate
158.         user = ldap_user.authenticate(password)
File "/home/moonwolf/Dokumenty/projects/python/virtualenv/lib/python2.7/site-packages/django_auth_ldap/backend.py" in authenticate
330.             self._get_or_create_user()
File "/home/moonwolf/Dokumenty/projects/python/virtualenv/lib/python2.7/site-packages/django_auth_ldap/backend.py" in _get_or_create_user
529.         self._user, created = self.backend.get_or_create_user(username, self)
File "/home/moonwolf/Dokumenty/projects/python/virtualenv/lib/python2.7/site-packages/django_auth_ldap/backend.py" in get_or_create_user
223.         return model.objects.get_or_create(**kwargs)
File "/home/moonwolf/Dokumenty/projects/python/virtualenv/lib/python2.7/site-packages/django/db/models/manager.py" in get_or_create
154.         return self.get_queryset().get_or_create(**kwargs)
File "/home/moonwolf/Dokumenty/projects/python/virtualenv/lib/python2.7/site-packages/django/db/models/query.py" in get_or_create
391.                     six.reraise(*exc_info)
File "/home/moonwolf/Dokumenty/projects/python/virtualenv/lib/python2.7/site-packages/django/db/models/query.py" in get_or_create
383.                     obj.save(force_insert=True, using=self.db)
File "/home/moonwolf/Dokumenty/projects/python/virtualenv/lib/python2.7/site-packages/django/db/models/base.py" in save
545.                        force_update=force_update, update_fields=update_fields)
File "/home/moonwolf/Dokumenty/projects/python/virtualenv/lib/python2.7/site-packages/django/db/models/base.py" in save_base
573.             updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/moonwolf/Dokumenty/projects/python/virtualenv/lib/python2.7/site-packages/django/db/models/base.py" in _save_table
654.             result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/home/moonwolf/Dokumenty/projects/python/virtualenv/lib/python2.7/site-packages/django/db/models/base.py" in _do_insert
687.                                using=using, raw=raw)
File "/home/moonwolf/Dokumenty/projects/python/virtualenv/lib/python2.7/site-packages/django/db/models/manager.py" in _insert
232.         return insert_query(self.model, objs, fields, **kwargs)
File "/home/moonwolf/Dokumenty/projects/python/virtualenv/lib/python2.7/site-packages/django/db/models/query.py" in insert_query
1514.     return query.get_compiler(using=using).execute_sql(return_id)
File "/home/moonwolf/Dokumenty/projects/python/virtualenv/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
903.             cursor.execute(sql, params)
File "/home/moonwolf/Dokumenty/projects/python/virtualenv/lib/python2.7/site-packages/django/db/backends/util.py" in execute
69.             return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/moonwolf/Dokumenty/projects/python/virtualenv/lib/python2.7/site-packages/django/db/backends/util.py" in execute
53.                 return self.cursor.execute(sql, params)
File "/home/moonwolf/Dokumenty/projects/python/virtualenv/lib/python2.7/site-packages/django/db/utils.py" in __exit__
99.                 six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/moonwolf/Dokumenty/projects/python/virtualenv/lib/python2.7/site-packages/django/db/backends/util.py" in execute
53.                 return self.cursor.execute(sql, params)
File "/home/moonwolf/Dokumenty/projects/python/virtualenv/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py" in execute
451.         return Database.Cursor.execute(self, query, params)

Exception Type: IntegrityError at /login/
Exception Value: NOT NULL constraint failed: test_ldap_myuser.uidNumber

所以我尝试使用文档中描述的populate_user信号:

@receiver(django_auth_ldap.backend.populate_user)
    def ldap_populate_user_receiver(sender, user=None, ldap_user=None, **kwargs):
        print "Signal received"
        # set uidNumber accordingly

(我把它放在models.py中)

但是这个函数没有输出(它根本没有被调用)。当我将用户模型改回默认用户时,一切正常(并且出现信号输出 - 在这种情况下,当然不需要更改任何数据)。看起来 django 试图将数据写入数据库 (get_or_create_user()) 或其他什么?

我究竟做错了什么?

4

1 回答 1

0

我设法解决了这个问题。我使用了默认的 auth.User 并添加了指向它的 ForeignKey 的模型。在这个模型中,我将拥有我需要的额外属性。通过与 LDAP 后端的一些同步(例如在创建新用户的情况下),这应该可以工作。

于 2014-06-10T10:51:55.580 回答