How to get Username by searching ACL attribute (ldap)

I'm creating a script that queries a sql database and creates content, in the sql database I have an "ID_number" column, and I have the same attribute map in ACL (ldap).

how do I search a username or userid from ID_number? all examples and documentation search for attributes base on username or userid

eg.
from plone import api
user = api.user.get(username='bob')
location = user.getProperty('location')

1 Like

You might be able to retrieve user information with the searchUsers method of the acl_users object (documented in Products.PluggableAuthService.interfaces.authservice.IPluggableAuthService). This method does not promise to support searches via arbitrary attributes (only via user id and user name), but your LDAP plugin might (or might not) support searches via general LDAP attributes.

It is quite some time that I have worked with an LDAP integration. Then, the used LDAP integration (I think this has been Products.LDAPUserFolder) allowed me to map the user id and user name used by Plone to LDAP attributes. Potentially, your LDAP integration allows this, too. If so, you can retrieve users indirectly via those mapped attributes.

Furthermore, the LDAP plugin itself may expose a search via arbitrary LDAP attributes. Finally, you can always use the Python library implementing the LDAP binding to access the LDAP server on your own and perform any LDAP search you like.

Thanks, I'm reverse engineering Products.LDAPUserFolder since I pass that attribute and used it many times, just never had to search by it. Products.LDAPUserFolder is capable of searching by the attribute in /manage_userrecords, just can figure out how they are doing it. I'm just going to pass ID_number as the userid

This isn't the best option, but in a case where I needed the same thing, I have a function that creates a dictionary of users:

def users_by_employee_id():
    """Build a dictionary of users by employee_id
    """
    users = api.user.get_users()
    users_by_id = {}
    for user in users:
        eid = user.getProperty('employee_id')
        users_by_id[eid] = user
    return users_by_id

Otherwise, we have other projects where we have created "fake brains" - a representation of the users in the catalog that allows the custom attributes to be searchable as indexes.

1 Like

thank you