allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brond...@apache.org
Subject git commit: [#7221] set display_name for ldap autoregister; decode unicode values from ldap
Date Fri, 26 Sep 2014 02:22:29 GMT
Repository: allura
Updated Branches:
  refs/heads/db/7721 69f30c4e0 -> 4921df8a1 (forced update)


[#7221] set display_name for ldap autoregister; decode unicode values from ldap


Project: http://git-wip-us.apache.org/repos/asf/allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/allura/commit/4921df8a
Tree: http://git-wip-us.apache.org/repos/asf/allura/tree/4921df8a
Diff: http://git-wip-us.apache.org/repos/asf/allura/diff/4921df8a

Branch: refs/heads/db/7721
Commit: 4921df8a187cc644c82adc9c0fb00f1f2d6b4414
Parents: da05789
Author: Dave Brondsema <dbrondsema@slashdotmedia.com>
Authored: Fri Sep 26 01:52:33 2014 +0000
Committer: Dave Brondsema <dbrondsema@slashdotmedia.com>
Committed: Fri Sep 26 02:22:18 2014 +0000

----------------------------------------------------------------------
 Allura/allura/lib/plugin.py                     | 34 ++++++++++++--------
 .../tests/unit/test_ldap_auth_provider.py       | 23 ++++++++++++-
 Allura/test.ini                                 |  1 +
 3 files changed, 43 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/4921df8a/Allura/allura/lib/plugin.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/plugin.py b/Allura/allura/lib/plugin.py
index 78aae92..256d11c 100644
--- a/Allura/allura/lib/plugin.py
+++ b/Allura/allura/lib/plugin.py
@@ -527,7 +527,9 @@ class LdapAuthenticationProvider(AuthenticationProvider):
         if user is None:
             if asbool(config.get('auth.ldap.autoregister', True)):
                 log.debug('LdapAuth: authorized user {} needs a mongo record registered.
 Creating...'.format(username))
-                user = M.User.register({'username': username})
+                user = M.User.register({'username': username,
+                                        'display_name': LdapUserPreferencesProvider()._get_pref(username,
'display_name'),
+                                        })
             else:
                 log.debug('LdapAuth: no user {} found in local mongo'.format(username))
                 raise exc.HTTPUnauthorized()
@@ -1233,22 +1235,26 @@ class LdapUserPreferencesProvider(UserPreferencesProvider):
     def get_pref(self, user, pref_name):
         from allura import model as M
         if pref_name in self.fields and user != M.User.anonymous():
-            con = ldap_conn()
-            try:
-                rs = con.search_s(ldap_user_dn(user.username), ldap.SCOPE_BASE)
-            except ldap.NO_SUCH_OBJECT:
-                rs = []
-            else:
-                con.unbind_s()
-            if not rs:
-                log.warning('LdapUserPref: No user record found for: {}'.format(user.username))
-                return ''
-            user_dn, user_attrs = rs[0]
-            ldap_attr = self.fields[pref_name]
-            return user_attrs[ldap_attr][0]  # assume single-valued list
+            self._get_pref(user.username, pref_name)
         else:
             return LocalUserPreferencesProvider().get_pref(user, pref_name)
 
+    def _get_pref(self, username, pref_name):
+        con = ldap_conn()
+        try:
+            rs = con.search_s(ldap_user_dn(username), ldap.SCOPE_BASE)
+        except ldap.NO_SUCH_OBJECT:
+            rs = []
+        else:
+            con.unbind_s()
+        if not rs:
+            log.warning('LdapUserPref: No user record found for: {}'.format(user.username))
+            return ''
+        user_dn, user_attrs = rs[0]
+        ldap_attr = self.fields[pref_name]
+        # assume single-valued list
+        return user_attrs[ldap_attr][0].decode('utf-8')
+
     def set_pref(self, user, pref_name, pref_value):
         if pref_name in self.fields:
             con = ldap_conn()

http://git-wip-us.apache.org/repos/asf/allura/blob/4921df8a/Allura/allura/tests/unit/test_ldap_auth_provider.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/unit/test_ldap_auth_provider.py b/Allura/allura/tests/unit/test_ldap_auth_provider.py
index cc37b53..417f200 100644
--- a/Allura/allura/tests/unit/test_ldap_auth_provider.py
+++ b/Allura/allura/tests/unit/test_ldap_auth_provider.py
@@ -72,13 +72,34 @@ class TestLdapAuthenticationProvider(object):
         self.provider.request.body = '&'.join(['%s=%s' % (k,v) for k,v in params.iteritems()])
         ldap.dn.escape_dn_chars = lambda x: x
 
-        dn = 'uid=%s,ou=users,dc=sf,dc=net' % params['username']
         self.provider._login()
+
+        dn = 'uid=%s,ou=users,dc=sf,dc=net' % params['username']
         ldap.initialize.assert_called_once_with('ldaps://localhost/')
         connection = ldap.initialize.return_value
         connection.bind_s.called_once_with(dn, 'test-password')
         connection.unbind_s.assert_called_once()
 
+    @patch('allura.lib.plugin.ldap')
+    def test_login_autoregister(self, ldap):
+        # covers ldap get_pref too, via the display_name fetch
+        params = {
+            'username': 'abc32590wr38',
+            'password': 'test-password',
+        }
+        self.provider.request.method = 'POST'
+        self.provider.request.body = '&'.join(['%s=%s' % (k,v) for k,v in params.iteritems()])
+        ldap.dn.escape_dn_chars = lambda x: x
+        dn = 'uid=%s,ou=users,dc=sf,dc=net' % params['username']
+        conn = ldap.initialize.return_value
+        conn.search_s.return_value = [(dn, {'cn': [u'åℒƒ'.encode('utf-8')]})]
+
+        self.provider._login()
+
+        user = M.User.query.get(username=params['username'])
+        assert user
+        assert_equal(user.display_name, u'åℒƒ')
+
     @patch('allura.lib.plugin.modlist')
     @patch('allura.lib.plugin.ldap')
     def test_register_user(self, ldap, modlist):

http://git-wip-us.apache.org/repos/asf/allura/blob/4921df8a/Allura/test.ini
----------------------------------------------------------------------
diff --git a/Allura/test.ini b/Allura/test.ini
index 129c305..ecf1639 100644
--- a/Allura/test.ini
+++ b/Allura/test.ini
@@ -146,6 +146,7 @@ auth.ldap.use_schroot = False
 auth.ldap.password.algorithm = 6
 auth.ldap.password.rounds = 6000
 auth.ldap.password.salt_len = 16
+user_prefs_storage.ldap.fields.display_name = cn
 
 auth.allow_user_to_disable_account = true
 auth.allow_edit_prefs = true


Mime
View raw message