You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

459 lines
20 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Copyright (c) 2010-2011 Camptocamp SA (http://www.camptocamp.com)
  5. # All Right Reserved
  6. #
  7. # Author : Nicolas Bessi (Camptocamp), Thanks to Laurent Lauden for his code adaptation
  8. # Active directory Donor: M. Benadiba (Informatique Assistances.fr)
  9. # Contribution : Joel Grand-Guillaume
  10. #
  11. # WARNING: This program as such is intended to be used by professional
  12. # programmers who take the whole responsability of assessing all potential
  13. # consequences resulting from its eventual inadequacies and bugs
  14. # End users who are looking for a ready-to-use solution with commercial
  15. # garantees and support are strongly adviced to contract a Free Software
  16. # Service Company
  17. #
  18. # This program is Free Software; you can redistribute it and/or
  19. # modify it under the terms of the GNU General Public License
  20. # as published by the Free Software Foundation; either version 2
  21. # of the License, or (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU General Public License
  29. # along with this program; if not, write to the Free Software
  30. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  31. #
  32. ##############################################################################
  33. # TODO Find why company parameter are cached
  34. import re
  35. import unicodedata
  36. import netsvc
  37. try:
  38. import ldap
  39. import ldap.modlist
  40. except ImportError:
  41. print('python ldap not installed please install it in order to use this module')
  42. from openerp.osv import orm, fields
  43. from tools.translate import _
  44. logger = netsvc.Logger()
  45. class LdapConnMApper(object):
  46. """LdapConnMApper: push specific fields from the Terp Partner_contacts to the
  47. LDAP schema inetOrgPerson. Ldap bind options are stored in company.
  48. """
  49. def __init__(self, cursor, uid, osv_obj, context=None):
  50. """Initialize connexion to ldap by using parameter set in the current user company"""
  51. logger.notifyChannel("MY TOPIC", netsvc.LOG_DEBUG,
  52. _('Initalize LDAP CONN'))
  53. self.USER_DN = ''
  54. self.CONTACT_DN = ''
  55. self.LDAP_SERVER = ''
  56. self.PASS = ''
  57. self.OU = ''
  58. self.connexion = ''
  59. self.ACTIVDIR = False
  60. # Reading ldap pref
  61. user = osv_obj.pool.get('res.users').browse(cursor, uid, uid, context=context)
  62. company = osv_obj.pool.get('res.company').browse(cursor,
  63. uid,
  64. user.company_id.id,
  65. context=context)
  66. self.USER_DN = company.base_dn
  67. self.CONTACT_DN = company.contact_dn
  68. self.LDAP_SERVER = company.ldap_server
  69. self.PASS = company.passwd
  70. self.PORT = company.ldap_port
  71. self.OU = company.ounit
  72. self.ACTIVDIR = company.is_activedir
  73. mand = (self.USER_DN, self.CONTACT_DN, self.LDAP_SERVER, self.PASS, self.OU)
  74. if company.ldap_active:
  75. for param in mand:
  76. if not param:
  77. raise orm.except_orm(_('Warning !'),
  78. _('An LDAP parameter is missing for company %s') % (company.name,))
  79. def get_connexion(self):
  80. """create a new ldap connexion"""
  81. logger.notifyChannel("LDAP Address", netsvc.LOG_DEBUG,
  82. _('connecting to server ldap %s') % (self.LDAP_SERVER,))
  83. if self.PORT:
  84. self.connexion = ldap.open(self.LDAP_SERVER, self.PORT)
  85. else:
  86. self.connexion = ldap.open(self.LDAP_SERVER)
  87. self.connexion.simple_bind_s(self.USER_DN, self.PASS)
  88. return self.connexion
  89. class LDAPAddress(orm.Model):
  90. """Override the CRUD of the objet in order to dynamically bind to ldap"""
  91. _inherit = 'res.partner.address'
  92. ldapMapper = None
  93. def init(self, cr):
  94. logger = netsvc.Logger()
  95. try:
  96. logger.notifyChannel(_('LDAP address init'),
  97. netsvc.LOG_INFO,
  98. _('try to ALTER TABLE res_partner_address RENAME '
  99. 'column name to lastname ;'))
  100. cr.execute('ALTER TABLE res_partner_address RENAME column name to lastname ;')
  101. except Exception:
  102. cr.rollback()
  103. logger.notifyChannel(_('LDAP address init'),
  104. netsvc.LOG_INFO,
  105. _('Warning ! impossible to rename column name'
  106. ' into lastname, this is probabely aleready'
  107. ' done or does not exist'))
  108. def _compute_name(self, firstname, lastname):
  109. firstname = firstname or u''
  110. lastname = lastname or u''
  111. firstname = (u' ' + firstname).rstrip()
  112. return u"%s%s" % (lastname, firstname)
  113. def _name_get_fnc(self, cursor, uid, ids, name, arg, context=None):
  114. """Get the name (lastname + firstname), otherwise ''"""
  115. if not ids:
  116. return []
  117. reads = self.read(cursor, uid, ids, ['lastname', 'firstname'])
  118. res = []
  119. for record in reads:
  120. # We want to have " firstname" or ""
  121. name = self._compute_name(record['firstname'], record['lastname'])
  122. res.append((record['id'], name))
  123. return dict(res)
  124. # TODO get the new version of name search not vulnerable to sql injections
  125. # def name_search(self, cursor, user, name, args=None, operator='ilike', context=None, limit=100):
  126. # if not context: context = {}
  127. # prep_name = '.*%s.*' %(name)
  128. # cursor.execute(("select id from res_partner_address where"
  129. # " (to_ascii(convert( lastname, 'UTF8', 'LATIN1'),'LATIN-1') ~* '%s'"
  130. # " or to_ascii(convert( firstname, 'UTF8', 'LATIN1'),'LATIN-1') ~* '%s')"
  131. # " limit %s") % (prep_name, prep_name, limit))
  132. # res = cursor.fetchall()
  133. # if res:
  134. # res = [x[0] for x in res]
  135. # else:
  136. # res = []
  137. # # search in partner name to know if we are searching partner...
  138. # partner_obj=self.pool.get('res.partner')
  139. # part_len = len(res)-limit
  140. # if part_len > 0:
  141. # partner_res = partner_obj.search(cursor, user, [('name', 'ilike', name)],
  142. # limit=part_len, context=context)
  143. # for p in partner_res:
  144. # addresses = partner_obj.browse(cursor, user, p).address
  145. # # Take each contact and add it to
  146. # for add in addresses:
  147. # res.append(add.id)
  148. # return self.name_get(cursor, user, res, context)
  149. _columns = {
  150. 'firstname': fields.char('First name', size=256),
  151. 'lastname': fields.char('Last name', size=256),
  152. 'name': fields.function(_name_get_fnc, method=True,
  153. type="char", size=512,
  154. store=True, string='Contact Name',
  155. help='Name generated from the first name and last name',
  156. nodrop=True),
  157. 'private_phone': fields.char('Private phone', size=128),
  158. }
  159. def create(self, cursor, uid, vals, context=None):
  160. self.getconn(cursor, uid, {})
  161. ids = None
  162. self.validate_entries(vals, cursor, uid, ids)
  163. tmp_id = super(LDAPAddress, self).create(cursor, uid,
  164. vals, context)
  165. if self.ldaplinkactive(cursor, uid, context):
  166. self.saveLdapContact(tmp_id, vals, cursor, uid, context)
  167. return tmp_id
  168. def write(self, cursor, uid, ids, vals, context=None):
  169. context = context or {}
  170. self.getconn(cursor, uid, {})
  171. if not isinstance(ids, list):
  172. ids = [ids]
  173. if ids:
  174. self.validate_entries(vals, cursor, uid, ids)
  175. if context.get('init_mode'):
  176. success = True
  177. else:
  178. success = super(LDAPAddress, self).write(cursor, uid, ids,
  179. vals, context)
  180. if self.ldaplinkactive(cursor, uid, context):
  181. for address_id in ids:
  182. self.updateLdapContact(address_id, vals, cursor, uid, context)
  183. return success
  184. def unlink(self, cursor, uid, ids, context=None):
  185. if context is None:
  186. context = {}
  187. if ids:
  188. self.getconn(cursor, uid, {})
  189. if not isinstance(ids, list):
  190. ids = [ids]
  191. if self.ldaplinkactive(cursor, uid, context):
  192. for id in ids:
  193. self.removeLdapContact(id, cursor, uid)
  194. return super(LDAPAddress, self).unlink(cursor, uid, ids)
  195. def validate_entries(self, vals, cursor, uid, ids):
  196. """Validate data of an address based on the inetOrgPerson schema"""
  197. for val in vals:
  198. try:
  199. if isinstance(vals[val], basestring):
  200. vals[val] = unicode(vals[val].decode('utf8'))
  201. except UnicodeError:
  202. logger.notifyChannel('LDAP encode', netsvc.LOG_DEBUG,
  203. 'cannot unicode ' + vals[val])
  204. pass
  205. if ids is not None:
  206. if isinstance(ids, (int, long)):
  207. ids = [ids]
  208. if len(ids) == 1:
  209. self.addNeededFields(ids[0], vals, cursor, uid)
  210. email = vals.get('email', False)
  211. phone = vals.get('phone', False)
  212. fax = vals.get('fax', False)
  213. mobile = vals.get('mobile', False)
  214. private_phone = vals.get('private_phone', False)
  215. if email:
  216. if re.match("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$",
  217. email) is None:
  218. raise orm.except_orm(_('Warning !'),
  219. _('Please enter a valid e-mail'))
  220. phones = (('phone', phone), ('fax', fax), ('mobile', mobile),
  221. ('private_phone', private_phone))
  222. for phone_tuple in phones:
  223. phone_number = phone_tuple[1]
  224. if phone_number:
  225. if not phone_number.startswith('+'):
  226. raise orm.except_orm(_('Warning !'),
  227. _('Please enter a valid phone number in %s'
  228. ' international format (i.e. leading +)') % phone_tuple[0])
  229. def getVals(self, att_name, key, vals, dico, uid, ids, cursor, context=None):
  230. """map to values to dict"""
  231. if context is None:
  232. context = {}
  233. # We explicitely test False value
  234. if vals.get(key, False) is not False:
  235. dico[att_name] = vals[key]
  236. else:
  237. if context.get('init_mode'):
  238. return False
  239. tmp = self.read(cursor, uid, ids, [key], context={})
  240. if tmp.get(key, False):
  241. dico[att_name] = tmp[key]
  242. def _un_unicodize_buf(self, in_buf):
  243. if isinstance(in_buf, unicode):
  244. try:
  245. return in_buf.encode()
  246. except Exception:
  247. return unicodedata.normalize("NFKD", in_buf).encode('ascii', 'ignore')
  248. return in_buf
  249. def unUnicodize(self, indict):
  250. """remove unicode data of modlist as unicode is not supported
  251. by python-ldap librairy till version 2.7"""
  252. for key in indict:
  253. if not isinstance(indict[key], list):
  254. indict[key] = self._un_unicodize_buf(indict[key])
  255. else:
  256. nonutfArray = []
  257. for val in indict[key]:
  258. nonutfArray.append(self._un_unicodize_buf(val))
  259. indict[key] = nonutfArray
  260. def addNeededFields(self, id, vals, cursor, uid):
  261. previousvalue = self.browse(cursor, uid, [id])[0]
  262. if not vals.get('partner_id'):
  263. vals['partner_id'] = previousvalue.partner_id.id
  264. values_to_check = ('email', 'phone', 'fax', 'mobile', 'firstname',
  265. 'lastname', 'private_phone', 'street', 'street2')
  266. for val in values_to_check:
  267. if not vals.get(val):
  268. vals[val] = previousvalue[val]
  269. def mappLdapObject(self, id, vals, cursor, uid, context):
  270. """Mapp ResPArtner adress to moddlist"""
  271. self.addNeededFields(id, vals, cursor, uid)
  272. conn = self.getconn(cursor, uid, {})
  273. partner_obj = self.pool.get('res.partner')
  274. part_name = partner_obj.browse(cursor, uid, vals['partner_id']).name
  275. vals['partner'] = part_name
  276. name = self._compute_name(vals.get('firstname'), vals.get('lastname'))
  277. if name:
  278. cn = name
  279. else:
  280. cn = part_name
  281. if not vals.get('lastname'):
  282. vals['lastname'] = part_name
  283. contact_obj = {'objectclass': ['inetOrgPerson'],
  284. 'uid': ['terp_'+str(id)],
  285. 'ou': [conn.OU],
  286. 'cn': [cn],
  287. 'sn': [vals['lastname']]}
  288. if not vals.get('street'):
  289. vals['street'] = u''
  290. if not vals.get('street2'):
  291. vals['street2'] = u''
  292. street_key = 'street'
  293. if self.getconn(cursor, uid, {}).ACTIVDIR:
  294. # ENTERING THE M$ Realm and it is weird
  295. # We manage the address
  296. street_key = 'streetAddress'
  297. contact_obj[street_key] = vals['street'] + "\r\n" + vals['street2']
  298. # we modifiy the class
  299. contact_obj['objectclass'] = ['top', 'person', 'organizationalPerson', 'inetOrgPerson', 'user']
  300. # we handle the country
  301. if vals.get('country_id'):
  302. country = self.browse(cursor, uid, id).country_id
  303. if country:
  304. vals['country_id'] = country.name
  305. vals['c'] = country.code
  306. else:
  307. vals['country_id'] = False
  308. vals['c'] = False
  309. if vals.get('country_id', False):
  310. self.getVals('co', 'country_id', vals, contact_obj, uid, id, cursor, context)
  311. self.getVals('c', 'c', vals, contact_obj, uid, id, cursor, context)
  312. # we compute the display name
  313. vals['display'] = '%s %s' % (vals['partner'], contact_obj['cn'][0])
  314. # we get the title
  315. if self.browse(cursor, uid, id).function:
  316. contact_obj['description'] = self.browse(cursor, uid, id).function.name
  317. # we replace carriage return
  318. if vals.get('comment', False):
  319. vals['comment'] = vals['comment'].replace("\n", "\r\n")
  320. # Active directory specific fields
  321. self.getVals('company', 'partner', vals, contact_obj, uid, id, cursor, context)
  322. self.getVals('info', 'comment', vals, contact_obj, uid, id, cursor, context)
  323. self.getVals('displayName', 'partner', vals, contact_obj, uid, id, cursor, context)
  324. # Web site management
  325. if self.browse(cursor, uid, id).partner_id.website:
  326. vals['website'] = self.browse(cursor, uid, id).partner_id.website
  327. self.getVals('wWWHomePage', 'website', vals, contact_obj, uid, id, cursor, context)
  328. del(vals['website'])
  329. self.getVals('title', 'title', vals, contact_obj, uid, id, cursor, context)
  330. else:
  331. contact_obj[street_key] = vals['street'] + u"\n" + vals['street2']
  332. self.getVals('o', 'partner', vals, contact_obj, uid, id, cursor, context)
  333. # Common attributes
  334. self.getVals('givenName', 'firstname', vals, contact_obj, uid, id, cursor, context)
  335. self.getVals('mail', 'email', vals, contact_obj, uid, id, cursor, context)
  336. self.getVals('telephoneNumber', 'phone', vals, contact_obj, uid, id, cursor, context)
  337. self.getVals('l', 'city', vals, contact_obj, uid, id, cursor, context)
  338. self.getVals('facsimileTelephoneNumber', 'fax', vals, contact_obj, uid, id, cursor, context)
  339. self.getVals('mobile', 'mobile', vals, contact_obj, uid, id, cursor, context)
  340. self.getVals('homePhone', 'private_phone', vals, contact_obj, uid, id, cursor, context)
  341. self.getVals('postalCode', 'zip', vals, contact_obj, uid, id, cursor, context)
  342. self.unUnicodize(contact_obj)
  343. return contact_obj
  344. def saveLdapContact(self, id, vals, cursor, uid, context=None):
  345. """save openerp adress to ldap"""
  346. contact_obj = self.mappLdapObject(id, vals, cursor, uid, context)
  347. conn = self.connectToLdap(cursor, uid, context=context)
  348. try:
  349. if self.getconn(cursor, uid, context).ACTIVDIR:
  350. conn.connexion.add_s("CN=%s,OU=%s,%s" % (contact_obj['cn'][0], conn.OU, conn.CONTACT_DN),
  351. ldap.modlist.addModlist(contact_obj))
  352. else:
  353. conn.connexion.add_s("uid=terp_%s,OU=%s,%s" % (str(id), conn.OU, conn.CONTACT_DN),
  354. ldap.modlist.addModlist(contact_obj))
  355. except Exception:
  356. raise
  357. conn.connexion.unbind_s()
  358. def updateLdapContact(self, id, vals, cursor, uid, context):
  359. """update an existing contact with the data of OpenERP"""
  360. conn = self.connectToLdap(cursor, uid, context={})
  361. try:
  362. old_contatc_obj = self.getLdapContact(conn, id)
  363. except ldap.NO_SUCH_OBJECT:
  364. self.saveLdapContact(id, vals, cursor, uid, context)
  365. return
  366. contact_obj = self.mappLdapObject(id, vals, cursor, uid, context)
  367. if conn.ACTIVDIR:
  368. modlist = []
  369. for key, val in contact_obj.items():
  370. if key in ('cn', 'uid', 'objectclass'):
  371. continue
  372. if isinstance(val, list):
  373. val = val[0]
  374. modlist.append((ldap.MOD_REPLACE, key, val))
  375. else:
  376. modlist = ldap.modlist.modifyModlist(old_contatc_obj[1], contact_obj)
  377. try:
  378. conn.connexion.modify_s(old_contatc_obj[0], modlist)
  379. conn.connexion.unbind_s()
  380. except Exception:
  381. raise
  382. def removeLdapContact(self, id, cursor, uid):
  383. """Remove a contact from ldap"""
  384. conn = self.connectToLdap(cursor, uid, context={})
  385. to_delete = None
  386. try:
  387. to_delete = self.getLdapContact(conn, id)
  388. except ldap.NO_SUCH_OBJECT:
  389. logger.notifyChannel("Warning", netsvc.LOG_INFO,
  390. _("'no object to delete in ldap' %s") % (id))
  391. except Exception:
  392. raise
  393. try:
  394. if to_delete:
  395. conn.connexion.delete_s(to_delete[0])
  396. conn.connexion.unbind_s()
  397. except Exception:
  398. raise
  399. def getLdapContact(self, conn, id):
  400. result = conn.connexion.search_ext_s("ou=%s,%s" % (conn.OU, conn.CONTACT_DN),
  401. ldap.SCOPE_SUBTREE,
  402. "(&(objectclass=*)(uid=terp_"+str(id)+"))")
  403. if not result:
  404. raise ldap.NO_SUCH_OBJECT
  405. return result[0]
  406. def ldaplinkactive(self, cursor, uid, context=None):
  407. """Check if ldap is activated for this company"""
  408. user = self.pool.get('res.users').browse(cursor, uid, uid, context=context)
  409. company = self.pool.get('res.company').browse(cursor, uid, user.company_id.id, context=context)
  410. return company.ldap_active
  411. def getconn(self, cursor, uid, context=None):
  412. """LdapConnMApper"""
  413. if not self.ldapMapper:
  414. self.ldapMapper = LdapConnMApper(cursor, uid, self)
  415. return self.ldapMapper
  416. def connectToLdap(self, cursor, uid, context=None):
  417. """Reinitialize ldap connection"""
  418. # getting ldap pref
  419. if not self.ldapMapper:
  420. self.getconn(cursor, uid, context)
  421. self.ldapMapper.get_connexion()
  422. return self.ldapMapper
  423. LDAPAddress()