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.
This commit is contained in:
Martin Pépin 2017-04-01 21:45:05 +01:00
parent 1eed2283f5
commit 9cdf064005
2 changed files with 20 additions and 8 deletions

View file

@ -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

View file

@ -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