【问题标题】:Django Admin, how to hide change password?Django Admin,如何隐藏更改密码?
【发布时间】: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


    【解决方案1】:

    我终于找到了解决办法。覆盖 'LDAPBackend' 确实是必要的,但还不够。它删除了仅用红色标记的“更改密码”(右上角):

    另一个“更改密码”区域(下图中用蓝色标记)保持不变:

    要摆脱“蓝色”区域,我们需要像这样覆盖模板:

    {% extends "admin/change_form.html" %}
    {% load i18n %}
    
    {% block field_sets %}
    {% for fieldset in adminform %}
    
      {% if forloop.first and user.has_usable_password %}
        {% include "admin/includes/fieldset.html" %}
      {% endif %}
     
      {% if not forloop.first %}
         {% include "admin/includes/fieldset.html" %}
      {% endif %}
    
    {% endfor %}
    {% endblock %}
    

    这个覆盖模板 html 需要被称为 change_form.html 并且需要像下面这样放置(红色圆圈):

    【讨论】:

      猜你喜欢
      • 2013-03-05
      • 2019-12-09
      • 2014-12-19
      • 1970-01-01
      • 2021-11-15
      • 1970-01-01
      • 2011-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多