Pārlūkot izejas kodu

HUE-9180 [useradmin] Convert LDAP names to unicode to reduce length

Ying Chen 5 gadi atpakaļ
vecāks
revīzija
40ec71638d

+ 7 - 4
apps/useradmin/src/useradmin/ldap_access.py

@@ -28,6 +28,7 @@ import re
 import desktop.conf
 from desktop.lib.python_util import CaseInsensitiveDict
 from useradmin.models import User
+from django.utils.encoding import smart_text
 
 
 LOG = logging.getLogger(__name__)
@@ -238,13 +239,15 @@ class LdapConnection(object):
           }
 
           if 'givenName' in data:
-            if len(data['givenName'][0]) > 30:
+            first_name = smart_text(data['givenName'][0])
+            if len(first_name) > 30:
               LOG.warn('First name is truncated to 30 characters for [<User: %s>].' % ldap_info['username'])
-            ldap_info['first'] = data['givenName'][0][:30]
+            ldap_info['first'] = first_name[:30]
           if 'sn' in data:
-            if len(data['sn'][0]) > 30:
+            last_name = smart_text(data['sn'][0])
+            if len(last_name) > 30:
               LOG.warn('Last name is truncated to 30 characters for [<User: %s>].' % ldap_info['username'])
-            ldap_info['last'] = data['sn'][0][:30]
+            ldap_info['last'] = last_name[:30]
           if 'mail' in data:
             ldap_info['email'] = data['mail'][0]
           # memberOf and isMemberOf should be the same if they both exist

+ 24 - 1
apps/useradmin/src/useradmin/test_ldap.py

@@ -18,12 +18,14 @@
 
 from __future__ import absolute_import
 import ldap
+import sys
 
 from django.conf import settings
+from django.db.utils import DatabaseError
 from django.urls import reverse
 from nose.plugins.attrib import attr
 from nose.plugins.skip import SkipTest
-from nose.tools import assert_true, assert_equal, assert_false
+from nose.tools import assert_true, assert_equal, assert_false, assert_raises
 
 import desktop.conf
 from desktop.lib.test_utils import grant_access
@@ -628,6 +630,27 @@ class TestUserAdminLdap(BaseUserAdminTests):
     assert_true(user_info[0]['first'] == 'Firstnamehasmorethanthirtychar', user_info[0]['first'])
     assert_true(user_info[0]['last'] == 'Lastnamehasmorethanthirtychara', user_info[0]['last'])
 
+    test_ldap_data = [('uid=thaiuser,ou=people,dc=sec,dc=test,dc=com', {'objectClass': ['inetOrgPerson', 'posixAccount', 'shadowAccount'], 'mail': ['thaiuser@sec.test.com'], 'givenName': ['ดีหรือแย่ อย่าไปแคร์ คนนินทา'], 'uid': ['thaiuser'], 'sn': ['ชมหรือด่า อย่าไปรับ ให้กลับคืนไป']})]
+
+    # Checking if first/last name in Thai truncation works for LDAP imports
+    user_info = ldap_access.LdapConnection._transform_find_user_results(result_data=test_ldap_data, user_name_attr='uid')
+    assert_false(len(user_info[0]['first']) > 30)
+    assert_false(len(user_info[0]['last']) > 30)
+    good_first_name = u'ดีหรือแย่ อย่าไปแคร์ คนนินทา'
+    truncated_last_name = u'ชมหรือด่า อย่าไปรับ ให้กลับคืนไป'[:30]
+    assert_true(user_info[0]['first'], good_first_name)
+    assert_true(user_info[0]['last'], truncated_last_name)
+
+    user, created = ldap_access.get_or_create_ldap_user(username=user_info[0]['username'])
+    user.first_name = user_info[0]['first']
+    user.last_name = 'ชมหรือด่า อย่าไปรับ ให้กลับคืนไป'[:30]
+    if sys.version_info[0] == 2:
+      assert_raises(DatabaseError, user.save) # 'Incorrect string value: '\\xE0\\xB8\\' for column 'last_name' at row 1'
+
+    user.last_name = user_info[0]['last']
+    user.save()
+    assert_true(user.first_name, good_first_name)
+    assert_true(user.last_name, truncated_last_name)
 
   def test_add_ldap_groups(self):
     URL = reverse(add_ldap_groups)