|
|
@ -9,10 +9,10 @@ |
|
|
|
# Contribution : Joel Grand-Guillaume |
|
|
|
# |
|
|
|
# WARNING: This program as such is intended to be used by professional |
|
|
|
# programmers who take the whole responsability of assessing all potential |
|
|
|
# programmers who take the whole responsibility of assessing all potential |
|
|
|
# consequences resulting from its eventual inadequacies and bugs |
|
|
|
# End users who are looking for a ready-to-use solution with commercial |
|
|
|
# garantees and support are strongly adviced to contract a Free Software |
|
|
|
# guarantees and support are strongly advised to contract a Free Software |
|
|
|
# Service Company |
|
|
|
# |
|
|
|
# This program is Free Software; you can redistribute it and/or |
|
|
@ -30,22 +30,24 @@ |
|
|
|
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
|
|
# |
|
|
|
############################################################################## |
|
|
|
#TODO FInd why company parameter are cached |
|
|
|
# TODO Find why company parameter are cached |
|
|
|
|
|
|
|
import re |
|
|
|
import unicodedata |
|
|
|
import netsvc |
|
|
|
from openerp import netsvc |
|
|
|
try: |
|
|
|
import ldap |
|
|
|
import ldap.modlist |
|
|
|
except: |
|
|
|
print 'python ldap not installed please install it in order to use this module' |
|
|
|
print('python ldap not installed please install it in order to use this module') |
|
|
|
|
|
|
|
|
|
|
|
from osv import osv, fields |
|
|
|
from tools.translate import _ |
|
|
|
from openerp.osv import orm, fields |
|
|
|
from openerp.tools.translate import _ |
|
|
|
|
|
|
|
logger = netsvc.Logger() |
|
|
|
|
|
|
|
|
|
|
|
class LdapConnMApper(object): |
|
|
|
"""LdapConnMApper: push specific fields from the Terp Partner_contacts to the |
|
|
|
LDAP schema inetOrgPerson. Ldap bind options are stored in company.r""" |
|
|
@ -79,7 +81,7 @@ class LdapConnMApper(object): |
|
|
|
if company.ldap_active: |
|
|
|
for param in mand: |
|
|
|
if not param: |
|
|
|
raise osv.except_osv(_('Warning !'), |
|
|
|
raise orm.except_orm(_('Warning !'), |
|
|
|
_('An LDAP parameter is missing for company %s') % (company.name, )) |
|
|
|
|
|
|
|
def get_connexion(self): |
|
|
@ -94,7 +96,7 @@ class LdapConnMApper(object): |
|
|
|
return self.connexion |
|
|
|
|
|
|
|
|
|
|
|
class LDAPAddress(osv.osv): |
|
|
|
class LDAPAddress(orm.Model): |
|
|
|
"""Override the CRUD of the objet in order to dynamically bind to ldap""" |
|
|
|
_inherit = 'res.partner.address' |
|
|
|
ldapMapper = None |
|
|
@ -108,7 +110,7 @@ class LDAPAddress(osv.osv): |
|
|
|
'column name to lastname ;')) |
|
|
|
cr.execute('ALTER TABLE res_partner_address RENAME column name to lastname ;') |
|
|
|
|
|
|
|
except Exception, e: |
|
|
|
except Exception: |
|
|
|
cr.rollback() |
|
|
|
logger.notifyChannel(_('LDAP address init'), |
|
|
|
netsvc.LOG_INFO, |
|
|
@ -129,7 +131,7 @@ class LDAPAddress(osv.osv): |
|
|
|
reads = self.read(cursor, uid, ids, ['lastname', 'firstname']) |
|
|
|
res = [] |
|
|
|
for record in reads: |
|
|
|
## We want to have " firstname" or "" |
|
|
|
# We want to have " firstname" or "" |
|
|
|
name = self._compute_name(record['firstname'], record['lastname']) |
|
|
|
res.append((record['id'], name)) |
|
|
|
return dict(res) |
|
|
@ -160,7 +162,6 @@ class LDAPAddress(osv.osv): |
|
|
|
# res.append(add.id) |
|
|
|
# return self.name_get(cursor, user, res, context) |
|
|
|
|
|
|
|
|
|
|
|
_columns = { |
|
|
|
'firstname': fields.char('First name', size=256), |
|
|
|
'lastname': fields.char('Last name', size=256), |
|
|
@ -189,7 +190,7 @@ class LDAPAddress(osv.osv): |
|
|
|
ids = [ids] |
|
|
|
if ids: |
|
|
|
self.validate_entries(vals, cursor, uid, ids) |
|
|
|
if context.has_key('init_mode') and context['init_mode'] : |
|
|
|
if context.get('init_mode'): |
|
|
|
success = True |
|
|
|
else: |
|
|
|
success = super(LDAPAddress, self).write(cursor, uid, ids, |
|
|
@ -200,7 +201,8 @@ class LDAPAddress(osv.osv): |
|
|
|
return success |
|
|
|
|
|
|
|
def unlink(self, cursor, uid, ids, context=None): |
|
|
|
if not context: context = {} |
|
|
|
if context is None: |
|
|
|
context = {} |
|
|
|
if ids: |
|
|
|
self.getconn(cursor, uid, {}) |
|
|
|
if not isinstance(ids, list): |
|
|
@ -230,12 +232,11 @@ class LDAPAddress(osv.osv): |
|
|
|
phone = vals.get('phone', False) |
|
|
|
fax = vals.get('fax', False) |
|
|
|
mobile = vals.get('mobile', False) |
|
|
|
lastname = vals.get('lastname', False) |
|
|
|
private_phone = vals.get('private_phone', False) |
|
|
|
if email: |
|
|
|
if re.match("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$", |
|
|
|
email) is None: |
|
|
|
raise osv.except_osv(_('Warning !'), |
|
|
|
raise orm.except_orm(_('Warning !'), |
|
|
|
_('Please enter a valid e-mail')) |
|
|
|
phones = (('phone', phone), ('fax', fax), ('mobile', mobile), |
|
|
|
('private_phone', private_phone)) |
|
|
@ -243,15 +244,16 @@ class LDAPAddress(osv.osv): |
|
|
|
phone_number = phone_tuple[1] |
|
|
|
if phone_number: |
|
|
|
if not phone_number.startswith('+'): |
|
|
|
raise osv.except_osv(_('Warning !'), |
|
|
|
raise orm.except_orm(_('Warning !'), |
|
|
|
_('Please enter a valid phone number in %s' |
|
|
|
' international format (i.e. leading +)') % phone_tuple[0]) |
|
|
|
|
|
|
|
def getVals(self, att_name, key, vals, dico, uid, ids, cursor, context=None): |
|
|
|
"""map to values to dict""" |
|
|
|
if not context: context = {} |
|
|
|
## We explicitely test False value |
|
|
|
if vals.get(key, False) != False: |
|
|
|
if not context: |
|
|
|
context = {} |
|
|
|
# We explicitly test False value |
|
|
|
if vals.get(key, False) is not False: |
|
|
|
dico[att_name] = vals[key] |
|
|
|
else: |
|
|
|
if context.get('init_mode'): |
|
|
@ -260,18 +262,18 @@ class LDAPAddress(osv.osv): |
|
|
|
if tmp.get(key, False): |
|
|
|
dico[att_name] = tmp[key] |
|
|
|
|
|
|
|
|
|
|
|
def _un_unicodize_buf(self, in_buf): |
|
|
|
if isinstance(in_buf, unicode): |
|
|
|
try: |
|
|
|
return in_buf.encode() |
|
|
|
except Exception, e: |
|
|
|
except Exception: |
|
|
|
return unicodedata.normalize("NFKD", in_buf).encode('ascii', 'ignore') |
|
|
|
return in_buf |
|
|
|
|
|
|
|
def unUnicodize(self, indict): |
|
|
|
"""remove unicode data of modlist as unicode is not supported |
|
|
|
by python-ldap librairy till version 2.7""" |
|
|
|
by python-ldap library till version 2.7 |
|
|
|
""" |
|
|
|
for key in indict: |
|
|
|
if not isinstance(indict[key], list): |
|
|
|
indict[key] = self._un_unicodize_buf(indict[key]) |
|
|
@ -282,7 +284,6 @@ class LDAPAddress(osv.osv): |
|
|
|
indict[key] = nonutfArray |
|
|
|
|
|
|
|
def addNeededFields(self, id, vals, cursor, uid): |
|
|
|
keys = vals.keys() |
|
|
|
previousvalue = self.browse(cursor, uid, [id])[0] |
|
|
|
if not vals.get('partner_id'): |
|
|
|
vals['partner_id'] = previousvalue.partner_id.id |
|
|
@ -296,7 +297,6 @@ class LDAPAddress(osv.osv): |
|
|
|
"""Mapp ResPArtner adress to moddlist""" |
|
|
|
self.addNeededFields(id, vals, cursor, uid) |
|
|
|
conn = self.getconn(cursor, uid, {}) |
|
|
|
keys = vals.keys() |
|
|
|
partner_obj = self.pool.get('res.partner') |
|
|
|
part_name = partner_obj.browse(cursor, uid, vals['partner_id']).name |
|
|
|
vals['partner'] = part_name |
|
|
@ -326,7 +326,7 @@ class LDAPAddress(osv.osv): |
|
|
|
contact_obj['objectclass'] = ['top', 'person', 'organizationalPerson', 'inetOrgPerson', 'user'] |
|
|
|
# we handle the country |
|
|
|
if vals.get('country_id'): |
|
|
|
country = self.browse(cursor, uid, id).country_id |
|
|
|
country = self.browse(cursor, uid, id, context=context).country_id |
|
|
|
if country: |
|
|
|
vals['country_id'] = country.name |
|
|
|
vals['c'] = country.code |
|
|
@ -348,7 +348,7 @@ class LDAPAddress(osv.osv): |
|
|
|
self.getVals('company', 'partner', vals, contact_obj, uid, id, cursor, context) |
|
|
|
self.getVals('info', 'comment', vals, contact_obj, uid, id, cursor, context) |
|
|
|
self.getVals('displayName', 'partner', vals, contact_obj, uid, id, cursor, context) |
|
|
|
## Web site management |
|
|
|
# Web site management |
|
|
|
if self.browse(cursor, uid, id).partner_id.website: |
|
|
|
vals['website'] = self.browse(cursor, uid, id).partner_id.website |
|
|
|
self.getVals('wWWHomePage', 'website', vals, contact_obj, uid, id, cursor, context) |
|
|
@ -381,8 +381,8 @@ class LDAPAddress(osv.osv): |
|
|
|
else: |
|
|
|
conn.connexion.add_s("uid=terp_%s,OU=%s,%s" % (str(id), conn.OU, conn.CONTACT_DN), |
|
|
|
ldap.modlist.addModlist(contact_obj)) |
|
|
|
except Exception, e: |
|
|
|
raise e |
|
|
|
except Exception: |
|
|
|
raise |
|
|
|
conn.connexion.unbind_s() |
|
|
|
|
|
|
|
def updateLdapContact(self, id, vals, cursor, uid, context): |
|
|
@ -407,8 +407,8 @@ class LDAPAddress(osv.osv): |
|
|
|
try: |
|
|
|
conn.connexion.modify_s(old_contatc_obj[0], modlist) |
|
|
|
conn.connexion.unbind_s() |
|
|
|
except Exception, e: |
|
|
|
raise e |
|
|
|
except Exception: |
|
|
|
raise |
|
|
|
|
|
|
|
def removeLdapContact(self, id, cursor, uid): |
|
|
|
"""Remove a contact from ldap""" |
|
|
@ -419,14 +419,14 @@ class LDAPAddress(osv.osv): |
|
|
|
except ldap.NO_SUCH_OBJECT: |
|
|
|
logger.notifyChannel("Warning", netsvc.LOG_INFO, |
|
|
|
_("'no object to delete in ldap' %s") % (id)) |
|
|
|
except Exception, e : |
|
|
|
raise e |
|
|
|
except Exception: |
|
|
|
raise |
|
|
|
try: |
|
|
|
if to_delete: |
|
|
|
conn.connexion.delete_s(to_delete[0]) |
|
|
|
conn.connexion.unbind_s() |
|
|
|
except Exception, e: |
|
|
|
raise e |
|
|
|
except Exception: |
|
|
|
raise |
|
|
|
|
|
|
|
def getLdapContact(self, conn, id): |
|
|
|
result = conn.connexion.search_ext_s("ou=%s, %s" % (conn.OU, conn.CONTACT_DN), |
|
|
@ -455,5 +455,3 @@ class LDAPAddress(osv.osv): |
|
|
|
self.getconn(cursor, uid, context) |
|
|
|
self.ldapMapper.get_connexion() |
|
|
|
return self.ldapMapper |
|
|
|
|
|
|
|
LDAPAddress() |