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.

69 lines
2.8 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. class res_users(Model):
  29. _inherit = 'res.users'
  30. def generate_password(self, cr, uid, ids, context=None):
  31. if not context:
  32. context = {}
  33. mm_obj = self.pool['mail.mail']
  34. icp_obj = self.pool['ir.config_parameter']
  35. imd_obj = self.pool['ir.model.data']
  36. et_obj = self.pool['email.template']
  37. try:
  38. int(icp_obj.get_param(
  39. cr, uid, 'auth_generate_password.password_size'))
  40. except:
  41. raise except_orm(_("error"), _("Only digit chars authorized"))
  42. password_size = eval(icp_obj.get_param(
  43. cr, uid, 'auth_generate_password.password_size'))
  44. password_chars = eval(icp_obj.get_param(
  45. cr, uid, 'auth_generate_password.password_chars'))
  46. et = imd_obj.get_object(
  47. cr, uid, 'auth_generate_password', 'generate_password_template')
  48. for ru in self.browse(cr, uid, ids, context=context):
  49. if not ru.email:
  50. raise except_orm(
  51. _("Cannot send email: user has no email address."),
  52. user.name)
  53. # Generate Password
  54. password = "".join([random.choice(
  55. password_chars) for n in xrange(password_size)])
  56. self._set_new_password(
  57. cr, uid, ru.id, None, password, None, context=None)
  58. # Send Mail
  59. context['generated_password'] = password
  60. mail_id = et_obj.send_mail(
  61. cr, uid, et.id, ru.id, True, context=context)
  62. del context['generated_password']
  63. return {}