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.

48 lines
1.6 KiB

  1. # -*- coding: utf-8 -*-
  2. # © Daniel Reis (https://launchpad.com/~dreis-pt)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/gpl.html).
  4. from odoo import models, fields
  5. import logging
  6. _log = logging.getLogger(__name__)
  7. class CompanyLDAP(models.Model):
  8. _inherit = 'res.company.ldap'
  9. name_attribute = fields.Char(
  10. 'Name Attribute',
  11. default='cn',
  12. help="By default 'cn' is used. "
  13. "For ActiveDirectory you might use 'displayName' instead.",
  14. )
  15. mail_attribute = fields.Char(
  16. 'E-mail attribute',
  17. default='mail',
  18. help="LDAP attribute to use to retrieve em-mail address.",
  19. )
  20. def get_ldap_dicts(self):
  21. """
  22. Copy of auth_ldap's funtion, changing only the SQL, so that it returns
  23. all fields in the table.
  24. """
  25. return self.sudo().search([('ldap_server', '!=', False)],
  26. order='sequence').read([])
  27. def map_ldap_attributes(self, conf, login, ldap_entry):
  28. values = super(CompanyLDAP, self).map_ldap_attributes(conf, login,
  29. ldap_entry)
  30. mapping = [
  31. ('name', 'name_attribute'),
  32. ('email', 'mail_attribute'),
  33. ]
  34. for value_key, conf_name in mapping:
  35. try:
  36. if conf[conf_name]:
  37. values[value_key] = ldap_entry[1][conf[conf_name]][0]
  38. except KeyError:
  39. _log.warning('No LDAP attribute "%s" found for login "%s"' % (
  40. conf.get(conf_name), values.get('login')))
  41. return values