From b08fa91929e17b351a230dba3408af392921dd98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D0=BE=D1=80=D0=BE=D0=B4=D0=B8=D0=BD=20=D0=A0=D0=BE?= =?UTF-8?q?=D0=BC=D0=B0=D0=BD?= Date: Thu, 26 Apr 2018 17:06:52 +0300 Subject: [PATCH] active directory objectGUID --- ldappr/connection.py | 14 +++++++++++++- ldappr/ldapprobject.py | 5 ++++- 2 files changed, 17 insertions(+), 2 deletions(-) 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):