sobjects/ldapparser.py
changeset 8430 5bee87a14bb1
parent 8427 63c4c65e4db1
child 8431 16c04cc464dd
equal deleted inserted replaced
8429:cad2d8e03b33 8430:5bee87a14bb1
    27 from cubicweb import Binary
    27 from cubicweb import Binary
    28 from cubicweb.server.utils import crypt_password
    28 from cubicweb.server.utils import crypt_password
    29 from cubicweb.server.sources import datafeed
    29 from cubicweb.server.sources import datafeed
    30 
    30 
    31 
    31 
    32 class DataFeedlDAPParser(datafeed.DataFeedParser):
    32 class DataFeedLDAPAdapter(datafeed.DataFeedParser):
    33     __regid__ = 'ldapfeed'
    33     __regid__ = 'ldapfeed'
    34     # attributes that may appears in source user_attrs dict which are not
    34     # attributes that may appears in source user_attrs dict which are not
    35     # attributes of the cw user
    35     # attributes of the cw user
    36     non_attribute_keys = set(('email',))
    36     non_attribute_keys = set(('email',))
    37 
    37 
    47             if entity is not None and not self.created_during_pull(entity):
    47             if entity is not None and not self.created_during_pull(entity):
    48                 self.notify_updated(entity)
    48                 self.notify_updated(entity)
    49                 attrs = self.ldap2cwattrs(userdict)
    49                 attrs = self.ldap2cwattrs(userdict)
    50                 self.update_if_necessary(entity, attrs)
    50                 self.update_if_necessary(entity, attrs)
    51                 self._process_email(entity, userdict)
    51                 self._process_email(entity, userdict)
       
    52 
       
    53 
       
    54     def handle_deletion(self, config, session, myuris):
       
    55         if config['delete-entities']:
       
    56             print 'DELETE'
       
    57             super(DataFeedLDAPAdapter, self).handle_deletion(config, session, myuris)
       
    58         if myuris:
       
    59             byetype = {}
       
    60             for extid, (eid, etype) in myuris.iteritems():
       
    61                 if self.is_deleted(extid, etype, eid):
       
    62                     byetype.setdefault(etype, []).append(str(eid))
       
    63 
       
    64             for etype, eids in byetype.iteritems():
       
    65                 if etype != 'CWUser':
       
    66                     continue
       
    67                 self.warning('deactivate %s %s entities', len(eids), etype)
       
    68                 for eid in eids:
       
    69                     wf = session.entity_from_eid(eid).cw_adapt_to('IWorkflowable')
       
    70                     wf.fire_transition('deactivate')
       
    71         session.commit(free_cnxset=False)
    52 
    72 
    53     def ldap2cwattrs(self, sdict, tdict=None):
    73     def ldap2cwattrs(self, sdict, tdict=None):
    54         if tdict is None:
    74         if tdict is None:
    55             tdict = {}
    75             tdict = {}
    56         for sattr, tattr in self.source.user_attrs.iteritems():
    76         for sattr, tattr in self.source.user_attrs.iteritems():
    70                 pwd = crypt_password(generate_password())
    90                 pwd = crypt_password(generate_password())
    71                 entity.cw_edited['upassword'] = Binary(pwd)
    91                 entity.cw_edited['upassword'] = Binary(pwd)
    72         return entity
    92         return entity
    73 
    93 
    74     def after_entity_copy(self, entity, sourceparams):
    94     def after_entity_copy(self, entity, sourceparams):
    75         super(DataFeedlDAPParser, self).after_entity_copy(entity, sourceparams)
    95         super(DataFeedLDAPAdapter, self).after_entity_copy(entity, sourceparams)
    76         if entity.__regid__ == 'EmailAddress':
    96         if entity.__regid__ == 'EmailAddress':
    77             return
    97             return
    78         groups = [self._get_group(n) for n in self.source.user_default_groups]
    98         groups = [self._get_group(n) for n in self.source.user_default_groups]
    79         entity.set_relations(in_group=groups)
    99         entity.set_relations(in_group=groups)
    80         self._process_email(entity, sourceparams)
   100         self._process_email(entity, sourceparams)
    82     def is_deleted(self, extid, etype, eid):
   102     def is_deleted(self, extid, etype, eid):
    83         try:
   103         try:
    84             extid, _ = extid.rsplit('@@', 1)
   104             extid, _ = extid.rsplit('@@', 1)
    85         except ValueError:
   105         except ValueError:
    86             pass
   106             pass
    87         return self.source.object_exists_in_ldap(extid)
   107         return not self.source.object_exists_in_ldap(extid)
    88 
   108 
    89     def _process_email(self, entity, userdict):
   109     def _process_email(self, entity, userdict):
    90         try:
   110         try:
    91             emailaddrs = userdict[self.source.user_rev_attrs['email']]
   111             emailaddrs = userdict[self.source.user_rev_attrs['email']]
    92         except KeyError:
   112         except KeyError: