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.

47 lines
1.8 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 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, fields, models
  6. _logger = getLogger(__name__)
  7. class ResCompanyLdap(models.Model):
  8. _inherit = 'res.company.ldap'
  9. group_mapping_ids = fields.One2many(
  10. 'res.company.ldap.group_mapping',
  11. 'ldap_id', 'Group mappings',
  12. help='Define how Odoo groups are assigned to ldap users',
  13. )
  14. only_ldap_groups = fields.Boolean(
  15. 'Only ldap groups', default=False,
  16. help='If this is checked, manual changes to group membership are '
  17. 'undone on every login (so Odoo groups are always synchronous '
  18. 'with LDAP groups). If not, manually added groups are preserved.',
  19. )
  20. @api.model
  21. def get_or_create_user(self, conf, login, ldap_entry):
  22. op_obj = self.env['res.company.ldap.operator']
  23. user_id = super(ResCompanyLdap, self).get_or_create_user(
  24. conf, login, ldap_entry
  25. )
  26. if not user_id:
  27. return user_id
  28. this = self.browse(conf['id'])
  29. user = self.env['res.users'].browse(user_id)
  30. if this.only_ldap_groups:
  31. _logger.debug('deleting all groups from user %d', user_id)
  32. user.write({'groups_id': [(5, False, False)]})
  33. for mapping in this.group_mapping_ids:
  34. operator = getattr(op_obj, mapping.operator)
  35. _logger.debug('checking mapping %s', mapping)
  36. if operator(ldap_entry, mapping):
  37. _logger.debug(
  38. 'adding user %d to group %s', user, mapping.group_id.name,
  39. )
  40. user.write({'groups_id': [(4, mapping.group_id.id)]})
  41. return user_id