LdapServer: add get_object() method

This commit is contained in:
Benjamin Renard 2020-12-14 20:56:43 +01:00 committed by root
parent d336ab3a8d
commit 1f72a85b71

View file

@ -64,20 +64,29 @@ class LdapServer(object):
return ldap.SCOPE_SUBTREE return ldap.SCOPE_SUBTREE
raise Exception("Unknown LDAP scope '%s'" % scope) raise Exception("Unknown LDAP scope '%s'" % scope)
def search(self, basedn, filterstr='(objectClass=*)', attrs=[], sizelimit=0, scope='sub'): def search(self, basedn, filterstr=None, attrs=None, sizelimit=0, scope=None):
res_id = self.con.search(basedn, self.get_scope(scope), filterstr, attrs) res_id = self.con.search(
basedn,
self.get_scope(scope if scope else 'sub'),
filterstr if filterstr else '(objectClass=*)',
attrs if attrs else []
)
ret = {} ret = {}
c=0 c = 0
while 1: while True:
res_type, res_data = self.con.result(res_id,0) res_type, res_data = self.con.result(res_id,0)
if res_data == [] or sizelimit!=0 and c>sizelimit: if res_data == [] or (sizelimit and c > sizelimit):
break break
else: else:
if res_type == ldap.RES_SEARCH_ENTRY: if res_type == ldap.RES_SEARCH_ENTRY:
ret[res_data[0][0]]=res_data[0][1] ret[res_data[0][0]] = res_data[0][1]
c=c+1 c += 1
return ret return ret
def get_object(self, dn, filterstr=None, attrs=None):
result = self.search(dn, filterstr=filterstr, scope='base', attrs=attrs)
return result[dn] if dn in result else None
def add_object(self,dn,attrs): def add_object(self,dn,attrs):
ldif = modlist.addModlist(attrs) ldif = modlist.addModlist(attrs)
try: try: