Switch to using a named LDAP tuple for more readable code

This commit is contained in:
Joseph Schorr 2015-06-01 13:51:00 -04:00
parent 386b1710ed
commit 2a56790d38

View file

@ -9,6 +9,7 @@ import os
from util.aes import AESCipher
from util.validation import generate_valid_usernames
from data import model
from collections import namedtuple
logger = logging.getLogger(__name__)
if os.environ.get('LDAP_DEBUG') == '1':
@ -55,6 +56,8 @@ class LDAPConnection(object):
class LDAPUsers(object):
_LDAPResult = namedtuple('LDAPResult', ['dn', 'attrs'])
def __init__(self, ldap_uri, base_dn, admin_dn, admin_passwd, user_rdn, uid_attr, email_attr):
self._ldap_conn = LDAPConnection(ldap_uri, admin_dn, admin_passwd)
self._ldap_uri = ldap_uri
@ -110,9 +113,11 @@ class LDAPUsers(object):
logger.debug('Found matching pairs: %s', pairs)
results = [LDAPUsers._LDAPResult(*pair) for pair in pairs]
# Filter out pairs without DNs. Some LDAP impls will return such
# pairs.
with_dns = [pair for pair in pairs if pair[0]]
with_dns = [result for result in results if result.dn]
if len(with_dns) < 1:
return None
@ -122,7 +127,7 @@ class LDAPUsers(object):
# Otherwise, there are multiple pairs with DNs, so find the one with the mail
# attribute (if any).
with_mail = [pair for pair in pairs if pair[1].get(self._email_attr)]
with_mail = [result for result in results if result.attrs.get(self._email_attr)]
return with_mail[0] if with_mail else with_dns[0]
def confirm_existing_user(self, username, password):