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.

110 lines
4.5 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 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.multi
  73. def get_or_create_user(self, cr, uid, conf, login, ldap_entry,
  74. context=None):
  75. user_id = super(CompanyLDAP, self).get_or_create_user(
  76. cr, uid, conf, login, ldap_entry, context)
  77. if not user_id:
  78. return user_id
  79. logger = logging.getLogger('users_ldap_groups')
  80. mappingobj = self.pool.get('res.company.ldap.group_mapping')
  81. userobj = self.pool.get('res.users')
  82. conf_all = self.read(cr, uid, conf['id'], ['only_ldap_groups'])
  83. if(conf_all['only_ldap_groups']):
  84. logger.debug('deleting all groups from user %d' % user_id)
  85. userobj.write(
  86. cr, uid, [user_id], {'groups_id': [(5, )]}, context=context)
  87. for mapping in mappingobj.read(cr, uid, mappingobj.search(
  88. cr, uid, [('ldap_id', '=', conf['id'])]), []):
  89. operator = getattr(users_ldap_groups_operators,
  90. mapping['operator'])()
  91. logger.debug('checking mapping %s' % mapping)
  92. if operator.check_value(ldap_entry, mapping['ldap_attribute'],
  93. mapping['value'], conf, self, logger):
  94. logger.debug('adding user %d to group %s' %
  95. (user_id, mapping['group'][1]))
  96. userobj.write(cr, uid, [user_id],
  97. {'groups_id': [(4, mapping['group'][0])]},
  98. context=context)
  99. return user_id