From 517eb8b4433888a3ac11f0ed9efeb30dca68838b Mon Sep 17 00:00:00 2001 From: Rodney Ewing Date: Tue, 9 Jul 2013 10:44:44 -0700 Subject: [PATCH] - fixed typo with unbinding code - added the ability to get the user's email from the ldap server upon registration --- mediagoblin/plugins/ldap/tools.py | 20 +++++++++++++++++--- mediagoblin/plugins/ldap/views.py | 7 ++++--- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/mediagoblin/plugins/ldap/tools.py b/mediagoblin/plugins/ldap/tools.py index fd13cfe4..3f15c07a 100644 --- a/mediagoblin/plugins/ldap/tools.py +++ b/mediagoblin/plugins/ldap/tools.py @@ -33,19 +33,33 @@ class LDAP(object): _log.info('Initiating TLS') self.conn.start_tls_s() + def _get_email(self, server, username): + results = self.conn.search_s(server['LDAP_SEARCH_BASE'], + ldap.SCOPE_SUBTREE, 'uid={0}' + .format(username), + [server['EMAIL_SEARCH_FIELD']]) + + try: + email = results[0][1][server['EMAIL_SEARCH_FIELD']][0] + except KeyError: + email = None + + return email + def login(self, username, password): for k, v in self.ldap_settings.iteritems(): try: self._connect(v) user_dn = v['LDAP_USER_DN_TEMPLATE'].format(username=username) self.conn.simple_bind_s(user_dn, password.encode('utf8')) - return username + email = self._get_email(v, username) + return username, email except ldap.LDAPError, e: _log.info(e) finally: - _log.info('Unbinding {0}.').format(v['LDAP_SERVER_URI']) + _log.info('Unbinding {0}.'.format(v['LDAP_SERVER_URI'])) self.conn.unbind() - return False + return False, None diff --git a/mediagoblin/plugins/ldap/views.py b/mediagoblin/plugins/ldap/views.py index 217c6d8c..aef1bf56 100644 --- a/mediagoblin/plugins/ldap/views.py +++ b/mediagoblin/plugins/ldap/views.py @@ -31,7 +31,8 @@ def login(request): if request.method == 'POST' and login_form.validate(): l = LDAP() - username = l.login(login_form.username.data, login_form.password.data) + username, email = l.login(login_form.username.data, + login_form.password.data) if username: user = User.query.filter_by( @@ -55,8 +56,8 @@ def login(request): 'instance.')) return redirect(request, 'index') - register_form = forms.RegisterForm(request.form, - username=username) + register_form = forms.RegisterForm(username=username, + email=email) return render_to_response( request, -- 2.25.1