26 |
26 |
27 from cubicweb.server.sources import datafeed |
27 from cubicweb.server.sources import datafeed |
28 |
28 |
29 class DataFeedlDAPParser(datafeed.DataFeedParser): |
29 class DataFeedlDAPParser(datafeed.DataFeedParser): |
30 __regid__ = 'ldapfeed' |
30 __regid__ = 'ldapfeed' |
|
31 # attributes that may appears in source user_attrs dict which are not |
|
32 # attributes of the cw user |
|
33 non_attribute_keys = set(('email',)) |
31 |
34 |
32 def process(self, url, raise_on_error=False, partialcommit=True): |
35 def process(self, url, raise_on_error=False, partialcommit=True): |
33 """IDataFeedParser main entry point""" |
36 """IDataFeedParser main entry point""" |
34 source = self.source |
37 source = self.source |
35 searchstr = '(&%s)' % ''.join(source.base_filters) |
38 searchstr = '(&%s)' % ''.join(source.base_filters) |
40 for userdict in source._search(self._cw, source.user_base_dn, |
43 for userdict in source._search(self._cw, source.user_base_dn, |
41 source.user_base_scope, searchstr): |
44 source.user_base_scope, searchstr): |
42 entity = self.extid2entity(userdict['dn'], 'CWUser', **userdict) |
45 entity = self.extid2entity(userdict['dn'], 'CWUser', **userdict) |
43 if not self.created_during_pull(entity): |
46 if not self.created_during_pull(entity): |
44 self.notify_updated(entity) |
47 self.notify_updated(entity) |
45 attrs = dict( (k, v) for k, v in userdict.iteritems() |
48 attrs = self.ldap2cwattrs(userdict) |
46 if not k in ('dn', 'email') ) |
|
47 self.update_if_necessary(entity, attrs) |
49 self.update_if_necessary(entity, attrs) |
48 self._process_email(entity, userdict) |
50 self._process_email(entity, userdict) |
|
51 |
|
52 def ldap2cwattrs(self, sdict, tdict=None): |
|
53 if tdict is None: |
|
54 tdict = {} |
|
55 for sattr, tattr in self.source.user_attrs.iteritems(): |
|
56 if tattr not in self.non_attribute_keys: |
|
57 tdict[tattr] = sdict[sattr] |
|
58 return tdict |
49 |
59 |
50 def before_entity_copy(self, entity, sourceparams): |
60 def before_entity_copy(self, entity, sourceparams): |
51 if entity.__regid__ == 'EmailAddress': |
61 if entity.__regid__ == 'EmailAddress': |
52 entity.cw_edited['address'] = sourceparams['address'] |
62 entity.cw_edited['address'] = sourceparams['address'] |
53 else: |
63 else: |
54 for ldapattr, cwattr in self.source.user_attrs.iteritems(): |
64 self.ldap2cwattrs(sourceparams, entity.cw_edited) |
55 if cwattr != 'email': |
|
56 entity.cw_edited[cwattr] = sourceparams[ldapattr] |
|
57 return entity |
65 return entity |
58 |
66 |
59 def after_entity_copy(self, entity, sourceparams): |
67 def after_entity_copy(self, entity, sourceparams): |
60 super(DataFeedlDAPParser, self).after_entity_copy(entity, sourceparams) |
68 super(DataFeedlDAPParser, self).after_entity_copy(entity, sourceparams) |
61 if entity.__regid__ == 'EmailAddress': |
69 if entity.__regid__ == 'EmailAddress': |