From 9cdf0640054dc0bf983aab276e5aaa98a2ec6f8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Sat, 1 Apr 2017 21:45:05 +0100 Subject: [PATCH 1/2] Handle incomplete values from the LDAP Sometime `uid` is not set in the objects fetched from the LDAP. This case has to be handled. Also, the `.uid` and `.cn` attributes of these objects in the python abstractions have a `.value` method which we should use. --- gestioncof/autocomplete.py | 14 ++++++++++---- kfet/autocomplete.py | 14 ++++++++++---- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/gestioncof/autocomplete.py b/gestioncof/autocomplete.py index a9abbad7..7a9c37d2 100644 --- a/gestioncof/autocomplete.py +++ b/gestioncof/autocomplete.py @@ -14,6 +14,10 @@ from gestioncof.decorators import buro_required class Clipper(object): def __init__(self, clipper, fullname): + if fullname is None: + fullname = "" + assert isinstance(clipper, str) + assert isinstance(fullname, str) self.clipper = clipper self.fullname = fullname @@ -62,17 +66,19 @@ def autocomplete(request): )) if ldap_query != "(&)": # If none of the bits were legal, we do not perform the query + entries = None with Connection(settings.LDAP_SERVER_URL) as conn: conn.search( 'dc=spi,dc=ens,dc=fr', ldap_query, attributes=['uid', 'cn'] ) - queries['clippers'] = conn.entries + entries = conn.entries # Clearing redundancies queries['clippers'] = [ - Clipper(clipper.uid, clipper.cn) - for clipper in queries['clippers'] - if str(clipper.uid) not in usernames + Clipper(entry.uid.value, entry.cn.value) + for entry in entries + if entry.uid.value is not None + and entry.uid.value not in usernames ] # Resulting data diff --git a/kfet/autocomplete.py b/kfet/autocomplete.py index bee99517..0ab2c8aa 100644 --- a/kfet/autocomplete.py +++ b/kfet/autocomplete.py @@ -13,6 +13,10 @@ from kfet.models import Account class Clipper(object): def __init__(self, clipper, fullname): + if fullname is None: + fullname = "" + assert isinstance(clipper, str) + assert isinstance(fullname, str) self.clipper = clipper self.fullname = fullname @@ -80,17 +84,19 @@ def account_create(request): )) if ldap_query != "(&)": # If none of the bits were legal, we do not perform the query + entries = None with Connection(settings.LDAP_SERVER_URL) as conn: conn.search( 'dc=spi,dc=ens,dc=fr', ldap_query, attributes=['uid', 'cn'] ) - queries['clippers'] = conn.entries + entries = conn.entries # Clearing redundancies queries['clippers'] = [ - Clipper(clipper.uid, clipper.cn) - for clipper in queries['clippers'] - if str(clipper.uid) not in usernames + Clipper(entry.uid.value, entry.cn.value) + for entry in entries + if entry.uid.value is not None + and entry.uid.value not in usernames ] # Resulting data From a793e9a2e754d5b6294f9cfdea3d80430c7ca8d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Sat, 1 Apr 2017 22:07:32 +0100 Subject: [PATCH 2/2] exclude empty strings from ldap results The uid attribute in a LDAP's entry cannot be an empty string. We need to get an actual identifier. --- gestioncof/autocomplete.py | 2 +- kfet/autocomplete.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/gestioncof/autocomplete.py b/gestioncof/autocomplete.py index 7a9c37d2..3532525d 100644 --- a/gestioncof/autocomplete.py +++ b/gestioncof/autocomplete.py @@ -77,7 +77,7 @@ def autocomplete(request): queries['clippers'] = [ Clipper(entry.uid.value, entry.cn.value) for entry in entries - if entry.uid.value is not None + if entry.uid.value and entry.uid.value not in usernames ] diff --git a/kfet/autocomplete.py b/kfet/autocomplete.py index 0ab2c8aa..09057d4a 100644 --- a/kfet/autocomplete.py +++ b/kfet/autocomplete.py @@ -95,7 +95,7 @@ def account_create(request): queries['clippers'] = [ Clipper(entry.uid.value, entry.cn.value) for entry in entries - if entry.uid.value is not None + if entry.uid.value and entry.uid.value not in usernames ]