- fixed typo with unbinding code
authorRodney Ewing <ewing.rj@gmail.com>
Tue, 9 Jul 2013 17:44:44 +0000 (10:44 -0700)
committerRodney Ewing <ewing.rj@gmail.com>
Thu, 15 Aug 2013 22:57:59 +0000 (15:57 -0700)
- added the ability to get the user's email from the ldap server upon registration

mediagoblin/plugins/ldap/tools.py
mediagoblin/plugins/ldap/views.py

index fd13cfe4df520c196143df573a73af102f04e8e1..3f15c07a9678a679ac216938d82b64588e1ee341 100644 (file)
@@ -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
index 217c6d8c3e3fc6a70c2dca37598acad14dd27b95..aef1bf56fff630036897e2f7fc0fccfc010205ad 100644 (file)
@@ -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,