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.

78 lines
2.8 KiB

  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(
  28. 'Name Attribute', size=64,
  29. help="By default 'cn' is used. "
  30. "For ActiveDirectory you might use 'displayName' instead."),
  31. 'mail_attribute': fields.char(
  32. 'E-mail attribute', size=64,
  33. help="LDAP attribute to use to retrieve em-mail address."),
  34. }
  35. _defaults = {
  36. 'name_attribute': 'cn',
  37. 'mail_attribute': 'mail',
  38. }
  39. def get_ldap_dicts(self, cr, ids=None):
  40. """
  41. Copy of auth_ldap's funtion, changing only the SQL, so that it returns
  42. all fields in the table.
  43. """
  44. if ids:
  45. id_clause = 'AND id IN (%s)'
  46. args = [tuple(ids)]
  47. else:
  48. id_clause = ''
  49. args = []
  50. cr.execute("""
  51. SELECT *
  52. FROM res_company_ldap
  53. WHERE ldap_server != '' """ + id_clause + """ ORDER BY sequence
  54. """, args)
  55. return cr.dictfetchall()
  56. def map_ldap_attributes(self, cr, uid, conf, login, ldap_entry):
  57. _super = super(CompanyLDAP, self)
  58. values = _super.map_ldap_attributes(cr, uid, conf,
  59. login, ldap_entry)
  60. mapping = [
  61. ('name', 'name_attribute'),
  62. ('email', 'mail_attribute'),
  63. ]
  64. for value_key, conf_name in mapping:
  65. try:
  66. if conf[conf_name]:
  67. values[value_key] = ldap_entry[1][conf[conf_name]][0]
  68. except KeyError:
  69. _log.warning('No LDAP attribute "%s" found for login "%s"',
  70. conf.get(conf_name),
  71. values.get('login'))
  72. return values