diff --git a/ldappr/connection.py b/ldappr/connection.py index c6140cf..636e7c6 100644 --- a/ldappr/connection.py +++ b/ldappr/connection.py @@ -1,6 +1,7 @@ import ldap import ldap.filter from .ldapprobject import LdapprObject +from uuid import UUID class Connection(object): @@ -44,7 +45,18 @@ class Connection(object): search_filter = ldap.filter.escape_filter_chars(search_filter) result = self.conn.search_s(self.search_base, ldap.SCOPE_SUBTREE, search_filter) - return [LdapprObject(item, self.conn) for item in result] + return [LdapprObject(item, self.conn) for item in result if item[0] != None ] + def search_by_guid(self, guid): + """Get list of objects that match the search_filter + + :param search_filter: filter to find the objects + :return: list of LdapperObjects (or empty list) + """ + u = UUID(guid) + search_filter = '(objectguid=%s)' % ''.join(['\\%s' % u.hex[i:i+2] for i in range(0, len(u.hex), 2)]) + result = self.conn.search_s(self.search_base, ldap.SCOPE_SUBTREE, + search_filter) + return [LdapprObject(item, self.conn) for item in result if item[0] != None ] def get(self, search_filter): """Get first object found diff --git a/ldappr/ldapprobject.py b/ldappr/ldapprobject.py index af797cf..f727f97 100644 --- a/ldappr/ldapprobject.py +++ b/ldappr/ldapprobject.py @@ -2,7 +2,7 @@ import ldap import ldif from ldap.cidict import cidict from io import StringIO - +from uuid import UUID class CustomCidict(cidict): def __getitem__(self, key): @@ -19,12 +19,15 @@ class LdapprObject(object): The LdapprObject is used to handle search results from the Connection class. It's a representation of a single object in the LDAP Directory. """ + guid = None def __init__(self, result, conn): """The class is initialized with a tuple: (dn, {attributes}), and the existing connection """ (self.dn, self.attributes) = result self.attrs = CustomCidict(self.attributes) + if 'objectguid' in map(lambda x: x.lower(), self.attrs.keys()): + self.guid = str(UUID(bytes=self.attrs['objectguid'][0])) self.conn = conn def __str__(self):