Don't crash on LDAP errors

This commit is contained in:
Martin Pépin 2020-06-28 23:48:20 +02:00
parent 56f1edebe3
commit de1bba3695
No known key found for this signature in database
GPG key ID: E7520278B1774448

View file

@ -1,3 +1,4 @@
import logging
from collections import namedtuple
from dal import autocomplete
@ -12,6 +13,9 @@ else:
ldap = None
django_logger = logging.getLogger("django.request")
class SearchUnit:
"""Base class for all the search utilities.
@ -128,17 +132,21 @@ class LDAPSearch(SearchUnit):
if ldap is None or query == "(&)":
return []
ldap_obj = ldap.initialize(self.ldap_server_url)
res = ldap_obj.search_s(
self.domain_component, ldap.SCOPE_SUBTREE, query, self.search_fields
)
return [
Clipper(
clipper=attrs["uid"][0].decode("utf-8"),
fullname=attrs["cn"][0].decode("utf-8"),
try:
ldap_obj = ldap.initialize(self.ldap_server_url)
res = ldap_obj.search_s(
self.domain_component, ldap.SCOPE_SUBTREE, query, self.search_fields
)
for (_, attrs) in res
]
return [
Clipper(
clipper=attrs["uid"][0].decode("utf-8"),
fullname=attrs["cn"][0].decode("utf-8"),
)
for (_, attrs) in res
]
except ldap.LDAPError as err:
django_logger.error("An LDAP error occurred", exc_info=err)
return []
# ---