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.

43 lines
1.4 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2012-2018 Therp BV <https://therp.nl>
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  4. from logging import getLogger
  5. from odoo import api, models
  6. from string import Template
  7. _logger = getLogger(__name__)
  8. class ResCompanyLdapOperator(models.AbstractModel):
  9. """Define operators for group mappings"""
  10. _name = "res.company.ldap.operator"
  11. _description = "Definition op LDAP operations"
  12. @api.model
  13. def operators(self):
  14. """Return names of function to call on this model as operator"""
  15. return ('contains', 'equals', 'query')
  16. @api.model
  17. def contains(self, ldap_entry, mapping):
  18. return mapping.ldap_attribute in ldap_entry[1] and \
  19. mapping.value in ldap_entry[1][mapping.ldap_attribute]
  20. def equals(self, ldap_entry, mapping):
  21. return mapping.ldap_attribute in ldap_entry[1] and \
  22. unicode(mapping.value) == unicode(
  23. ldap_entry[1][mapping.ldap_attribute]
  24. )
  25. def query(self, ldap_entry, mapping):
  26. query_string = Template(mapping.value).safe_substitute({
  27. attr: ldap_entry[1][attr][0] for attr in ldap_entry[1]
  28. })
  29. _logger.debug(
  30. 'evaluating query group mapping, filter: %s' % query_string
  31. )
  32. results = mapping.ldap_id.query(
  33. mapping.ldap_id.read()[0], query_string
  34. )
  35. _logger.debug(results)
  36. return bool(results)