def getAsDict(con, base, ldapFilter, attrList, sizeLimit, scope=ldap.SCOPE_SUBTREE): """ Makes a search on LDAP, adjusting string to required type (ascii on python2, str on python3). returns an generator with the results, where each result is a dictionary where it values are always a list of strings """ logger.debug('Filter: {}, attr list: {}'.format(ldapFilter, attrList)) if attrList is not None: attrList = [tools.b2(i) for i in attrList] res = None try: # On python2, attrs and search string is str (not unicode), in 3, str (not bytes) res = con.search_ext_s(base, scope=scope, filterstr=tools.b2(ldapFilter), attrlist=attrList, sizelimit=sizeLimit) except ldap.LDAPError as e: LDAPError.reraise(e) except Exception as e: logger.exception('Exception connection:') raise LDAPError('{}'.format(e)) logger.debug('Result of search {} on {}: {}'.format(ldapFilter, base, res)) if res is not None: for r in res: if r[0] is None: continue # Skip None entities # Convert back attritutes to test_type ONLY on python2 dct = tools.CaseInsensitiveDict( (k, ['']) for k in attrList ) if attrList is not None else tools.CaseInsensitiveDict() # Convert back result fields to str for k, v in six.iteritems(r[1]): dct[tools.u2(k)] = list( i.decode('utf8', errors='replace') for i in v) dct.update({'dn': r[0]}) yield dct
def getFirst(con, base, objectClass, field, value, attributes=None, sizeLimit=50): """ Searchs for the username and returns its LDAP entry @param username: username to search, using user provided parameters at configuration to map search entries. @param objectClass: Objectclass of the user mane username to search. @return: None if username is not found, an dictionary of LDAP entry attributes if found (all in unicode on py2, str on py3). """ value = ldap.filter.escape_filter_chars(tools.b2(value)) # Convert atttribute list to bynary ONLY on python2 attrList = [field] + [i for i in attributes] ldapFilter = '(&(objectClass={})({}={}))'.format(objectClass, field, value) try: obj = next(getAsDict(con, base, ldapFilter, attrList, sizeLimit)) except StopIteration: return None # None found obj['_id'] = value return obj
def getAsDict(con, base, ldapFilter, attrList, sizeLimit, scope=ldap.SCOPE_SUBTREE): """ Makes a search on LDAP, adjusting string to required type (ascii on python2, str on python3). returns an generator with the results, where each result is a dictionary where it values are always a list of strings """ logger.debug('Filter: {}, attr list: {}'.format(ldapFilter, attrList)) if attrList is not None: attrList = [i for i in attrList] res = None try: # On python2, attrs and search string is str (not unicode), in 3, str (not bytes) res = con.search_ext_s( base, scope=scope, filterstr=tools.b2(ldapFilter), attrlist=attrList, sizelimit=sizeLimit ) except ldap.LDAPError as e: LDAPError.reraise(e) except Exception as e: logger.exception('Exception connection:') raise LDAPError('{}'.format(e)) logger.debug('Result of search {} on {}: {}'.format(ldapFilter, base, res)) if res is not None: for r in res: if r[0] is None: continue # Skip None entities # Convert back attritutes to test_type ONLY on python2 dct = tools.CaseInsensitiveDict((k, ['']) for k in attrList) if attrList is not None else tools.CaseInsensitiveDict() # Convert back result fields to str for k, v in r[1].items(): dct[k] = list(i.decode('utf8', errors='replace') for i in v) dct.update({'dn': r[0]}) yield dct
def escape(value): """ Escape filter chars for ldap search filter """ return ldap.filter.escape_filter_chars(tools.b2(value))