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
raise Exception("Unknown LDAP scope '%s'" % scope)
def search(self, basedn, filterstr='(objectClass=*)', attrs=[], sizelimit=0, scope='sub'):
res_id = self.con.search(basedn, self.get_scope(scope), filterstr, attrs)
def search(self, basedn, filterstr=None, attrs=None, sizelimit=0, scope=None):
res_id = self.con.search(
basedn,
self.get_scope(scope if scope else 'sub'),
filterstr if filterstr else '(objectClass=*)',
attrs if attrs else []
)
ret = {}
c = 0
while 1:
while True:
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
else:
if res_type == ldap.RES_SEARCH_ENTRY:
ret[res_data[0][0]] = res_data[0][1]
c=c+1
c += 1
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):
ldif = modlist.addModlist(attrs)
try: