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.

74 lines
2.7 KiB

11 years ago
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # This module copyright (C) 2013 Daniel Reis
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. from openerp.osv import fields, orm
  22. import logging
  23. _log = logging.getLogger(__name__)
  24. class CompanyLDAP(orm.Model):
  25. _inherit = 'res.company.ldap'
  26. _columns = {
  27. 'name_attribute': fields.char('Name Attribute', size=64,
  28. help="By default 'cn' is used. "
  29. "For ActiveDirectory you might use 'displayName' instead."),
  30. 'mail_attribute': fields.char('E-mail attribute', size=64,
  31. help="LDAP attribute to use to retrieve em-mail address."),
  32. }
  33. _defaults = {
  34. 'name_attribute': 'cn',
  35. 'mail_attribute': 'mail',
  36. }
  37. def get_ldap_dicts(self, cr, ids=None):
  38. """
  39. Copy of auth_ldap's funtion, changing only the SQL, so that it returns
  40. all fields in the table.
  41. """
  42. if ids:
  43. id_clause = 'AND id IN (%s)'
  44. args = [tuple(ids)]
  45. else:
  46. id_clause = ''
  47. args = []
  48. cr.execute("""
  49. SELECT *
  50. FROM res_company_ldap
  51. WHERE ldap_server != '' """ + id_clause + """ ORDER BY sequence
  52. """, args)
  53. return cr.dictfetchall()
  54. def map_ldap_attributes(self, cr, uid, conf, login, ldap_entry):
  55. values = super(CompanyLDAP, self).map_ldap_attributes(cr, uid, conf,
  56. login, ldap_entry)
  57. mapping = [
  58. ('name', 'name_attribute'),
  59. ('email', 'mail_attribute'),
  60. ]
  61. for value_key, conf_name in mapping:
  62. try:
  63. if conf[conf_name]:
  64. values[value_key] = ldap_entry[1][conf[conf_name]][0]
  65. except KeyError:
  66. _log.warning('No LDAP attribute "%s" found for login "%s"' % (
  67. conf.get(conf_name), values.get('login')))
  68. return values