From 78469599596eee6f3acbc4331309700992938cbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robin=20Hru=C5=A1ka?= Date: Tue, 16 Apr 2024 19:16:03 +0200 Subject: [PATCH] do not count nonexisting credentials in the result --- seacatauth/credentials/service.py | 51 ++++++++++++------------------- 1 file changed, 20 insertions(+), 31 deletions(-) diff --git a/seacatauth/credentials/service.py b/seacatauth/credentials/service.py index 69acb1d3..80bdda39 100644 --- a/seacatauth/credentials/service.py +++ b/seacatauth/credentials/service.py @@ -288,38 +288,27 @@ async def list(self, session: SessionAdapter, search_params: generic.SearchParam filtered_cids = sorted(filtered_cids) offset = search_params.Page * search_params.ItemsPerPage - if search_params.SimpleFilter: - # String filter is specified - for cid in filtered_cids: - _, provider_id, _ = cid.split(":", 2) - try: - provider = self.CredentialProviders[provider_id] - cred_data = await provider.get(cid) - except KeyError: - L.info("Found an assignment of nonexisting credentials", struct_data={ - "cid": cid, "role_ids": searched_roles, "tenant_ids": searched_tenants}) + for cid in filtered_cids: + _, provider_id, _ = cid.split(":", 2) + try: + provider = self.CredentialProviders[provider_id] + cred_data = await provider.get(cid) + except KeyError: + L.info("Found an assignment of nonexisting credentials", struct_data={ + "cid": cid, "role_ids": searched_roles, "tenant_ids": searched_tenants}) + continue + if not search_params.SimpleFilter or ( + cred_data.get("username", "").startswith(search_params.SimpleFilter) + or cred_data.get("email", "").startswith(search_params.SimpleFilter) + ): + if offset > 0: + # Skip until offset is reached + offset -= 1 continue - if ( - cred_data.get("username", "").startswith(search_params.SimpleFilter) - or cred_data.get("email", "").startswith(search_params.SimpleFilter) - ): - if offset > 0: - # Skip until offset is reached - offset -= 1 - continue - credentials.append(cred_data) - if len(credentials) >= search_params.ItemsPerPage: - # Page is full - break - else: - for cid in filtered_cids[offset:offset + search_params.ItemsPerPage]: - _, provider_id, _ = cid.split(":", 2) - try: - provider = self.CredentialProviders[provider_id] - credentials.append(await provider.get(cid)) - except KeyError: - L.info("Found an assignment of nonexisting credentials", struct_data={ - "cid": cid, "role_ids": searched_roles, "tenant_ids": searched_tenants}) + credentials.append(cred_data) + if len(credentials) >= search_params.ItemsPerPage: + # Page is full + break return { "data": credentials,