1
##############################################################################
3
# Copyright (c) 2005-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
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
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.
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.
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.
28
##############################################################################
31
if sys.version_info[:2] < (2, 4):
32
from sets import Set as set
41
BASE_DN = 'ou=addressbook,dc=localdomain'
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]]
52
def get_lastname(dico):
53
lnames = dico['name'].split(' ', 1)[1:]
55
return ' '.join(lnames)
60
return ' '.join([dico[x] for x in ('street', 'street2') if dico[x]])
62
def get_name(attribute):
64
return dico[attribute][1]
69
'displayname': 'name',
71
'o': get_name('partner_id'),
72
'telephoneNumber': 'phone',
82
class sync_ldap(wizard.interface):
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" />
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},
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]))
119
l.modify_s(current_dn, modlist)
127
'arch' : _login_arch,
128
'fields' : _login_fields,
129
'state' : (('end', 'Cancel'),('sync', 'Synchronize'))
133
'actions' : [ _do_sync ],
134
'result' : { 'type' : 'state', 'state' : 'end' },
139
sync_ldap('partners.sync_ldap')