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.

77 lines
3.0 KiB

9 years ago
  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Authentification - Generate Password module for Odoo
  5. # Copyright (C) 2013-2014 GRAP (http://www.grap.coop)
  6. # @author Sylvain LE GAL (https://twitter.com/legalsylvain)
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Affero General Public License as
  10. # published by the Free Software Foundation, either version 3 of the
  11. # License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Affero General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Affero General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ##############################################################################
  22. # This line is noqa flaged, to allow users to use string functions written
  23. # in ir_config_parameter
  24. import string # flake8: noqa
  25. import random
  26. from openerp.osv.orm import Model, except_orm
  27. from openerp.tools.translate import _
  28. from openerp.tools.safe_eval import safe_eval
  29. class res_users(Model):
  30. _inherit = 'res.users'
  31. def generate_password(self, cr, uid, ids, context=None):
  32. if not context:
  33. context = {}
  34. mm_obj = self.pool['mail.mail']
  35. icp_obj = self.pool['ir.config_parameter']
  36. imd_obj = self.pool['ir.model.data']
  37. et_obj = self.pool['email.template']
  38. globals_dict = {'string': string}
  39. try:
  40. int(icp_obj.get_param(
  41. cr, uid, 'auth_generate_password.password_size'))
  42. except:
  43. raise except_orm(_("error"), _("Only digit chars authorized"))
  44. password_size = safe_eval(
  45. icp_obj.get_param(
  46. cr, uid, 'auth_generate_password.password_size'),
  47. globals_dict=globals_dict
  48. )
  49. password_chars = safe_eval(
  50. icp_obj.get_param(
  51. cr, uid, 'auth_generate_password.password_chars'),
  52. globals_dict=globals_dict
  53. )
  54. et = imd_obj.get_object(
  55. cr, uid, 'auth_generate_password', 'generate_password_template')
  56. for ru in self.browse(cr, uid, ids, context=context):
  57. if not ru.email:
  58. raise except_orm(
  59. _("Cannot send email: user has no email address."),
  60. user.name)
  61. # Generate Password
  62. password = "".join([random.choice(
  63. password_chars) for n in xrange(password_size)])
  64. self._set_new_password(
  65. cr, uid, ru.id, None, password, None, context=None)
  66. # Send Mail
  67. context['generated_password'] = password
  68. mail_id = et_obj.send_mail(
  69. cr, uid, et.id, ru.id, True, context=context)
  70. del context['generated_password']
  71. return {}