Search code examples
pythonpython-3.xldap

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:

mailLocalAddress: [email protected]
mailLocalAddress: [email protected]
mailLocalAddress: [email protected]

I'm using python 3, the ldap3 lib and this is how I define the value for the mailLocalAdress attribute

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

mailLocalAddress: ['[email protected]', '[email protected]', '[email protected]']

Any ideas how to get the listing above?


Solution

  • I'm not able to reproduce the behavior you've reported. If I start with this in my ldap directory:

    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:

    >>> 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': ['[email protected]', '[email protected]']}
    >>> conn.add(dn, object_class, ldap_values)
    True
    >>>
    

    An ldapsearch returns:

    /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: [email protected]
    mailLocalAddress: [email protected]
    objectClass: person
    objectClass: organizationalPerson
    objectClass: inetOrgPerson
    objectClass: inetLocalMailRecipient
    uid: alice
    

    Which seems to be exactly what you want.