pythonldap3无法将用户添加到现有LDAP组

2024-10-02 00:37:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我能够使用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 group not found,如下所示

>>> 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
>>>

Tags: fromimportservermygroupnotallconnection
2条回答

The above line says MY-LDAP-GROUP exists and returns TRUE

返回True只意味着搜索成功。这并不意味着发现了什么。在conn.entries里有什么东西吗

但我怀疑你真正的问题是不同的。如果thisad_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

相关问题 更多 >

    热门问题