[ldap] fix email synchronization code: bad rql query + avoid integrity error on creating the address + nicer use_email / primary_email handling stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 06 Apr 2010 16:04:50 +0200
branchstable
changeset 5153 3684ccae5cdc
parent 5150 7a9e71ee5671
child 5154 834269261ae4
[ldap] fix email synchronization code: bad rql query + avoid integrity error on creating the address + nicer use_email / primary_email handling
server/sources/ldapuser.py
--- a/server/sources/ldapuser.py	Tue Apr 06 10:29:41 2010 +0200
+++ b/server/sources/ldapuser.py	Tue Apr 06 16:04:50 2010 +0200
@@ -200,6 +200,7 @@
         except KeyError:
             return # no email in ldap, we're done
         session = self.repo.internal_session()
+        execute = session.execute
         try:
             cursor = session.system_sql("SELECT eid, extid FROM entities WHERE "
                                         "source='%s'" % self.uri)
@@ -210,20 +211,29 @@
                 if res:
                     ldapemailaddr = res[0].get(ldap_emailattr)
                     if ldapemailaddr:
-                        rset = session.execute('EmailAddress A WHERE '
-                                               'U use_email X, U eid %(u)s',
-                                               {'u': eid})
+                        rset = execute('Any X,A WHERE '
+                                       'X address A, U use_email X, U eid %(u)s',
+                                       {'u': eid})
                         ldapemailaddr = unicode(ldapemailaddr)
-                        for emailaddr, in rset:
+                        for emaileid, emailaddr, in rset:
                             if emailaddr == ldapemailaddr:
                                 break
                         else:
                             self.info('updating email address of user %s to %s',
                                       extid, ldapemailaddr)
-                            if rset:
-                                session.execute('SET X address %(addr)s WHERE '
-                                                'U primary_email X, U eid %(u)s',
-                                                {'addr': ldapemailaddr, 'u': eid})
+                            emailrset = execute('EmailAddress A WHERE A address %(addr)s',
+                                                {'addr': ldapemailaddr})
+                            if emailrset:
+                                execute('SET U use_email X WHERE '
+                                        'X eid %(x)s, U eid %(u)s',
+                                        {'x': emailrset[0][0], 'u': eid})
+                            elif rset:
+                                if not execute('SET X address %(addr)s WHERE '
+                                               'U primary_email X, U eid %(u)s',
+                                               {'addr': ldapemailaddr, 'u': eid}, 'u'):
+                                    execute('SET X address %(addr)s WHERE '
+                                            'X eid %(x)s',
+                                            {'addr': ldapemailaddr, 'x': rset[0][0]}, 'x')
                             else:
                                 # no email found, create it
                                 _insert_email(session, ldapemailaddr, eid)