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.

98 lines
3.5 KiB

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. import re
  22. from openerp import models, api, _
  23. from openerp.exceptions import UserError
  24. import logging
  25. _logger = logging.getLogger(__name__)
  26. try:
  27. from ldap.filter import filter_format
  28. except ImportError:
  29. _logger.debug('Can not `from ldap.filter import filter_format`.')
  30. class CompanyLDAP(models.Model):
  31. _inherit = 'res.company.ldap'
  32. @api.multi
  33. def action_populate(self):
  34. """
  35. Prepopulate the user table from one or more LDAP resources.
  36. Obviously, the option to create users must be toggled in
  37. the LDAP configuration.
  38. Return the number of users created (as far as we can tell).
  39. """
  40. users_pool = self.env['res.users']
  41. users_no_before = users_pool.search_count([])
  42. logger = logging.getLogger('orm.ldap')
  43. logger.debug("action_populate called on res.company.ldap ids %s",
  44. self.ids)
  45. for conf in self.get_ldap_dicts():
  46. if not conf['create_user']:
  47. continue
  48. attribute_match = re.search(
  49. r'([a-zA-Z_]+)=\%s', conf['ldap_filter'])
  50. if attribute_match:
  51. login_attr = attribute_match.group(1)
  52. else:
  53. raise UserError(
  54. _("No login attribute found: "
  55. "Could not extract login attribute from filter %s") %
  56. conf['ldap_filter'])
  57. ldap_filter = filter_format(conf['ldap_filter'] % '*', ())
  58. for result in self.query(conf, ldap_filter.encode('utf-8')):
  59. self.get_or_create_user(conf, result[1][login_attr][0], result)
  60. users_no_after = users_pool.search_count([])
  61. users_created = users_no_after - users_no_before
  62. logger.debug("%d users created", users_created)
  63. return users_created
  64. @api.multi
  65. def populate_wizard(self):
  66. """
  67. GUI wrapper for the populate method that reports back
  68. the number of users created.
  69. """
  70. if not self:
  71. return
  72. wizard_obj = self.env['res.company.ldap.populate_wizard']
  73. res_id = wizard_obj.create({'ldap_id': self.id}).id
  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': self.env.context,
  81. 'type': 'ir.actions.act_window',
  82. 'target': 'new',
  83. 'res_id': res_id,
  84. 'nodestroy': True,
  85. }