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.

73 lines
2.7 KiB

12 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. 'mail_attribute': 'mail',
  35. }
  36. def get_ldap_dicts(self, cr, ids=None):
  37. """
  38. Copy of auth_ldap's funtion, changing only the SQL, so that it returns
  39. all fields in the table.
  40. """
  41. if ids:
  42. id_clause = 'AND id IN (%s)'
  43. args = [tuple(ids)]
  44. else:
  45. id_clause = ''
  46. args = []
  47. cr.execute("""
  48. SELECT *
  49. FROM res_company_ldap
  50. WHERE ldap_server != '' """ + id_clause + """ ORDER BY sequence
  51. """, args)
  52. return cr.dictfetchall()
  53. def map_ldap_attributes(self, cr, uid, conf, login, ldap_entry):
  54. values = super(CompanyLDAP, self).map_ldap_attributes(cr, uid, conf,
  55. login, ldap_entry)
  56. mapping = [
  57. ('name', 'name_attribute'),
  58. ('email', 'mail_attribute'),
  59. ]
  60. for value_key, conf_name in mapping:
  61. try:
  62. if conf[conf_name]:
  63. values[value_key] = ldap_entry[1][conf[conf_name]][0]
  64. except KeyError:
  65. _log.warning('No LDAP attribute "%s" found for login "%s"' % (
  66. conf.get(conf_name), values.get('login')))
  67. return values