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.

64 lines
2.6 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # This module copyright (C) 2015 Therp BV (<http://therp.nl>).
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as
  8. # published by the Free Software Foundation, either version 3 of the
  9. # License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. ##############################################################################
  20. from openerp import _, models, fields, api, exceptions
  21. class ResCompanyLdap(models.Model):
  22. _inherit = 'res.company.ldap'
  23. @api.model
  24. def _create_ldap_entry_field_mappings_default(self):
  25. return [
  26. (0, 0, {
  27. 'field_id':
  28. self.env.ref('base.field_res_users_login').id,
  29. 'attribute': 'userid',
  30. 'use_for_dn': True,
  31. }),
  32. ]
  33. create_ldap_entry = fields.Boolean('Create ldap entry', default=True)
  34. create_ldap_entry_base = fields.Char(
  35. 'Create ldap entry in subtree',
  36. help='Leave empty to use your LDAP base')
  37. create_ldap_entry_objectclass = fields.Char(
  38. 'Object class', default='account',
  39. help='Separate object classes by comma if you need more than one')
  40. create_ldap_entry_field_mappings = fields.One2many(
  41. 'res.company.ldap.field_mapping', 'ldap_id', string='Field mappings',
  42. default=_create_ldap_entry_field_mappings_default)
  43. @api.model
  44. def get_or_create_user(self, conf, login, ldap_entry):
  45. user_id = super(ResCompanyLdap, self).get_or_create_user(
  46. conf, login, ldap_entry)
  47. if user_id:
  48. self.env['res.users'].browse(user_id).write({
  49. 'ldap_entry_dn': ldap_entry[0],
  50. })
  51. return user_id
  52. @api.constrains('create_ldap_entry_field_mappings')
  53. def _constrain_create_ldap_entry_field_mappings(self):
  54. for this in self:
  55. if len(this.create_ldap_entry_field_mappings
  56. .filtered('use_for_dn')) != 1:
  57. raise exceptions.ValidationError(
  58. _('You need to set exactly one mapping as DN'))