active directory objectGUID

master
Бородин Роман 2018-04-26 17:06:52 +03:00
parent 62d5f96624
commit b08fa91929
2 changed files with 17 additions and 2 deletions

View File

@ -1,6 +1,7 @@
import ldap import ldap
import ldap.filter import ldap.filter
from .ldapprobject import LdapprObject from .ldapprobject import LdapprObject
from uuid import UUID
class Connection(object): class Connection(object):
@ -44,7 +45,18 @@ class Connection(object):
search_filter = ldap.filter.escape_filter_chars(search_filter) search_filter = ldap.filter.escape_filter_chars(search_filter)
result = self.conn.search_s(self.search_base, ldap.SCOPE_SUBTREE, result = self.conn.search_s(self.search_base, ldap.SCOPE_SUBTREE,
search_filter) 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): def get(self, search_filter):
"""Get first object found """Get first object found

View File

@ -2,7 +2,7 @@ import ldap
import ldif import ldif
from ldap.cidict import cidict from ldap.cidict import cidict
from io import StringIO from io import StringIO
from uuid import UUID
class CustomCidict(cidict): class CustomCidict(cidict):
def __getitem__(self, key): def __getitem__(self, key):
@ -19,12 +19,15 @@ class LdapprObject(object):
The LdapprObject is used to handle search results from the Connection The LdapprObject is used to handle search results from the Connection
class. It's a representation of a single object in the LDAP Directory. class. It's a representation of a single object in the LDAP Directory.
""" """
guid = None
def __init__(self, result, conn): def __init__(self, result, conn):
"""The class is initialized with a tuple: (dn, {attributes}), and the """The class is initialized with a tuple: (dn, {attributes}), and the
existing connection existing connection
""" """
(self.dn, self.attributes) = result (self.dn, self.attributes) = result
self.attrs = CustomCidict(self.attributes) 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 self.conn = conn
def __str__(self): def __str__(self):