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.

44 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', _defaults='cn',
  11. help="By default 'cn' is used. "
  12. "For ActiveDirectory you might use 'displayName' instead.")
  13. mail_attribute = fields.Char(
  14. 'E-mail attribute', _defaults='mail',
  15. help="LDAP attribute to use to retrieve em-mail address.")
  16. def get_ldap_dicts(self):
  17. """
  18. Copy of auth_ldap's funtion, changing only the SQL, so that it returns
  19. all fields in the table.
  20. """
  21. return self.sudo().search([('ldap_server', '!=', False)],
  22. order='sequence').read([])
  23. def map_ldap_attributes(self, conf, login, ldap_entry):
  24. values = super(CompanyLDAP, self).map_ldap_attributes(conf, login,
  25. ldap_entry)
  26. mapping = [
  27. ('name', 'name_attribute'),
  28. ('email', 'mail_attribute'),
  29. ]
  30. for value_key, conf_name in mapping:
  31. try:
  32. if conf[conf_name]:
  33. values[value_key] = ldap_entry[1][conf[conf_name]][0]
  34. except KeyError:
  35. _log.warning('No LDAP attribute "%s" found for login "%s"' % (
  36. conf.get(conf_name), values.get('login')))
  37. return values