~technofluid-team/openobject-addons/technofluid_multiple_installations

« back to all changes in this revision

Viewing changes to partner_ldap/wizard/synchronize.py

  • Committer: pinky
  • Date: 2006-12-07 13:41:40 UTC
  • Revision ID: pinky-dedd7f8a42bd4557112a0513082691b8590ad6cc
New trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##############################################################################
 
2
#
 
3
# Copyright (c) 2005-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
 
4
#
 
5
# $Id$
 
6
#
 
7
# WARNING: This program as such is intended to be used by professional
 
8
# programmers who take the whole responsability of assessing all potential
 
9
# consequences resulting from its eventual inadequacies and bugs
 
10
# End users who are looking for a ready-to-use solution with commercial
 
11
# garantees and support are strongly adviced to contract a Free Software
 
12
# Service Company
 
13
#
 
14
# This program is Free Software; you can redistribute it and/or
 
15
# modify it under the terms of the GNU General Public License
 
16
# as published by the Free Software Foundation; either version 2
 
17
# of the License, or (at your option) any later version.
 
18
#
 
19
# This program is distributed in the hope that it will be useful,
 
20
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
21
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
22
# GNU General Public License for more details.
 
23
#
 
24
# You should have received a copy of the GNU General Public License
 
25
# along with this program; if not, write to the Free Software
 
26
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
27
#
 
28
##############################################################################
 
29
 
 
30
import sys
 
31
if sys.version_info[:2] < (2, 4):
 
32
        from sets import Set as set
 
33
 
 
34
import time
 
35
import ldap
 
36
import ldap.modlist
 
37
 
 
38
import wizard
 
39
import pooler
 
40
 
 
41
BASE_DN = 'ou=addressbook,dc=localdomain'
 
42
 
 
43
def terp2ldap(data):
 
44
        res = {}
 
45
        for ldap_name, terp in LDAP_TERP_MAPPER.items():
 
46
                if callable(terp) and terp(data):
 
47
                        res[ldap_name] = [terp(data)]
 
48
                elif isinstance(terp, basestring) and data[terp]:
 
49
                        res[ldap_name] = ['%s' % data[terp]]
 
50
        return res
 
51
 
 
52
def get_lastname(dico):
 
53
        lnames = dico['name'].split(' ', 1)[1:]
 
54
        if lnames:
 
55
                return ' '.join(lnames)
 
56
        else:
 
57
                return dico['name']
 
58
 
 
59
def get_street(dico):
 
60
        return ' '.join([dico[x] for x in ('street', 'street2') if dico[x]])
 
61
 
 
62
def get_name(attribute):
 
63
        def func(dico):
 
64
                return dico[attribute][1]
 
65
        return func
 
66
 
 
67
 
 
68
LDAP_TERP_MAPPER = {
 
69
                'displayname': 'name',
 
70
                'mail': 'email',
 
71
                'o': get_name('partner_id'),
 
72
                'telephoneNumber': 'phone',
 
73
                'street': get_street,
 
74
                'postalcode': 'zip',
 
75
                'l': 'city',
 
76
                'cn': 'name',
 
77
                'sn': get_lastname,
 
78
                'uid': 'id',
 
79
                }
 
80
 
 
81
 
 
82
class sync_ldap(wizard.interface):
 
83
        
 
84
        _login_arch = '''<?xml version="1.0" ?>
 
85
        <form string="LDAP Credentials">
 
86
                <field name="ldap_host" colspan="4" />
 
87
                <field name="dn" colspan="4" />
 
88
                <field name="password" colspan="4" />
 
89
        </form>
 
90
        '''
 
91
 
 
92
        _login_fields = {
 
93
                        'ldap_host': {'string': 'LDAP Host', 'type': 'char', 'size': 128},
 
94
                        'dn': {'string': 'Distinguished name', 'type': 'char', 'size': 128},
 
95
                        'password': {'string': 'Password', 'type': 'char', 'size': 128},
 
96
                        }
 
97
 
 
98
        def _do_sync(self, cr, uid, data, context):
 
99
                l = ldap.open(data['form']['ldap_host'])
 
100
                l.simple_bind_s(data['form']['dn'], data['form']['password'])
 
101
                ldap_objs = dict(l.search_s(BASE_DN, ldap.SCOPE_ONELEVEL, 'objectclass=*',
 
102
                                                                LDAP_TERP_MAPPER.keys()))
 
103
                address = pooler.get_pool(cr.dbname).get('res.partner.address')
 
104
                terp_objs = dict([(x['id'], x) for x in address.read(cr, uid, address.search(cr, uid, []))])
 
105
                ldap_set = set([int(x['uid'][0]) for x in ldap_objs.values()])
 
106
                terp_set = set(terp_objs.keys())
 
107
                for to_delete in ldap_set - terp_set:
 
108
                        l.delete_s('uid=%s,%s' % (to_delete, BASE_DN))
 
109
                for to_add in terp_set - ldap_set:
 
110
                        new_dn = 'uid=%s,%s' % (to_add, BASE_DN)
 
111
                        ldap_data = {'objectclass' : ['organizationalPerson', 'inetOrgPerson']}
 
112
                        ldap_data.update(terp2ldap(terp_objs[to_add]))
 
113
                        l.add_s(new_dn, ldap.modlist.addModlist(ldap_data))
 
114
                        address.write(cr, uid, [to_add], {'dn' : new_dn})
 
115
                for to_update in terp_set.intersection(ldap_set):
 
116
                        current_dn = 'uid=%s,%s' % (to_update, BASE_DN)
 
117
                        modlist = ldap.modlist.modifyModlist(ldap_objs[current_dn], terp2ldap(terp_objs[to_update]))
 
118
                        if modlist:
 
119
                                l.modify_s(current_dn, modlist)
 
120
                return {}
 
121
 
 
122
        states = {
 
123
                'init' : {
 
124
                        'actions' : [],
 
125
                        'result' : {
 
126
                                'type' : 'form',
 
127
                                'arch' : _login_arch,
 
128
                                'fields' : _login_fields,
 
129
                                'state' : (('end', 'Cancel'),('sync', 'Synchronize'))
 
130
                                },
 
131
                },
 
132
                'sync' : {
 
133
                        'actions' : [ _do_sync ],
 
134
                        'result' : { 'type' : 'state', 'state' : 'end' },
 
135
                },
 
136
        }
 
137
 
 
138
 
 
139
sync_ldap('partners.sync_ldap')
 
140
 
 
141
# vim:noexpandtab: