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.

128 lines
4.9 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 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. from string import Template
  26. class LDAPOperator(models.AbstractModel):
  27. _name = "res.company.ldap.operator"
  28. def operators(self):
  29. return ('contains', 'equals', 'query')
  30. def contains(self, ldap_entry, attribute, value, ldap_config, company,
  31. logger):
  32. return (attribute in ldap_entry[1]) and \
  33. (value in ldap_entry[1][attribute])
  34. def equals(self, ldap_entry, attribute, value, ldap_config, company,
  35. logger):
  36. return attribute in ldap_entry[1] and \
  37. unicode(value) == unicode(ldap_entry[1][attribute])
  38. def query(self, ldap_entry, attribute, value, ldap_config, company,
  39. logger):
  40. query_string = Template(value).safe_substitute(dict(
  41. [(attr, ldap_entry[1][attribute][0]) for attr in ldap_entry[1]]
  42. )
  43. )
  44. logger.debug('evaluating query group mapping, filter: %s' %
  45. query_string)
  46. results = company.query(ldap_config, query_string)
  47. logger.debug(results)
  48. return bool(results)
  49. class CompanyLDAPGroupMapping(models.Model):
  50. _name = 'res.company.ldap.group_mapping'
  51. _rec_name = 'ldap_attribute'
  52. _order = 'ldap_attribute'
  53. def _get_operators(self):
  54. op_obj = self.env['res.company.ldap.operator']
  55. operators = [(op, op) for op in op_obj.operators()]
  56. return tuple(operators)
  57. ldap_id = fields.Many2one('res.company.ldap', 'LDAP server', required=True)
  58. ldap_attribute = fields.Char(
  59. 'LDAP attribute',
  60. help='The LDAP attribute to check.\n'
  61. 'For active directory, use memberOf.')
  62. operator = fields.Selection(
  63. _get_operators, 'Operator',
  64. help='The operator to check the attribute against the value\n'
  65. 'For active directory, use \'contains\'', required=True)
  66. value = fields.Char(
  67. 'Value',
  68. help='The value to check the attribute against.\n'
  69. 'For active directory, use the dn of the desired group',
  70. required=True)
  71. group = fields.Many2one(
  72. 'res.groups', 'OpenERP group',
  73. help='The OpenERP group to assign', required=True)
  74. class CompanyLDAP(models.Model):
  75. _inherit = 'res.company.ldap'
  76. group_mappings = fields.One2many(
  77. 'res.company.ldap.group_mapping',
  78. 'ldap_id', 'Group mappings',
  79. help='Define how OpenERP groups are assigned to ldap users')
  80. only_ldap_groups = fields.Boolean(
  81. 'Only ldap groups',
  82. help='If this is checked, manual changes to group membership are '
  83. 'undone on every login (so OpenERP groups are always synchronous '
  84. 'with LDAP groups). If not, manually added groups are preserved.')
  85. _default = {
  86. 'only_ldap_groups': False,
  87. }
  88. @api.model
  89. def get_or_create_user(self, conf, login, ldap_entry):
  90. op_obj = self.env['res.company.ldap.operator']
  91. id_ = conf['id']
  92. this = self.browse(id_)
  93. user_id = super(CompanyLDAP, self).get_or_create_user(
  94. conf, login, ldap_entry)
  95. if not user_id:
  96. return user_id
  97. userobj = self.env['res.users']
  98. user = userobj.browse(user_id)
  99. logger = logging.getLogger('users_ldap_groups')
  100. if self.only_ldap_groups:
  101. logger.debug('deleting all groups from user %d' % user_id)
  102. user.write({'groups_id': [(5, )]})
  103. for mapping in this.group_mappings:
  104. operator = getattr(op_obj, mapping.operator)
  105. logger.debug('checking mapping %s' % mapping)
  106. if operator(ldap_entry, mapping['ldap_attribute'],
  107. mapping['value'], conf, self, logger):
  108. logger.debug('adding user %d to group %s' %
  109. (user_id, mapping.group.name))
  110. user.write({'groups_id': [(4, mapping.group.id)]})
  111. return user_id