36 |
36 |
37 @cachedproperty |
37 @cachedproperty |
38 def searchfilterstr(self): |
38 def searchfilterstr(self): |
39 """ ldap search string, including user-filter """ |
39 """ ldap search string, including user-filter """ |
40 return '(&%s)' % ''.join(self.source.base_filters) |
40 return '(&%s)' % ''.join(self.source.base_filters) |
|
41 |
|
42 @cachedproperty |
|
43 def searchgroupfilterstr(self): |
|
44 """ ldap search string, including user-filter """ |
|
45 return '(&%s)' % ''.join(self.source.group_base_filters) |
41 |
46 |
42 @cachedproperty |
47 @cachedproperty |
43 def user_source_entities_by_extid(self): |
48 def user_source_entities_by_extid(self): |
44 source = self.source |
49 source = self.source |
45 if source.user_base_dn.strip(): |
50 if source.user_base_dn.strip(): |
48 for userdict in source._search(self._cw, |
53 for userdict in source._search(self._cw, |
49 source.user_base_dn, |
54 source.user_base_dn, |
50 source.user_base_scope, |
55 source.user_base_scope, |
51 self.searchfilterstr, |
56 self.searchfilterstr, |
52 attrs)) |
57 attrs)) |
|
58 return {} |
|
59 |
|
60 @cachedproperty |
|
61 def group_source_entities_by_extid(self): |
|
62 source = self.source |
|
63 if source.group_base_dn.strip(): |
|
64 attrs = map(str, ['modifyTimestamp'] + source.group_attrs.keys()) |
|
65 return dict((groupdict['dn'], groupdict) |
|
66 for groupdict in source._search(self._cw, |
|
67 source.group_base_dn, |
|
68 source.group_base_scope, |
|
69 self.searchgroupfilterstr, |
|
70 attrs)) |
53 return {} |
71 return {} |
54 |
72 |
55 def _process(self, etype, sdict): |
73 def _process(self, etype, sdict): |
56 self.warning('fetched %s %s', etype, sdict) |
74 self.warning('fetched %s %s', etype, sdict) |
57 extid = sdict['dn'] |
75 extid = sdict['dn'] |
68 def process(self, url, raise_on_error=False): |
86 def process(self, url, raise_on_error=False): |
69 """IDataFeedParser main entry point""" |
87 """IDataFeedParser main entry point""" |
70 self.debug('processing ldapfeed source %s %s', self.source, self.searchfilterstr) |
88 self.debug('processing ldapfeed source %s %s', self.source, self.searchfilterstr) |
71 for userdict in self.user_source_entities_by_extid.itervalues(): |
89 for userdict in self.user_source_entities_by_extid.itervalues(): |
72 self._process('CWUser', userdict) |
90 self._process('CWUser', userdict) |
|
91 self.debug('processing ldapfeed source %s %s', self.source, self.searchgroupfilterstr) |
|
92 for groupdict in self.group_source_entities_by_extid.itervalues(): |
|
93 self._process('CWGroup', groupdict) |
73 |
94 |
74 def handle_deletion(self, config, session, myuris): |
95 def handle_deletion(self, config, session, myuris): |
75 if config['delete-entities']: |
96 if config['delete-entities']: |
76 super(DataFeedLDAPAdapter, self).handle_deletion(config, session, myuris) |
97 super(DataFeedLDAPAdapter, self).handle_deletion(config, session, myuris) |
77 return |
98 return |
112 etype must be CWUser or CWGroup """ |
133 etype must be CWUser or CWGroup """ |
113 if tdict is None: |
134 if tdict is None: |
114 tdict = {} |
135 tdict = {} |
115 if etype == 'CWUser': |
136 if etype == 'CWUser': |
116 items = self.source.user_attrs.iteritems() |
137 items = self.source.user_attrs.iteritems() |
|
138 elif etype == 'CWGroup': |
|
139 items = self.source.group_attrs.iteritems() |
117 for sattr, tattr in items: |
140 for sattr, tattr in items: |
118 if tattr not in self.non_attribute_keys: |
141 if tattr not in self.non_attribute_keys: |
119 try: |
142 try: |
120 tdict[tattr] = sdict[sattr] |
143 tdict[tattr] = sdict[sattr] |
121 except KeyError: |
144 except KeyError: |
151 groups = filter(None, [self._get_group(name) |
174 groups = filter(None, [self._get_group(name) |
152 for name in self.source.user_default_groups]) |
175 for name in self.source.user_default_groups]) |
153 if groups: |
176 if groups: |
154 entity.cw_set(in_group=groups) |
177 entity.cw_set(in_group=groups) |
155 self._process_email(entity, sourceparams) |
178 self._process_email(entity, sourceparams) |
|
179 elif etype == 'CWGroup': |
|
180 self._process_membership(entity, sourceparams) |
156 |
181 |
157 def is_deleted(self, extidplus, etype, eid): |
182 def is_deleted(self, extidplus, etype, eid): |
158 try: |
183 try: |
159 extid, _ = extidplus.rsplit('@@', 1) |
184 extid, _ = extidplus.rsplit('@@', 1) |
160 except ValueError: |
185 except ValueError: |
185 # source once import is finished |
210 # source once import is finished |
186 uri = userdict['dn'] + '@@' + emailaddr.encode('utf-8') |
211 uri = userdict['dn'] + '@@' + emailaddr.encode('utf-8') |
187 self.sourceuris.pop(uri, None) |
212 self.sourceuris.pop(uri, None) |
188 # XXX else check use_email relation? |
213 # XXX else check use_email relation? |
189 |
214 |
|
215 def _process_membership(self, entity, sourceparams): |
|
216 """ Find existing CWUsers with the same login as the memberUids in the |
|
217 CWGroup entity and create the in_group relationship """ |
|
218 mdate = sourceparams.get('modification_date') |
|
219 if (not mdate or mdate > entity.modification_date): |
|
220 self._cw.execute('DELETE U in_group G WHERE G eid %(g)s', |
|
221 {'g':entity.eid}) |
|
222 members = sourceparams.get(self.source.group_rev_attrs['member']) |
|
223 if members: |
|
224 members = ["'%s'" % e for e in members] |
|
225 rql = 'SET U in_group G WHERE G eid %%(g)s, U login IN (%s)' % ','.join(members) |
|
226 self._cw.execute(rql, {'g':entity.eid, }) |
|
227 |
190 @cached |
228 @cached |
191 def _get_group(self, name): |
229 def _get_group(self, name): |
192 try: |
230 try: |
193 return self._cw.execute('Any X WHERE X is CWGroup, X name %(name)s', |
231 return self._cw.execute('Any X WHERE X is CWGroup, X name %(name)s', |
194 {'name': name}).get_entity(0, 0) |
232 {'name': name}).get_entity(0, 0) |