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.

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