【问题标题】:django active directory backend no attribute __getitem__django活动目录后端没有属性__getitem__
【发布时间】:2016-05-18 11:59:42
【问题描述】:

我一直在尝试使用在这里找到的 sn-p https://djangosnippets.org/snippets/2899/ 来拥有一个 ldap 后端,但是当我现在登录到我的管理页面时,我得到了错误

TypeError at /admin/login/
'NoneType' object has no attribute '__getitem__'
Request Method: POST
Request URL:    http://it.intranet.com/admin/login/?next=/admin/
Django Version: 1.9.6
Exception Type: TypeError
Exception Value:    
'NoneType' object has no attribute '__getitem__'
Exception Location: /var/www/infternal/infternal/backend.py in get_or_create_user, line 69
Python Executable:  /usr/bin/python
Python Version: 2.7.5
Python Path:    
['/var/www/infternal',
 '/usr/lib64/python27.zip',
 '/usr/lib64/python2.7',
 '/usr/lib64/python2.7/plat-linux2',
 '/usr/lib64/python2.7/lib-tk',
 '/usr/lib64/python2.7/lib-old',
 '/usr/lib64/python2.7/lib-dynload',
 '/usr/lib64/python2.7/site-packages',
 '/usr/lib64/python2.7/site-packages/gtk-2.0',
 '/usr/lib/python2.7/site-packages']
Server time:    Wed, 18 May 2016 11:54:09 +0000

我想这是因为我没有成功连接到 ldap 服务器?

我该如何测试?

后端代码完全从 sn-p 复制而来。 我的 settings.py 如下

我唯一不确定的是 AD_CERT_FILE 字段,我不知道它在哪里,也不知道该放什么,但由于我没有使用 ssl,所以我认为它不需要?

# active directory authentication module
AD_DNS_NAME = 'example.domain.com'   # FQDN of your DC (using just the Domain Name to utilize all DC's)
# If using non-SSL use these
AD_LDAP_PORT=389
AD_LDAP_URL='ldap://%s:%s' % (AD_DNS_NAME,AD_LDAP_PORT)
# If using SSL use these:
#AD_LDAP_PORT=636
#AD_LDAP_URL='ldaps://%s:%s' % (AD_DNS_NAME,AD_LDAP_PORT)
AD_SEARCH_DN = 'DC=example,DC=domain,DC=com'
AD_NT4_DOMAIN = 'example.domain.COM'
AD_SEARCH_FIELDS = ['mail','givenName','sn','sAMAccountName','memberOf']
AD_MEMBERSHIP_ADMIN = ['ITService_App_Admin']   # this ad group gets superuser status in django
# only members of this group can access
AD_MEMBERSHIP_REQ = AD_MEMBERSHIP_ADMIN + ['GS_ITsupport',
                                           'GS_ITDevelopment',]
AD_CERT_FILE = '/certs/certfile'    # this is the certificate of the Certificate Authority issuing your DCs certificate
AD_DEBUG=True #Set to false for prod, Slows things down ALOT
AD_DEBUG_FILE='/tmp/ldap.debug'


AUTHENTICATION_BACKENDS = (
    'infternal.backend.ActiveDirectoryAuthenticationBackend',
    'django.contrib.auth.backends.ModelBackend', #Comment out to prevent authentication from DB
)    

【问题讨论】:

  • 在 Google Hangouts 上给我打电话,也许我可以帮你弄清楚。

标签: python django active-directory


【解决方案1】:

这不是你的错,你得到的 TypeError 信息不是很丰富。

这个特殊的 sn-p 要求您在 Django 中镜像相关的 AD 组(您必须使用管理员在 Django 中创建相同的组)。这些组不应具有相同的名称,而是使用以下约定(来自源中的注释):

我们的 AD 组与 Django 组镜像,但以“ID”开头(注意空格)

如果您的 Django 组命名为“ITsupport”,那么您的 AD 组必须命名为“ID ITsupport”等等。

如果要修改此行为,则必须在第 142 行更改正则表达式:

re.compile(r'^CN=ID (?P<groupName>[\w|\d|\s]+),')

例如:

re.compile(r'^CN=(?P<groupName>MyADGroup|MyOtherAdGroup|AndSoOn),')

只需将原始表达式替换为您的 AD 组列表,并用竖线 (|) 分隔。您仍然必须使用 Django 管理员或 shell 创建组。

【讨论】:

    猜你喜欢
    • 2015-04-19
    • 2017-01-16
    • 2014-08-10
    • 1970-01-01
    • 2014-05-21
    • 2010-12-18
    • 2013-05-12
    • 1970-01-01
    • 2013-03-17
    相关资源
    最近更新 更多