From 86eae5dae5a19b6a85ad7990a88241eab466312a Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Wed, 20 Jul 2022 10:50:58 +0200 Subject: [PATCH] ldap.LdapServer: make parameters accepted by search and paged_search methods identical --- mylib/ldap.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/mylib/ldap.py b/mylib/ldap.py index 4bf8c7d..8748309 100644 --- a/mylib/ldap.py +++ b/mylib/ldap.py @@ -116,7 +116,7 @@ class LdapServer: return ldap.SCOPE_SUBTREE # pylint: disable=no-member raise Exception(f'Unknown LDAP scope "{scope}"') - def search(self, basedn, filterstr=None, attrs=None, sizelimit=0, scope=None): + def search(self, basedn, filterstr=None, attrs=None, sizelimit=None, scope=None): """ Run a search on LDAP server """ assert self.con or self.connect() res_id = self.con.search( @@ -141,10 +141,18 @@ class LdapServer: result = self.search(dn, filterstr=filterstr, scope='base', attrs=attrs) return result[dn] if dn in result else None - def paged_search(self, basedn, filterstr, attrs, scope='sub', pagesize=500): + def paged_search(self, basedn, filterstr=None, attrs=None, scope=None, pagesize=None, + sizelimit=None): """ Run a paged search on LDAP server """ assert not self.v2, "Paged search is not available on LDAP version 2" assert self.con or self.connect() + + # Set parameters default values (if not defined) + filterstr = filterstr if filterstr else '(objectClass=*)' + attrs = attrs if attrs else [] + scope = scope if scope else 'sub' + pagesize = pagesize if pagesize else 500 + # Initialize SimplePagedResultsControl object page_control = SimplePagedResultsControl( True, @@ -208,6 +216,13 @@ class LdapServer: # Store results of this page for obj_dn, obj_attrs in rdata: ret[obj_dn] = obj_attrs + # If sizelimit reached, stop + if sizelimit and len(ret) >= sizelimit: + break + + # If sizelimit reached, stop + if sizelimit and len(ret) >= sizelimit: + break # If no cookie returned, we are done if not result_page_control.cookie: