From 2a56790d380018adc0f6e0ab77e3c40ac23433f7 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Mon, 1 Jun 2015 13:51:00 -0400 Subject: [PATCH] Switch to using a named LDAP tuple for more readable code --- data/users.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/data/users.py b/data/users.py index ecd7e86a8..f528694a3 100644 --- a/data/users.py +++ b/data/users.py @@ -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):