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.

96 lines
3.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. import re
  22. from ldap.filter import filter_format
  23. from openerp.osv import orm
  24. import logging
  25. class CompanyLDAP(orm.Model):
  26. _inherit = 'res.company.ldap'
  27. def action_populate(self, cr, uid, ids, context=None):
  28. """
  29. Prepopulate the user table from one or more LDAP resources.
  30. Obviously, the option to create users must be toggled in
  31. the LDAP configuration.
  32. Return the number of users created (as far as we can tell).
  33. """
  34. if isinstance(ids, (int, float)):
  35. ids = [ids]
  36. users_pool = self.pool.get('res.users')
  37. users_no_before = users_pool.search(
  38. cr, uid, [], context=context, count=True)
  39. logger = logging.getLogger('orm.ldap')
  40. logger.debug("action_populate called on res.company.ldap ids %s", ids)
  41. for conf in self.get_ldap_dicts(cr, ids):
  42. if not conf['create_user']:
  43. continue
  44. attribute_match = re.search(
  45. '([a-zA-Z_]+)=\%s', conf['ldap_filter'])
  46. if attribute_match:
  47. login_attr = attribute_match.group(1)
  48. else:
  49. raise orm.except_orm(
  50. "No login attribute found",
  51. "Could not extract login attribute from filter %s" %
  52. conf['ldap_filter'])
  53. ldap_filter = filter_format(conf['ldap_filter'] % '*', ())
  54. for result in self.query(conf, ldap_filter):
  55. self.get_or_create_user(
  56. cr, uid, conf, result[1][login_attr][0], result)
  57. users_no_after = users_pool.search(
  58. cr, uid, [], context=context, count=True)
  59. users_created = users_no_after - users_no_before
  60. logger.debug("%d users created", users_created)
  61. return users_created
  62. def populate_wizard(self, cr, uid, ids, context=None):
  63. """
  64. GUI wrapper for the populate method that reports back
  65. the number of users created.
  66. """
  67. if not ids:
  68. return
  69. if isinstance(ids, (int, float)):
  70. ids = [ids]
  71. wizard_obj = self.pool.get('res.company.ldap.populate_wizard')
  72. res_id = wizard_obj.create(
  73. cr, uid, {'ldap_id': ids[0]}, context=context)
  74. return {
  75. 'name': wizard_obj._description,
  76. 'view_type': 'form',
  77. 'view_mode': 'form',
  78. 'res_model': wizard_obj._name,
  79. 'domain': [],
  80. 'context': context,
  81. 'type': 'ir.actions.act_window',
  82. 'target': 'new',
  83. 'res_id': res_id,
  84. 'nodestroy': True,
  85. }