简体   繁体   English

python - ldap3 库:如何向属性添加多个值

[英]python - ldap3 lib: How to add multiple values to attribute

I want to add more than one email adress to a user in ldap. Therefore is an attribut called mailLocalAdress that looks like this when I search/find a specific user with ldapsearch:我想向 ldap 中的用户添加多个 email 地址。因此,当我使用 ldapsearch 搜索/查找特定用户时,名为 mailLocalAdress 的属性如下所示:

mailLocalAddress: abc@testing.com
mailLocalAddress: abcd@testing.com
mailLocalAddress: abcdefg@testing.com

I'm using python 3, the ldap3 lib and this is how I define the value for the mailLocalAdress attribute我正在使用 python 3,ldap3 库,这就是我定义 mailLocalAdress 属性值的方式

ldap_values['mailLocalAddress'] = [user.email_first, user.email_second, user.email_third]

and this is the add command这是添加命令

ldap_con.add(dn, object_class, ldap_values)

and I got this as result when doing ldapsearch我在做 ldapsearch 时得到了这个结果

mailLocalAddress: ['abc@testing.com', 'abcd@testing.com', 'abcdefg@testing.com']

Any ideas how to get the listing above?任何想法如何获得上面的清单?

I'm not able to reproduce the behavior you've reported.我无法重现您报告的行为。 If I start with this in my ldap directory:如果我从我的 ldap 目录开始:

dn: dc=example,dc=com
objectclass: dcObject
objectclass: organization
o: example
dc: example

dn: ou=people,dc=example,dc=com
objectclass: organizationalunit
ou: people

And then run the following Python code:然后运行以下 Python 代码:

>>> from ldap3 import Server, Connection, ALL
>>> server=Server('localhost')
>>> conn = Connection(server, user='cn=manager,dc=example,dc=com', password='secret')
>>> conn.bind()
True
>>> dn='uid=alice,ou=people,dc=example,dc=com'
>>> object_class=['person', 'organizationalperson', 'inetorgperson', 'inetlocalmailrecipient']
>>> ldap_values={'sn': 'example', 'cn': 'alice example', 'mailLocalAddress': ['alice@example.com', 'alice.example@example.com']}
>>> conn.add(dn, object_class, ldap_values)
True
>>>

An ldapsearch returns: ldapsearch返回:

/etc/openldap # ldapsearch ... -b dc=example,dc=com uid=alice
# alice, people, example.com
dn: uid=alice,ou=people,dc=example,dc=com
sn: example
cn: alice example
mailLocalAddress: alice@example.com
mailLocalAddress: alice.example@example.com
objectClass: person
objectClass: organizationalPerson
objectClass: inetOrgPerson
objectClass: inetLocalMailRecipient
uid: alice

Which seems to be exactly what you want.这似乎正是你想要的。

It was a completely different problem:这是一个完全不同的问题:

ldap_values = {k: str(v).encode("utf-8") for k,v in ldap_values.items()}

Before I add the values, I had to convert them to UTF8.在添加值之前,我必须将它们转换为 UTF8。 Therefore I convert every value in my dict to string, also the lists.因此,我将字典中的每个值都转换为字符串,还有列表。 And of course after this 'brilliant move of mine' the list is treated as string when adding the values to ldap. I've overlooked this one-liner all along.当然,在这个“我的绝妙举动”之后,在将值添加到 ldap 时,列表被视为字符串。我一直忽略了这一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM