Python ldap.controls() Examples

The following are 2 code examples of ldap.controls(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module ldap , or try the search function .
Example #1
Source File: AD.py    From Vibe with MIT License 5 votes vote down vote up
def ldap_query(self,l, base_dn, subtree, objectFilter, attrs):
		ldap_control = ldap.controls.SimplePagedResultsControl (True, size=1000, cookie='' )
		results = []
		while True:
			msgid = l.search_ext (base_dn, subtree, objectFilter, attrs, serverctrls=[ldap_control] )
			rtype, rawResults, id, server_controls = l.result3 ( msgid )
			results += rawResults
			page_controls = [c for c in server_controls if c.controlType == ldap.controls.SimplePagedResultsControl.controlType]
			if page_controls:
				cookie = page_controls[0].cookie
			if not cookie:
				break
			else:
				ldap_control.cookie = cookie
		return results 
Example #2
Source File: externalldap.py    From quay with Apache License 2.0 5 votes vote down vote up
def at_least_one_user_exists(self):
        logger.debug("Checking if any users exist in LDAP")
        try:
            with self._ldap.get_connection():
                pass
        except ldap.INVALID_CREDENTIALS:
            return (None, "LDAP Admin dn or password is invalid")

        has_pagination = not self._force_no_pagination
        with self._ldap.get_connection() as conn:
            for user_search_dn in self._user_dns:
                search_flt = "(objectClass=*)"
                search_flt = self._add_user_filter(search_flt)

                lc = ldap.controls.libldap.SimplePagedResultsControl(
                    criticality=True, size=1, cookie=""
                )
                try:
                    if has_pagination:
                        msgid = conn.search_ext(
                            user_search_dn, ldap.SCOPE_SUBTREE, search_flt, serverctrls=[lc]
                        )
                        _, rdata, _, serverctrls = conn.result3(msgid)
                    else:
                        msgid = conn.search(user_search_dn, ldap.SCOPE_SUBTREE, search_flt)
                        _, rdata = conn.result(msgid)

                    for entry in rdata:  # Handles both lists and iterators.
                        return (True, None)

                except ldap.LDAPError as lde:
                    return (False, str(lde) or "Could not find DN %s" % user_search_dn)

        return (False, None)