76 @cachedproperty |
76 @cachedproperty |
77 def group_source_entities_by_extid(self): |
77 def group_source_entities_by_extid(self): |
78 source = self.source |
78 source = self.source |
79 if source.group_base_dn.strip(): |
79 if source.group_base_dn.strip(): |
80 attrs = list(map(str, ['modifyTimestamp'] + list(source.group_attrs.keys()))) |
80 attrs = list(map(str, ['modifyTimestamp'] + list(source.group_attrs.keys()))) |
81 return dict((groupdict['dn'], groupdict) |
81 return dict((groupdict['dn'].encode('ascii'), groupdict) |
82 for groupdict in source._search(self._cw, |
82 for groupdict in source._search(self._cw, |
83 source.group_base_dn, |
83 source.group_base_dn, |
84 source.group_base_scope, |
84 source.group_base_scope, |
85 self.searchgroupfilterstr, |
85 self.searchgroupfilterstr, |
86 attrs)) |
86 attrs)) |
118 {'s': self.source.eid}) |
118 {'s': self.source.eid}) |
119 |
119 |
120 def build_importer(self, raise_on_error): |
120 def build_importer(self, raise_on_error): |
121 """Instantiate and configure an importer""" |
121 """Instantiate and configure an importer""" |
122 etypes = ('CWUser', 'EmailAddress', 'CWGroup') |
122 etypes = ('CWUser', 'EmailAddress', 'CWGroup') |
123 extid2eid = importer.cwuri2eid(self._cw, etypes, source_eid=self.source.eid) |
123 extid2eid = dict((x.encode('ascii'), y) for x, y in |
|
124 importer.cwuri2eid(self._cw, etypes, source_eid=self.source.eid).items()) |
124 existing_relations = {} |
125 existing_relations = {} |
125 for rtype in ('in_group', 'use_email', 'owned_by'): |
126 for rtype in ('in_group', 'use_email', 'owned_by'): |
126 rql = 'Any S,O WHERE S {} O, S cw_source SO, SO eid %(s)s'.format(rtype) |
127 rql = 'Any S,O WHERE S {} O, S cw_source SO, SO eid %(s)s'.format(rtype) |
127 rset = self._cw.execute(rql, {'s': self.source.eid}) |
128 rset = self._cw.execute(rql, {'s': self.source.eid}) |
128 existing_relations[rtype] = set(tuple(x) for x in rset) |
129 existing_relations[rtype] = set(tuple(x) for x in rset) |
147 if not pwd: |
148 if not pwd: |
148 # generate a dumb password if not fetched from ldap (see |
149 # generate a dumb password if not fetched from ldap (see |
149 # userPassword) |
150 # userPassword) |
150 pwd = crypt_password(generate_password()) |
151 pwd = crypt_password(generate_password()) |
151 attrs['upassword'] = set([Binary(pwd)]) |
152 attrs['upassword'] = set([Binary(pwd)]) |
152 extuser = importer.ExtEntity('CWUser', userdict['dn'], attrs) |
153 extuser = importer.ExtEntity('CWUser', userdict['dn'].encode('ascii'), attrs) |
153 extuser.values['owned_by'] = set([extuser.extid]) |
154 extuser.values['owned_by'] = set([extuser.extid]) |
154 for extemail in self._process_email(extuser, userdict): |
155 for extemail in self._process_email(extuser, userdict): |
155 yield extemail |
156 yield extemail |
156 groups = list(filter(None, [self._get_group(name) |
157 groups = list(filter(None, [self._get_group(name) |
157 for name in self.source.user_default_groups])) |
158 for name in self.source.user_default_groups])) |
159 extuser.values['in_group'] = groups |
160 extuser.values['in_group'] = groups |
160 yield extuser |
161 yield extuser |
161 # generate groups |
162 # generate groups |
162 for groupdict in self.group_source_entities_by_extid.values(): |
163 for groupdict in self.group_source_entities_by_extid.values(): |
163 attrs = self.ldap2cwattrs(groupdict, 'CWGroup') |
164 attrs = self.ldap2cwattrs(groupdict, 'CWGroup') |
164 extgroup = importer.ExtEntity('CWGroup', groupdict['dn'], attrs) |
165 extgroup = importer.ExtEntity('CWGroup', groupdict['dn'].encode('ascii'), attrs) |
165 yield extgroup |
166 yield extgroup |
166 # record group membership for later insertion |
167 # record group membership for later insertion |
167 members = groupdict.get(self.source.group_rev_attrs['member'], ()) |
168 members = groupdict.get(self.source.group_rev_attrs['member'], ()) |
168 self._group_members[attrs['name']] = members |
169 self._group_members[attrs['name']] = members |
169 |
170 |
176 emailaddrs = [emailaddrs] |
177 emailaddrs = [emailaddrs] |
177 for emailaddr in emailaddrs: |
178 for emailaddr in emailaddrs: |
178 # search for existing email first, may be coming from another source |
179 # search for existing email first, may be coming from another source |
179 rset = self._cw.execute('EmailAddress X WHERE X address %(addr)s', |
180 rset = self._cw.execute('EmailAddress X WHERE X address %(addr)s', |
180 {'addr': emailaddr}) |
181 {'addr': emailaddr}) |
|
182 emailextid = (userdict['dn'] + '@@' + emailaddr).encode('ascii') |
181 if not rset: |
183 if not rset: |
182 # not found, create it. first forge an external id |
184 # not found, create it. first forge an external id |
183 emailextid = userdict['dn'] + '@@' + emailaddr |
|
184 extuser.values.setdefault('use_email', []).append(emailextid) |
185 extuser.values.setdefault('use_email', []).append(emailextid) |
185 yield importer.ExtEntity('EmailAddress', emailextid, dict(address=[emailaddr])) |
186 yield importer.ExtEntity('EmailAddress', emailextid, dict(address=[emailaddr])) |
186 elif self.sourceuris: |
187 elif self.sourceuris: |
187 # pop from sourceuris anyway, else email may be removed by the |
188 # pop from sourceuris anyway, else email may be removed by the |
188 # source once import is finished |
189 # source once import is finished |
189 emailextid = userdict['dn'] + '@@' + emailaddr |
|
190 self.sourceuris.pop(emailextid, None) |
190 self.sourceuris.pop(emailextid, None) |
191 # XXX else check use_email relation? |
191 # XXX else check use_email relation? |
192 |
192 |
193 def handle_deletion(self, config, cnx, myuris): |
193 def handle_deletion(self, config, cnx, myuris): |
194 if config['delete-entities']: |
194 if config['delete-entities']: |