|
1 # copyright 2011-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
3 # |
|
4 # This file is part of CubicWeb. |
|
5 # |
|
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
|
7 # terms of the GNU Lesser General Public License as published by the Free |
|
8 # Software Foundation, either version 2.1 of the License, or (at your option) |
|
9 # any later version. |
|
10 # |
|
11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
|
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
14 # details. |
|
15 # |
|
16 # You should have received a copy of the GNU Lesser General Public License along |
|
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
|
18 """cubicweb ldap feed source |
|
19 |
|
20 unlike ldapuser source, this source is copy based and will import ldap content |
|
21 (beside passwords for authentication) into the system source. |
|
22 """ |
|
23 from base64 import b64decode |
|
24 |
|
25 from logilab.common.decorators import cached |
|
26 |
|
27 from cubicweb.server.sources import datafeed |
|
28 |
|
29 class DataFeedlDAPParser(datafeed.DataFeedParser): |
|
30 __regid__ = 'ldapfeed' |
|
31 |
|
32 def process(self, url, raise_on_error=False, partialcommit=True): |
|
33 """IDataFeedParser main entry point""" |
|
34 source = self.source |
|
35 searchstr = '(&%s)' % ''.join(source.base_filters) |
|
36 try: |
|
37 ldap_emailattr = source.user_rev_attrs['email'] |
|
38 except KeyError: |
|
39 ldap_emailattr = None |
|
40 for userdict in source._search(self._cw, source.user_base_dn, |
|
41 source.user_base_scope, searchstr): |
|
42 entity = self.extid2entity(userdict['dn'], 'CWUser', **userdict) |
|
43 if not self.created_during_pull(entity): |
|
44 self.notify_updated(entity) |
|
45 attrs = dict( (k, v) for k, v in userdict.iteritems() |
|
46 if not k in ('dn', 'email') ) |
|
47 self.update_if_necessary(entity, attrs) |
|
48 self._process_email(entity, userdict) |
|
49 |
|
50 def before_entity_copy(self, entity, sourceparams): |
|
51 if entity.__regid__ == 'EmailAddress': |
|
52 entity.cw_edited['address'] = sourceparams['address'] |
|
53 else: |
|
54 for ldapattr, cwattr in self.source.user_attrs.iteritems(): |
|
55 if cwattr != 'email': |
|
56 entity.cw_edited[cwattr] = sourceparams[ldapattr] |
|
57 return entity |
|
58 |
|
59 def after_entity_copy(self, entity, sourceparams): |
|
60 super(DataFeedlDAPParser, self).after_entity_copy(entity, sourceparams) |
|
61 if entity.__regid__ == 'EmailAddress': |
|
62 return |
|
63 groups = [self._get_group(n) for n in self.source.user_default_groups] |
|
64 entity.set_relations(in_group=groups) |
|
65 self._process_email(entity, sourceparams) |
|
66 |
|
67 def is_deleted(self, extid, etype, eid): |
|
68 try: |
|
69 extid, _ = extid.rsplit('@@', 1) |
|
70 except ValueError: |
|
71 pass |
|
72 return self.source.object_exists_in_ldap(extid) |
|
73 |
|
74 def _process_email(self, entity, userdict): |
|
75 try: |
|
76 emailaddrs = userdict[self.source.user_rev_attrs['email']] |
|
77 except KeyError: |
|
78 return # no email for that user, nothing to do |
|
79 if not isinstance(emailaddrs, list): |
|
80 emailaddrs = [emailaddrs] |
|
81 for emailaddr in emailaddrs: |
|
82 # search for existant email first, may be coming from another source |
|
83 rset = self._cw.execute('EmailAddress X WHERE X address %(addr)s', |
|
84 {'addr': emailaddr}) |
|
85 if not rset: |
|
86 # not found, create it. first forge an external id |
|
87 emailextid = userdict['dn'] + '@@' + emailaddr |
|
88 email = self.extid2entity(emailextid, 'EmailAddress', |
|
89 address=emailaddr) |
|
90 if entity.primary_email: |
|
91 entity.set_relations(use_email=email) |
|
92 else: |
|
93 entity.set_relations(primary_email=email) |
|
94 # XXX else check use_email relation? |
|
95 |
|
96 @cached |
|
97 def _get_group(self, name): |
|
98 return self._cw.execute('Any X WHERE X is CWGroup, X name %(name)s', |
|
99 {'name': name}).get_entity(0, 0) |