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.

106 lines
4.1 KiB

9 years ago
9 years ago
9 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # This module copyright (C) 2012 Therp BV (<http://therp.nl>).
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. from openerp import models
  22. from openerp import fields
  23. from openerp import api
  24. import logging
  25. import users_ldap_groups_operators
  26. import inspect
  27. class CompanyLDAPGroupMapping(models.Model):
  28. _name = 'res.company.ldap.group_mapping'
  29. _rec_name = 'ldap_attribute'
  30. _order = 'ldap_attribute'
  31. def _get_operators(self):
  32. operators = []
  33. members = inspect.getmembers(
  34. users_ldap_groups_operators,
  35. lambda cls:
  36. inspect.isclass(cls) and
  37. cls != users_ldap_groups_operators.LDAPOperator)
  38. for name, operator in members:
  39. operators.append((name, name))
  40. return tuple(operators)
  41. ldap_id = fields.Many2one('res.company.ldap', 'LDAP server', required=True)
  42. ldap_attribute = fields.Char(
  43. 'LDAP attribute', size=64,
  44. help='The LDAP attribute to check.\n'
  45. 'For active directory, use memberOf.')
  46. operator = fields.Selection(
  47. _get_operators, 'Operator',
  48. help='The operator to check the attribute against the value\n'
  49. 'For active directory, use \'contains\'', required=True)
  50. value = fields.Char(
  51. 'Value', size=1024,
  52. help='The value to check the attribute against.\n'
  53. 'For active directory, use the dn of the desired group',
  54. required=True)
  55. group = fields.Many2one(
  56. 'res.groups', 'OpenERP group',
  57. help='The OpenERP group to assign', required=True)
  58. class CompanyLDAP(models.Model):
  59. _inherit = 'res.company.ldap'
  60. group_mappings = fields.One2many(
  61. 'res.company.ldap.group_mapping',
  62. 'ldap_id', 'Group mappings',
  63. help='Define how OpenERP groups are assigned to ldap users')
  64. only_ldap_groups = fields.Boolean(
  65. 'Only ldap groups',
  66. help='If this is checked, manual changes to group membership are '
  67. 'undone on every login (so OpenERP groups are always synchronous '
  68. 'with LDAP groups). If not, manually added groups are preserved.')
  69. _default = {
  70. 'only_ldap_groups': False,
  71. }
  72. @api.model
  73. def get_or_create_user(self, conf, login, ldap_entry):
  74. id_ = conf['id']
  75. this = self.browse(id_)
  76. user_id = super(CompanyLDAP, self).get_or_create_user(
  77. conf, login, ldap_entry)
  78. if not user_id:
  79. return user_id
  80. userobj = self.env['res.users']
  81. user = userobj.browse(user_id)
  82. logger = logging.getLogger('users_ldap_groups')
  83. if self.only_ldap_groups:
  84. logger.debug('deleting all groups from user %d' % user_id)
  85. user.write({'groups_id': [(5, )]})
  86. for mapping in this.group_mappings:
  87. operator = mapping.operator
  88. operator = getattr(users_ldap_groups_operators, mapping.operator)()
  89. logger.debug('checking mapping %s' % mapping)
  90. if operator.check_value(ldap_entry, mapping['ldap_attribute'],
  91. mapping['value'], conf, self, logger):
  92. logger.debug('adding user %d to group %s' %
  93. (user_id, mapping.group.name))
  94. user.write({'groups_id': [(4, mapping.group.id)]})
  95. return user_id