Stefan
Stefan

Reputation: 51

How to use ldap3 generator for pagination?

I want to paginate the results of an ldap query so that I get 50 users per query for each page. The documentation here http://ldap3.readthedocs.io/searches.html?highlight=generator suggests that using a generator is the simplest way to do this, however it doesn't provide any detail as to how to use it to achieve pagination. When I iterate through the generator object, it prints out every user entry, even though I specified 'paged_size=5' in my search query. Can anyone explain to me what's going on here? Thanks!!

Upvotes: 2

Views: 4380

Answers (3)

Eric
Eric

Reputation: 422

This is an old question, but still valid. The ldap3 docs don't go into much detail about using search_paged on the Reader.

Here's an example how to use a Reader to cycle through all the members of a group.

# Construct an ObjectDef from the host schema (note this requires get_info='SCHEMA' when defining the Server, which is the default
objdef = ObjectDef('user', connection)

# These are the attributes we want returned
attributes = ['name', 'displayName']

# This is the DN where users live
users_dn = 'OU=People,DC=my,DC=domain,DC=com'

# This is the group DN; we want to search its members
group_dn = 'CN=MyGroupName,OU=AppGroups,OU=Administration,DC=my,DC=domain,DC=com'
reader = Reader(objdef, users_dn, f"(memberOf:={group_dn})", attributes=attributes)

page_size = 10
page = reader.search_paged(page_size)

# page is a generator that yields an Entry representing a single member
#  after getting to the end of the page, a new page is retrieved until we have exhausted all members

for member in page:
   print(f"Name: {member.name.value}, Display Name: {member.displayName.value}")

This method is useful when you have a group with a lot of members (1500+) and you want to avoid read timeouts.

Upvotes: 0

Daniel Lee
Daniel Lee

Reputation: 8041

This is a similar system that I used:

# Set up your ldap connection
conn = Connection(*args)

# create a generator
entry_generator = conn.extend.standard.paged_search(
        search_base=self.dc, search_filter=query[0],
        search_scope=SUBTREE, attributes=self.user_attributes,
        paged_size=1, generator=True)

# Then get your results:
results = []
for entry in entry_generator:
    total_entries += 1
    results.append(entry)
    if total_entries % 50 == 0:
        # do something with results

Otherwise try setting the page_size to 50 and get results like that.

Upvotes: 1

Kevin Schellenberg
Kevin Schellenberg

Reputation: 845

Try setting the paged_criticality parameter to True. It could be that the server is not capable of performing a paged search. If that's the case and paged_criticality is True the search will fail rather than returning all users.

Upvotes: 0

Related Questions