簡體   English   中英

python-ldap3 無法將用戶添加到現有 LDAP 組

[英]python-ldap3 is unable to add user to and existing LDAP group

我能夠使用 LDAP3 成功連接並檢索我的 LDAP 組成員,如下所示。

from ldap3 import Server, Connection, ALL, SUBTREE
from ldap3.extend.microsoft.addMembersToGroups import ad_add_members_to_groups as addMembersToGroups

>>> conn = Connection(Server('ldaps://ldap.****.com:***', get_info=ALL),check_names=False, auto_bind=False,user="ANT\*****",password="******", authentication="NTLM")
>>> 
>>> conn.open()
>>> conn.search('ou=Groups,o=****.com', '(&(cn=MY-LDAP-GROUP))', attributes=['cn', 'objectclass', 'memberuid'])
it returns True and I can see members by printing 
conn.entries
>>> 

上面一行表示MY-LDAP-GROUP存在並在搜索時返回TRUE但當我嘗試將用戶添加到組時拋出LDAP 組未找到,如下所示

>>> addMembersToGroups(conn, ['myuser'], 'MY-LDAP-GROUP')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/****/anaconda3/lib/python3.7/site-packages/ldap3/extend/microsoft/addMembersToGroups.py", line 69, in ad_add_members_to_groups
    raise LDAPInvalidDnError(group + ' not found')
ldap3.core.exceptions.LDAPInvalidDnError: MY-LDAP-GROUP not found
>>>

上面一行說MY-LDAP-GROUP存在並返回TRUE

返回True僅意味着搜索成功。 這並不意味着找到了任何東西。 conn.entries有什么conn.entries嗎?

但我懷疑你真正的問題是不同的。 如果ad_add_members_to_groups的源代碼,那么它需要的是組的distinguishedName名稱(注意參數名稱group_dn ),但您傳遞的是cn (通用名稱)。 例如,您的代碼應該類似於:

addMembersToGroups(conn, ['myuser'], 'CN=MY-LDAP-GROUP,OU=Groups,DC=example,DC=com')

如果您不知道 DN,則從搜索中詢問distinguishedName屬性。

警告: ad_add_members_to_groups代碼在添加新成員之前檢索所有當前成員。 如果您正在使用具有大量成員的組,那么您可能會遇到性能問題(例如,如果該組有 1000 名成員,它將在添加任何人之前加載所有 1000 名成員)。 您實際上不需要這樣做(您可以在不查看當前成員身份的情況下添加新成員)。 我認為他們試圖避免的是當您嘗試添加已經在組中的人時遇到的錯誤。 但我認為有更好的方法來處理。 如果您只與小團體合作,這對您來說可能無關緊要。

經過這么多次試驗和錯誤,我感到沮喪並使用舊的python-ldap庫來添加現有用戶。 現在我的代碼是ldap3ldap的混合。

我知道這不是 OP 想要的。 但這可能對某人有所幫助。

這里用戶 Dinesh Kumar 已經是組 group1 的一部分。 我正在嘗試將他添加到另一個組 group2 中,該組成功並且不會干擾現有組

import ldap
import ldap.modlist as modlist

def add_existing_user_to_group(user_name, user_id, group_id):
    """
    :return:
    """

    # ldap expects a byte string.
    converted_user_name = bytes(user_name, 'utf-8')
    converted_user_id = bytes(user_id, 'utf-8')
    converted_group_id = bytes(group_id, 'utf-8')

    # Add all the attributes for the new dn
    ldap_attr = {}
    ldap_attr['uid'] = converted_user_name
    ldap_attr['cn'] = converted_user_name
    ldap_attr['uidNumber'] = converted_user_id
    ldap_attr['gidNumber'] = converted_group_id
    ldap_attr['objectClass'] =  [b'top', b'posixAccount', b'inetOrgPerson']
    ldap_attr['sn'] = b'Kumar'
    ldap_attr['homeDirectory'] = b'/home/users/dkumar'

    # Establish connection to server using ldap
    conn = ldap.initialize(server_uri, bytes_mode=False)
    bind_resp = conn.simple_bind_s("cn=admin,dc=testldap,dc=com", "password")

    dn_new = "cn={},cn={},ou=MyOU,dc=testldap,dc=com".format('Dinesh Kumar','group2')

    ldif = modlist.addModlist(ldap_attr)
    try:
        response = conn.add_s(dn_new, ldif)
    except ldap.error as e:
        response = e
    print(" The response is ", response)
    conn.unbind()
    return response

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM