Switch to using a named LDAP tuple for more readable code
This commit is contained in:
parent
386b1710ed
commit
2a56790d38
1 changed files with 7 additions and 2 deletions
|
@ -9,6 +9,7 @@ import os
|
||||||
from util.aes import AESCipher
|
from util.aes import AESCipher
|
||||||
from util.validation import generate_valid_usernames
|
from util.validation import generate_valid_usernames
|
||||||
from data import model
|
from data import model
|
||||||
|
from collections import namedtuple
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
if os.environ.get('LDAP_DEBUG') == '1':
|
if os.environ.get('LDAP_DEBUG') == '1':
|
||||||
|
@ -55,6 +56,8 @@ class LDAPConnection(object):
|
||||||
|
|
||||||
|
|
||||||
class LDAPUsers(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):
|
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_conn = LDAPConnection(ldap_uri, admin_dn, admin_passwd)
|
||||||
self._ldap_uri = ldap_uri
|
self._ldap_uri = ldap_uri
|
||||||
|
@ -110,9 +113,11 @@ class LDAPUsers(object):
|
||||||
|
|
||||||
logger.debug('Found matching pairs: %s', pairs)
|
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
|
# Filter out pairs without DNs. Some LDAP impls will return such
|
||||||
# pairs.
|
# 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:
|
if len(with_dns) < 1:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@ -122,7 +127,7 @@ class LDAPUsers(object):
|
||||||
|
|
||||||
# Otherwise, there are multiple pairs with DNs, so find the one with the mail
|
# Otherwise, there are multiple pairs with DNs, so find the one with the mail
|
||||||
# attribute (if any).
|
# 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]
|
return with_mail[0] if with_mail else with_dns[0]
|
||||||
|
|
||||||
def confirm_existing_user(self, username, password):
|
def confirm_existing_user(self, username, password):
|
||||||
|
|
Reference in a new issue