cubicweb/sobjects/ldapparser.py
changeset 11758 3f81636a75db
parent 11755 96ced95e4002
child 12567 26744ad37953
child 12618 3f125fdbcd70
--- a/cubicweb/sobjects/ldapparser.py	Thu Sep 29 11:44:19 2016 +0200
+++ b/cubicweb/sobjects/ldapparser.py	Thu Sep 29 22:23:16 2016 +0200
@@ -86,10 +86,18 @@
                                                         attrs))
         return {}
 
+    def process_urls(self, *args, **kwargs):
+        """IDataFeedParser main entry point."""
+        self._source_uris = {}
+        self._group_members = {}
+        error = super(DataFeedLDAPAdapter, self).process_urls(*args, **kwargs)
+        if not error:
+            self.handle_deletion()
+        return error
+
     def process(self, url, raise_on_error=False):
-        """IDataFeedParser main entry point"""
+        """Called once by process_urls (several URL are not expected with this parser)."""
         self.debug('processing ldapfeed source %s %s', self.source, self.searchfilterstr)
-        self._group_members = {}
         eeimporter = self.build_importer(raise_on_error)
         for name in self.source.user_default_groups:
             geid = self._get_group(name)
@@ -125,7 +133,10 @@
             rset = self._cw.execute('Any XURI, X WHERE X cwuri XURI, X is {0},'
                                     ' X cw_source S, S name %(source)s'.format(etype),
                                     {'source': self.source.uri})
-            extid2eid.update(dict((extid.encode('ascii'), eid) for extid, eid in rset))
+            for extid, eid in rset:
+                extid = extid.encode('ascii')
+                extid2eid[extid] = eid
+                self._source_uris[extid] = (eid, etype)
         existing_relations = {}
         for rtype in ('in_group', 'use_email', 'owned_by'):
             rql = 'Any S,O WHERE S {} O, S cw_source SO, SO eid %(s)s'.format(rtype)
@@ -154,6 +165,7 @@
                 # userPassword)
                 pwd = crypt_password(generate_password())
                 attrs['upassword'] = set([pwd])
+            self._source_uris.pop(userdict['dn'], None)
             extuser = importer.ExtEntity('CWUser', userdict['dn'].encode('ascii'), attrs)
             extuser.values['owned_by'] = set([extuser.extid])
             for extemail in self._process_email(extuser, userdict):
@@ -166,6 +178,7 @@
         # generate groups
         for groupdict in self.group_source_entities_by_extid.values():
             attrs = self.ldap2cwattrs(groupdict, 'CWGroup')
+            self._source_uris.pop(groupdict['dn'], None)
             extgroup = importer.ExtEntity('CWGroup', groupdict['dn'].encode('ascii'), attrs)
             yield extgroup
             # record group membership for later insertion
@@ -184,28 +197,20 @@
             rset = self._cw.execute('EmailAddress X WHERE X address %(addr)s',
                                     {'addr': emailaddr})
             emailextid = (userdict['dn'] + '@@' + emailaddr).encode('ascii')
+            self._source_uris.pop(emailextid, None)
             if not rset:
                 # not found, create it. first forge an external id
                 extuser.values.setdefault('use_email', []).append(emailextid)
                 yield importer.ExtEntity('EmailAddress', emailextid, dict(address=[emailaddr]))
-            elif self.source_uris:
-                # pop from source_uris anyway, else email may be removed by the
-                # source once import is finished
-                self.source_uris.pop(emailextid, None)
             # XXX else check use_email relation?
 
-    def handle_deletion(self, config, cnx, myuris):
-        if config['delete-entities']:
-            super(DataFeedLDAPAdapter, self).handle_deletion(config, cnx, myuris)
-            return
-        if myuris:
-            for extid, (eid, etype) in myuris.items():
-                if etype != 'CWUser' or not self.is_deleted(extid, etype, eid):
-                    continue
-                self.info('deactivate user %s', eid)
-                wf = cnx.entity_from_eid(eid).cw_adapt_to('IWorkflowable')
-                wf.fire_transition_if_possible('deactivate')
-        cnx.commit()
+    def handle_deletion(self):
+        for extid, (eid, etype) in self._source_uris.items():
+            if etype != 'CWUser' or not self.is_deleted(extid, etype, eid):
+                continue
+            self.info('deactivate user %s', eid)
+            wf = self._cw.entity_from_eid(eid).cw_adapt_to('IWorkflowable')
+            wf.fire_transition_if_possible('deactivate')
 
     def ensure_activated(self, entity):
         if entity.cw_etype == 'CWUser':