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.

107 lines
4.5 KiB

  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: inspect.isclass(cls)
  34. and cls != users_ldap_groups_operators.LDAPOperator)
  35. for name, operator in members:
  36. operators.append((name, name))
  37. return tuple(operators)
  38. _columns = {
  39. 'ldap_id': fields.many2one('res.company.ldap', 'LDAP server', required=True),
  40. 'ldap_attribute': fields.char(
  41. 'LDAP attribute', size=64,
  42. help='The LDAP attribute to check.\n'
  43. 'For active directory, use memberOf.'),
  44. 'operator': fields.selection(
  45. _get_operators, 'Operator',
  46. help='The operator to check the attribute against the value\n'
  47. 'For active directory, use \'contains\'', required=True),
  48. 'value': fields.char(
  49. 'Value', size=1024,
  50. help='The value to check the attribute against.\n'
  51. 'For active directory, use the dn of the desired group',
  52. required=True),
  53. 'group': fields.many2one(
  54. 'res.groups', 'OpenERP group',
  55. help='The OpenERP group to assign', required=True),
  56. }
  57. class CompanyLDAP(orm.Model):
  58. _inherit = 'res.company.ldap'
  59. _columns = {
  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. }
  70. _default = {
  71. 'only_ldap_groups': False,
  72. }
  73. def get_or_create_user(self, cr, uid, conf, login, ldap_entry, context=None):
  74. user_id = super(CompanyLDAP, self).get_or_create_user(cr, uid, conf, login,
  75. ldap_entry, context)
  76. if not user_id:
  77. return user_id
  78. logger = logging.getLogger('users_ldap_groups')
  79. mappingobj = self.pool.get('res.company.ldap.group_mapping')
  80. userobj = self.pool.get('res.users')
  81. conf_all = self.read(cr, uid, conf['id'], ['only_ldap_groups'])
  82. if(conf_all['only_ldap_groups']):
  83. logger.debug('deleting all groups from user %d' % user_id)
  84. userobj.write(cr, uid, [user_id], {'groups_id': [(5, )]}, context=context)
  85. for mapping in mappingobj.read(cr, uid, mappingobj.search(
  86. cr, uid, [('ldap_id', '=', conf['id'])]), []):
  87. operator = getattr(users_ldap_groups_operators, mapping['operator'])()
  88. logger.debug('checking mapping %s' % mapping)
  89. if operator.check_value(ldap_entry, mapping['ldap_attribute'],
  90. mapping['value'], conf, self, logger):
  91. logger.debug('adding user %d to group %s' %
  92. (user_id, mapping['group'][1]))
  93. userobj.write(cr, uid, [user_id],
  94. {'groups_id': [(4, mapping['group'][0])]},
  95. context=context)
  96. return user_id