【发布时间】:2021-12-30 12:31:22
【问题描述】:
我的 Django 应用程序的一些用户来自 LDAP 服务器。这些用户不能更改他们的 django-admin 密码。最好的,如果他们甚至没有的话。
这就是为什么我像这样子类django_auth_ldap.backend.LDAPBackend:
from django_auth_ldap.backend import LDAPBackend
from django.contrib.auth import get_user_model
class CustomLDAPBackend(LDAPBackend)
def authenticate_ldap_user(self, ldap_user, password):
user = ldap_user.authenticate(password)
print("BEFORE set_unusable_password(): ", user.has_usable_password())
user.set_unusable_password()
user.save()
print("AFTER set_unusable_password(): ", user.has_usable_password())
return user
user.set_unusable_password() 我试图隐藏密码,因为我在几个地方读过它(这里是 SO 和 docs)。但我能做到的就是没有设置密码:
此外,如果我多次登录,print("BEFORE set_unusable_password(): ", user.has_usable_password()) 的结果始终为 True,尽管调用了 user.set_unusable_password() 并保存了用户。好像每次都会创建一个新的用户对象。
This question does not solve my problem 因为user.set_unusable_password() 显然没有隐藏密码更改区域。
我错过了什么?如何隐藏“更改密码”区域?
下面是settings.py中与LDAP相关的部分:
import ldap
from django_auth_ldap.config import LDAPSearch, LDAPGroupQuery,GroupOfNamesType,PosixGroupType
AUTH_LDAP_SERVER_URI = 'ldap://localhost'
AUTH_LDAP_BIND_DN = 'cn=admin,dc=example,dc=com'
AUTH_LDAP_BIND_PASSWORD = 'secret'
AUTH_LDAP_USER_SEARCH = LDAPSearch('dc=example,dc=com',ldap.SCOPE_SUBTREE, '(uid=%(user)s)')
AUTH_LDAP_GROUP_SEARCH = LDAPSearch('dc=example,dc=com',ldap.SCOPE_SUBTREE, '(objectClass=top)')
AUTH_LDAP_GROUP_TYPE = PosixGroupType(name_attr="cn")
AUTH_LDAP_MIRROR_GROUPS = True
# Populate the Django user from the LDAP directory.
AUTH_LDAP_REQUIRE_GROUP = "cn=enabled,ou=groups,dc=example,dc=com"
AUTH_LDAP_USER_ATTR_MAP = {
"first_name": "givenName",
"last_name": "sn",
"email": "mail",
"username": "uid",
"password": "userPassword",
}
AUTH_LDAP_PROFILE_ATTR_MAP = {
"home_directory": "homeDirectory"
}
AUTH_LDAP_USER_FLAGS_BY_GROUP = {
"is_active": "cn=active,ou=groups,dc=example,dc=com",
"is_staff": "cn=staff,ou=groups,dc=example,dc=com",
"is_superuser": "cn=superuser,ou=groups,dc=example,dc=com"
}
AUTH_LDAP_ALWAYS_UPDATE_USER = True
AUTH_LDAP_FIND_GROUP_PERMS = True
AUTH_LDAP_CACHE_TIMEOUT = 3600
AUTH_LDAP_FIND_GROUP_PERMS = True
# Keep ModelBackend around for per-user permissions and maybe a local
# superuser.
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'ldappro.backend_ldap.CustomLDAPBackend',
)
【问题讨论】:
标签: django authentication ldap django-admin django-users