【问题标题】:Modify an Active Directory User in Django在 Django 中修改 Active Directory 用户
【发布时间】:2017-12-27 15:29:02
【问题描述】:

所以我正在尝试修改我的活动目录中的用户。到目前为止,我可以作为 AD 用户登录,但是当我尝试编辑我的个人资料时,它并没有在 AD 中实现。

我将 django-auth-ldap 用于 AD 后端。

我与具有读写权限的用户建立了连接。

AUTH_LDAP_SERVER_URI = "ldap://192.168.1.12"

AUTH_LDAP_BIND_DN = "user"
AUTH_LDAP_BIND_PASSWORD = "password"
AUTH_LDAP_CONNECTION_OPTIONS = {
    ldap.OPT_DEBUG_LEVEL: 1,
    ldap.OPT_REFERRALS: 0
}
AUTH_LDAP_USER_SEARCH = LDAPSearch("DC=sb,DC=ch", ldap.SCOPE_SUBTREE, "(sAMAccountName=%(user)s)")

# Set up the basic group parameters.
AUTH_LDAP_GROUP_SEARCH = LDAPSearch("DC=sb,DC=ch", ldap.SCOPE_SUBTREE, "(objectClass=group)")
AUTH_LDAP_GROUP_TYPE = NestedActiveDirectoryGroupType()


# What to do once the user is authenticated
AUTH_LDAP_USER_ATTR_MAP = {
    "first_name": "givenName",
    "last_name": "sn",
    "email": "mail"
}

AUTH_LDAP_USER_FLAGS_BY_GROUP = {
    "is_active": "CN=ipa-users,cn=users,DC=sb,DC=ch",
    "is_staff": "CN=ipa-users,cn=users,DC=sb,DC=ch",
    "is_superuser": "CN=ipa-users,cn=users,DC=sb,DC=ch"
}

# This is the default, but be explicit.
AUTH_LDAP_ALWAYS_UPDATE_USER = True

# Use LDAP group membership to calculate group permissions.
AUTH_LDAP_FIND_GROUP_PERMS = True

# Cache settings
AUTH_LDAP_CACHE_GROUPS = True
AUTH_LDAP_GROUP_CACHE_TIMEOUT = 3600

AUTHENTICATION_BACKENDS = (
    'django_auth_ldap.backend.LDAPBackend',
    'django.contrib.auth.backends.ModelBackend',
)

那么我必须在哪里设置或获取什么?

这是我的edit_profile.html:

<form method="post">
        {% csrf_token %}

        <label for="first_name">Vorname </label>
        <input style="margin-bottom: 1em;" id="first_name" class="form-control" type="text" name="first_name" value="{{ user.first_name }}"><br>
        <label for="last_name">Nachname </label>
        <input style=" margin-bottom: 1em;" id="last_name" class="form-control" type="text" name="last_name" value="{{ user.last_name }}"><br>
        <label for="email">E-Mail </label>
        <input style="margin-bottom: 1em;" id="email" class="form-control" type="email" required=True unique=True name="email" value="{{ user.email }}"><br>

        <button class="btn btn-success btn-sm" type="submit">Bestätigen</button>

【问题讨论】:

    标签: python django active-directory ldap user-profile


    【解决方案1】:

    只有django-auth-ldap是不可能的

    粗略的猜测表明您正在使用django-auth-ldap(我更新了您的问题)。一看就知道它只有一个后端,不能做其他任何事情。

    如果你真的想更新 AD 中的一些数据,你需要自己做。我正在使用python-ldap3,为此我可以推荐它。它还包括一些专门用于 AD 的助手。


    更新:根据要求,使用python-ldap3 的示例

    类似的东西,不确定下面的代码是否有效(它是现有代码的混搭)。但它应该让你知道你应该做什么。欢迎来到 LDAP 的地狱。

    import ldap3
    
    conn = ldap3.Connection(
        server="ldaps://foobar",
        user="username@domain",  # normally full DN, but AD supports this format as well
        password="password",
        auto_bind=ldap3.AUTO_BIND_NONE,
        authentication=ldap3.SIMPLE,
        raise_exceptions=True,
        auto_referrals=False,  # 90% you want it set to False
        receive_timeout=10,  # seconds, exception afterwards
    )
    
    conn.start_tls()
    conn.bind()
    
    search = conn.extend.standard.paged_search(
        search_base="dc=domain",
        search_filter="(userPrincipalName=username@domain)",  # or (cn=username) or (sAMAccountName=username) or whatever
        search_scope=ldap3.SUBTREE,
        attributes=ldap3.ALL_ATTRIBUTES,
        dereference_aliases=ldap3.DEREF_NEVER,
        generator=True,
    )
    
    entries = [entry for entry in search if entry["type"] == "searchResEntry"]  # not sure how to get rid of all the aliases otherwise
    
    assert len(entries) is 1, "got {0} entries".format(len(entries))
    entry = entries[0]
    
    dn = entry["dn"]
    
    changes = {
        "attributeName": [
            [ldap3.MODIFY_DELETE, ["old value 1", "old value 2",]],
            [ldap3.MODIFY_ADD, ["a new value"]],
        ]
    }
    
    conn.modify(dn, changes)
    
    conn.unbind()
    

    【讨论】:

    • 非常感谢!!你有这方面的例子吗?
    • 这个代码在setting.py文件中还是我必须在某个地方绑定它。如果是,你能告诉我怎么做吗?
    • @ZeraICT 不,将其放入设置中无济于事>。
    • 如果我有一个 GUI 并且我想运行此代码,是否可以绑定它,以便我可以更改即登录用户的名字,它会自动更新用户按下提交按钮后的 LDAP?
    • 当用户按下提交按钮时运行此代码。如果您在用户提交表单时不知道如何运行 python 代码,check django documentation.
    【解决方案2】:

    并非不可能,但并不容易。

    首先,您需要对用户进行身份验证或在 LDAP 中找到他。示例:

    user = authenticate(
        username=request.user.username,
        password=request.POST['password']
    )
    

    现在,用户拥有request.user 对象没有的一些属性,例如ldap_user。该属性可用于修改密码,例如:

    # remember to add: import ldap.modlist as modlist
    
    pwd = "new password"
    ldif = modlist.modifyModlist(
        {'userpassword': user.ldap_user.attrs['userpassword']},
        {'userpassword': [pwd.encode()]}
    )
    user.ldap_user.connection.modify_s(user.ldap_user.dn, ldif)
    

    就是这样。

    【讨论】:

    • 您的解决方案非常好,但是ldif 在 django 1.8 中给我带来了一些麻烦。未来人:ldif = modlist.modifyModlist( {u'userpassword': [password_old.encode('utf-8')]}, {u'userpassword': [password_hashed.encode('utf-8')]} )
    • 我建议你不要用其他工具搞乱和管理 LDAP,如果可能的话可能是 Keycloak 或类似工具,而使用 django-ldap 仅用于查询。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多