[ldapfeed] add support for multiple email addresses from ldap
authorPierre-Yves David <pierre-yves.david@logilab.fr>
Wed, 24 Apr 2013 14:00:52 +0200
changeset 8917 685b93559e33
parent 8916 2a424950463d
child 8918 43fd866e8f8a
[ldapfeed] add support for multiple email addresses from ldap Multiple emails case was ignored before this change. email test
server/ldaputils.py
server/test/unittest_ldapsource.py
sobjects/ldapparser.py
--- a/server/ldaputils.py	Wed Apr 24 15:37:43 2013 +0200
+++ b/server/ldaputils.py	Wed Apr 24 14:00:52 2013 +0200
@@ -342,10 +342,11 @@
                     value = utils.crypt_password(value)
                 itemdict[key] = Binary(value)
             else:
-                for i, val in enumerate(value):
-                    value[i] = unicode(val, 'utf-8', 'replace')
-                if isinstance(value, list) and len(value) == 1:
+                value = [unicode(val, 'utf-8', 'replace') for val in value]
+                if len(value) == 1:
                     itemdict[key] = value = value[0]
+                else:
+                    itemdict[key] = value
         return itemdict
 
     def _process_no_such_object(self, session, dn):
--- a/server/test/unittest_ldapsource.py	Wed Apr 24 15:37:43 2013 +0200
+++ b/server/test/unittest_ldapsource.py	Wed Apr 24 14:00:52 2013 +0200
@@ -35,7 +35,10 @@
 
 from cubicweb.server.sources.ldapuser import GlobTrFunc, UnknownEid, RQL2LDAPFilter
 
-CONFIG_LDAPFEED = CONFIG_LDAPUSER = u'''user-base-dn=ou=People,dc=cubicweb,dc=test'''
+CONFIG_LDAPFEED = CONFIG_LDAPUSER = u'''
+user-base-dn=ou=People,dc=cubicweb,dc=test
+user-attrs-map=uid=login,mail=email,userPassword=upassword
+'''
 
 URL = None
 
@@ -226,7 +229,12 @@
         self.assertEqual(e.in_group[0].name, 'users')
         self.assertEqual(e.owned_by[0].login, 'syt')
         self.assertEqual(e.created_by, ())
-        self.assertEqual(e.primary_email[0].address, 'Sylvain Thenault')
+        addresses = [pe.address for pe in e.use_email]
+        addresses.sort()
+        self.assertEqual(['sylvain.thenault@logilab.fr', 'syt@logilab.fr'],
+                         addresses)
+        self.assertIn(e.primary_email[0].address, ['sylvain.thenault@logilab.fr',
+                                                   'syt@logilab.fr'])
         # email content should be indexed on the user
         rset = self.sexecute('CWUser X WHERE X has_text "thenault"')
         self.assertEqual(rset.rows, [[e.eid]])
@@ -362,7 +370,13 @@
         self.assertEqual(e.in_group[0].name, 'users')
         self.assertEqual(e.owned_by[0].login, 'syt')
         self.assertEqual(e.created_by, ())
-        self.assertEqual(e.primary_email[0].address, 'Sylvain Thenault')
+        addresses = [pe.address for pe in e.use_email]
+        addresses.sort()
+        # should habe two element but ldapuser seems buggy. It's going to be dropped anyway.
+        self.assertEqual(['sylvain.thenault@logilab.fr',], # 'syt@logilab.fr'],
+                         addresses)
+        self.assertIn(e.primary_email[0].address,
+                      ['sylvain.thenault@logilab.fr', 'syt@logilab.fr'])
         # email content should be indexed on the user
         rset = self.sexecute('CWUser X WHERE X has_text "thenault"')
         self.assertEqual(rset.rows, [[e.eid]])
--- a/sobjects/ldapparser.py	Wed Apr 24 15:37:43 2013 +0200
+++ b/sobjects/ldapparser.py	Wed Apr 24 14:00:52 2013 +0200
@@ -161,10 +161,7 @@
                 emailextid = userdict['dn'] + '@@' + emailaddr
                 email = self.extid2entity(emailextid, 'EmailAddress',
                                           address=emailaddr)
-                if entity.primary_email:
-                    entity.cw_set(use_email=email)
-                else:
-                    entity.cw_set(primary_email=email)
+                entity.cw_set(use_email=email)
             elif self.sourceuris:
                 # pop from sourceuris anyway, else email may be removed by the
                 # source once import is finished