我从Django复制了RemoteUserBackend类并对其进行了修改,以便在继续进行身份验证之前检查连接的remote_user是否在特定的LDAP组中。如果用户在Django的数据库中填充用户之前从未登录过。我正在试图弄清楚如何设置first_name,last_name和email字段以及制作用户的东西和superadmin。这是我的代码。它是RemoteUserBackend类的一部分。我做错了什么。 LDAP查找很好,但用户不会更改。
def configure_user(self, user):
"""
Configures a user after creation and returns the updated user.
By default, returns the user unmodified.
"""
con = ldap.initialize(settings.AUTH_LDAP_SERVER_URI)
bind_dn = settings.AUTH_LDAP_BIND_DN
bind_pw = settings.AUTH_LDAP_BIND_PASSWORD
con.simple_bind_s(bind_dn, bind_pw)
base_dn = settings.AUTH_LDAP_USER_DN
attrs = ['givenName', 'sn', 'mail']
filter = '(SAMAccountName={0})'.format(user.username)
ldap_user = con.search_s(base_dn, ldap.SCOPE_SUBTREE, filter, attrs)
givenName = ldap_user[0][1]['givenName'][0].decode('utf-8')
sn = ldap_user[0][1]['sn'][0].decode('utf-8')
mail = ldap_user[0][1]['mail'][0].decode('utf-8')
user.is_staff = True
user.is_superadmin = True
user.first_name = givenName
user.last_name = sn
user.email = mail
user.save()
return user
答案 0 :(得分:0)
发现我的问题......有点尴尬......
应该是user.is_superuser = True,而不是user.is_superadmin = True