From 11761b24a7a766119e71f5503bb49a9784ea9ef3 Mon Sep 17 00:00:00 2001 From: David Harper Date: Mon, 8 Dec 2014 17:54:24 +0100 Subject: [PATCH] Use the user name from ldap to search for existing tryton user ldap attributes can be case sensitive or insensitive, by using the login name from ldap to search for the tryton user, then the mapping of users between ldap and tryton should be one to one. issue4385 review12781002 --- res.py | 4 +++- tests/test_ldap_authentication.py | 19 +++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/res.py b/res.py index 6fa11f2e2d..03cd633c05 100644 --- a/res.py +++ b/res.py @@ -160,13 +160,15 @@ class User: if users and len(users) == 1: [(dn, attrs)] = users if password and con.simple_bind_s(dn, password): + # Use ldap uid so we always get the right case + login = attrs.get(uid, [login])[0] user_id, _ = cls._get_login(login) if user_id: LoginAttempt.remove(login) return user_id elif config.getboolean(section, 'create_user'): user, = cls.create([{ - 'name': attrs.get(uid, [login])[0], + 'name': login, 'login': login, }]) return user.id diff --git a/tests/test_ldap_authentication.py b/tests/test_ldap_authentication.py index 1e5426e2a9..0a4859d3cd 100644 --- a/tests/test_ldap_authentication.py +++ b/tests/test_ldap_authentication.py @@ -42,26 +42,33 @@ class LDAPAuthenticationTestCase(unittest.TestCase): con = initialize.return_value con.simple_bind_s.return_value = True if find: - ldap_search_user.return_value = [('dn', {})] + ldap_search_user.return_value = [('dn', {'uid': [find]})] else: ldap_search_user.return_value = None return User.get_login(login, password) # Test existing user - self.assertEqual(get_login('admin', 'admin', False), USER) - self.assertEqual(get_login('admin', 'admin', True), USER) + self.assertEqual(get_login('admin', 'admin', None), USER) + self.assertEqual(get_login('admin', 'admin', 'admin'), USER) + self.assertEqual(get_login('AdMiN', 'admin', 'admin'), USER) # Test new user - self.assertFalse(get_login('foo', 'bar', False)) - self.assertFalse(get_login('foo', 'bar', True)) + self.assertFalse(get_login('foo', 'bar', None)) + self.assertFalse(get_login('foo', 'bar', 'foo')) # Test create new user config.set(section, 'create_user', 'True') - user_id = get_login('foo', 'bar', True) + user_id = get_login('foo', 'bar', 'foo') foo, = User.search([('login', '=', 'foo')]) self.assertEqual(user_id, foo.id) self.assertEqual(foo.name, 'foo') + # Test create new user with different case + user_id = get_login('BaR', 'foo', 'bar') + bar, = User.search([('login', '=', 'bar')]) + self.assertEqual(user_id, bar.id) + self.assertEqual(bar.name, 'bar') + def test_parse_ldap_url(self): 'Test parse_ldap_url' self.assertEqual(