|
|
@ -30,27 +30,30 @@ |
|
|
|
# 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 |
|
|
|
try: |
|
|
|
import ldap |
|
|
|
import ldap.modlist |
|
|
|
except : |
|
|
|
print 'python ldap not installed please install it in order to use this module' |
|
|
|
|
|
|
|
except ImportError: |
|
|
|
print('python ldap not installed please install it in order to use this module') |
|
|
|
|
|
|
|
from osv import osv, fields |
|
|
|
from openerp.osv import orm, fields |
|
|
|
from 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""" |
|
|
|
LDAP schema inetOrgPerson. Ldap bind options are stored in company. |
|
|
|
""" |
|
|
|
|
|
|
|
def __init__(self, cursor, uid, osv_obj, context=None): |
|
|
|
"""Initialize connexion to ldap by using parameter set in the current user compagny""" |
|
|
|
"""Initialize connexion to ldap by using parameter set in the current user company""" |
|
|
|
logger.notifyChannel("MY TOPIC", netsvc.LOG_DEBUG, |
|
|
|
_('Initalize LDAP CONN')) |
|
|
|
self.USER_DN = '' |
|
|
@ -79,7 +82,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 +97,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 +111,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 +132,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 +163,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), |
|
|
@ -172,7 +174,7 @@ class LDAPAddress(osv.osv): |
|
|
|
'private_phone': fields.char('Private phone', size=128), |
|
|
|
} |
|
|
|
|
|
|
|
def create(self, cursor, uid, vals, context={}): |
|
|
|
def create(self, cursor, uid, vals, context=None): |
|
|
|
self.getconn(cursor, uid, {}) |
|
|
|
ids = None |
|
|
|
self.validate_entries(vals, cursor, uid, ids) |
|
|
@ -189,7 +191,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 +202,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 +233,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 +245,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 context is None: |
|
|
|
context = {} |
|
|
|
# We explicitely test False value |
|
|
|
if vals.get(key, False) is not False: |
|
|
|
dico[att_name] = vals[key] |
|
|
|
else: |
|
|
|
if context.get('init_mode'): |
|
|
@ -260,12 +263,11 @@ 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 |
|
|
|
|
|
|
@ -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 |
|
|
@ -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), |
|
|
|