# Access user source LDAP instance for search

**URL:** <https://forum.inductiveautomation.com/t/access-user-source-ldap-instance-for-search/62669>\
**Category:** Ignition\
**Tags:** scripting, ignition79\
**Created:** [August 4, 2022, 12:57pm UTC](https://forum.inductiveautomation.com/t/access-user-source-ldap-instance-for-search/62669 "2022-08-04T12:57:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![guipinper](https://avatars.discourse-cdn.com/v4/letter/g/ce73a5/32.png) [@guipinper](https://forum.inductiveautomation.com/u/guipinper)\
**Post date:** [August 4, 2022, 12:57pm UTC](https://forum.inductiveautomation.com/t/access-user-source-ldap-instance-for-search/62669/1 "2022-08-04T12:57:56Z")

</div>

Hi all,  
I want to make LDAP queries using the LDAP Helper object. I wanted to use the, already configured, UserSource instance from the Gateway context.  
I was trying with something like:

```auto
from com.inductiveautomation.ignition.gateway import SRContext
context = SRContext.get()
profile = context.getUserSourceManager().getProfile("profilename")

```

the _profile_ is an object of the type

> com.inductiveautomation.ignition.gateway.authentication.UserSourceWrapper

from there I was hoping to find a LDAPHelper or something that could point to the LDAP client. But I can't find anything information regarding that.

Ignition version:7.9.18

Appreciate any help. Thanks

---

<div class="post-metadata">

**Author:** ![DNice](https://avatars.discourse-cdn.com/v4/letter/d/858c86/32.png) [@DNice](https://forum.inductiveautomation.com/u/DNice)\
**Post date:** [March 6, 2023, 6:43pm UTC](https://forum.inductiveautomation.com/t/access-user-source-ldap-instance-for-search/62669/2 "2023-03-06T18:43:39Z")

</div>

Did you make any progress on this? I am working down this path as well and looking for some help.

---

<div class="post-metadata">

**Author:** ![guipinper](https://avatars.discourse-cdn.com/v4/letter/g/ce73a5/32.png) [@guipinper](https://forum.inductiveautomation.com/u/guipinper)\
**Post date:** [March 30, 2023, 2:37pm UTC](https://forum.inductiveautomation.com/t/access-user-source-ldap-instance-for-search/62669/3 "2023-03-30T14:37:44Z")

</div>

Hey, sorry for the long waiting.

I manage to re-implement a class based on the work done [here](https://forum.inductiveautomation.com/t/ldap-attributes-access/39754/10). You probably need to adapt several parameters to match your config.

Let me know if this works for you.

```auto
from com.inductiveautomation.ignition.gateway.authentication.impl import LDAPHelper
from com.inductiveautomation.ignition.common.util import LoggerEx
#work from https://forum.inductiveautomation.com/t/ldap-attributes-access/39754/10
#user is https://docs.oracle.com/javase/7/docs/api/javax/naming/directory/SearchResult.html
# attibute is https://docs.oracle.com/javase/7/docs/api/javax/naming/directory/Attributes.html
class mySearchHandlerClass(LDAPHelper.SearchHandler):
    def __init__ (self):
        self.ctx = None
        self.result = None
        self.results = []
        return
    def create(self, ctx, result):
        self.ctx = ctx
        self.result = result
        return self.result  
    def getNoun(self):
        return 'LDAP'
        

class LDAP(object):

    builder = LoggerEx.newBuilder()
    search_handler = mySearchHandlerClass()
    logger = builder.build('LDAP_INTEGRATION')
    
    PRIMARY_DOMAIN_CONTROLLER = "Ahostname" 
    DC_PORT_PRIMARY = "1234"
    SECONDARY_DOMAIN_CONTROLLER = "Anotherhostname"
    DC_PORT_SECONDARY = "1235"
    
    ROLE_ID = "CN"
    ROLE_ATTR = "memberOf"
    USER_ATTR = "sAMAccountName"
    CONTACT_ATTR = ["mail","proxyAddresses","phone"]
    def __init__ (self):
        self.instance = LDAPHelper(self.logger)
        
        self.instance.setLdapHost(self.PRIMARY_DOMAIN_CONTROLLER)
        self.instance.setLdapPort(self.DC_PORT_PRIMARY)
        self.instance.setSecondaryLdapHost(self.SECONDARY_DOMAIN_CONTROLLER)
        self.instance.setSecondaryLdapPort(self.DC_PORT_SECONDARY)
        self.instance.setUseSSL(True)
        self.instance.setProfileUsername(self.readCredentials()["gatewayuser"])
        self.instance.setProfilePassword(self.readCredentials()["password"])
        self.instance.setReadTimeout(60000)
        self.instance.setPageSize(1000)
    def getReadTimeout(self):
        return self.instance.getReadTimeout()
        
    def getUsers(self):
        query = "(&(objectClass=user)(!(objectClass=computer)))"
        attrs = [self.USER_ATTR,self.ROLE_ATTR]+self.CONTACT_ATTR
        base = ["OU=User OU, DC=example, DC=com"]
        
        results = self.instance.search(base, query, attrs,self.search_handler)
        return results
        
    def getUsersWithRole(self,role):
        query = "(&(objectClass=user)(memberOf="+self.ROLE_ID+"="+role.lower()+")(!(objectClass=computer)))"
        query = "(&(objectClass=user)(memberOf=*"+self.ROLE_ID+"="+role.lower()+"*)(!(objectClass=computer)))"
        attrs = [self.USER_ATTR,self.ROLE_ATTR]+self.CONTACT_ATTR
        base = ["OU=User OU, DC=example, DC=com"]
        
        results = self.instance.search(base, query, attrs,self.search_handler)
        return results

        
    def __enter__ (self):
        return self
    def __exit__ (self, exc_type, exc_value, traceback):
        pass
        
        
    def readCredentials(self):
        return shared.lz.ldap.credentials.key #this is a json {"gatewayuser": "john@doe", "password":"strongpassword"}

```
