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.

108 lines
4.6 KiB

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.osv import fields, orm
  22. import logging
  23. import users_ldap_groups_operators
  24. import inspect
  25. class CompanyLDAPGroupMapping(orm.Model):
  26. _name = 'res.company.ldap.group_mapping'
  27. _rec_name = 'ldap_attribute'
  28. _order = 'ldap_attribute'
  29. def _get_operators(self, cr, uid, context=None):
  30. operators = []
  31. members = inspect.getmembers(
  32. users_ldap_groups_operators,
  33. lambda cls:
  34. inspect.isclass(cls) and
  35. cls != users_ldap_groups_operators.LDAPOperator)
  36. for name, operator in members:
  37. operators.append((name, name))
  38. return tuple(operators)
  39. _columns = {
  40. 'ldap_id': fields.many2one('res.company.ldap', 'LDAP server', required=True),
  41. 'ldap_attribute': fields.char(
  42. 'LDAP attribute', size=64,
  43. help='The LDAP attribute to check.\n'
  44. 'For active directory, use memberOf.'),
  45. 'operator': fields.selection(
  46. _get_operators, 'Operator',
  47. help='The operator to check the attribute against the value\n'
  48. 'For active directory, use \'contains\'', required=True),
  49. 'value': fields.char(
  50. 'Value', size=1024,
  51. help='The value to check the attribute against.\n'
  52. 'For active directory, use the dn of the desired group',
  53. required=True),
  54. 'group': fields.many2one(
  55. 'res.groups', 'OpenERP group',
  56. help='The OpenERP group to assign', required=True),
  57. }
  58. class CompanyLDAP(orm.Model):
  59. _inherit = 'res.company.ldap'
  60. _columns = {
  61. 'group_mappings': fields.one2many(
  62. 'res.company.ldap.group_mapping',
  63. 'ldap_id', 'Group mappings',
  64. help='Define how OpenERP groups are assigned to ldap users'),
  65. 'only_ldap_groups': fields.boolean(
  66. 'Only ldap groups',
  67. help='If this is checked, manual changes to group membership are '
  68. 'undone on every login (so OpenERP groups are always synchronous '
  69. 'with LDAP groups). If not, manually added groups are preserved.')
  70. }
  71. _default = {
  72. 'only_ldap_groups': False,
  73. }
  74. def get_or_create_user(self, cr, uid, conf, login, ldap_entry, context=None):
  75. user_id = super(CompanyLDAP, self).get_or_create_user(cr, uid, conf, login,
  76. 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(cr, uid, [user_id], {'groups_id': [(5, )]}, context=context)
  86. for mapping in mappingobj.read(cr, uid, mappingobj.search(
  87. cr, uid, [('ldap_id', '=', conf['id'])]), []):
  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'][1]))
  94. userobj.write(cr, uid, [user_id],
  95. {'groups_id': [(4, mapping['group'][0])]},
  96. context=context)
  97. return user_id