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.

61 lines
2.5 KiB

  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
  27. from openerp.osv.osv import except_osv
  28. from openerp.tools.translate import _
  29. class res_users(Model):
  30. _inherit = 'res.users'
  31. def generate_password(self, cr, uid, ids, context=None):
  32. mm_obj = self.pool['mail.mail']
  33. icp_obj = self.pool['ir.config_parameter']
  34. imd_obj = self.pool['ir.model.data']
  35. et_obj = self.pool['email.template']
  36. password_size = eval(icp_obj.get_param(
  37. cr, uid, 'auth_generate_password.password_size'))
  38. password_chars = eval(icp_obj.get_param(
  39. cr, uid, 'auth_generate_password.password_chars'))
  40. for ru in self.browse(cr, uid, ids, context=context):
  41. if not ru.email:
  42. raise osv.except_osv(
  43. _("Cannot send email: user has no email address."),
  44. user.name)
  45. # Generate Password
  46. password = "".join([random.choice(
  47. password_chars) for n in xrange(password_size)])
  48. self._set_new_password(
  49. cr, uid, ru.id, None, password, None, context=None)
  50. # Send Mail
  51. et = imd_obj.get_object(
  52. cr, uid, 'auth_generate_password',
  53. 'generate_password_template')
  54. mail_id = et_obj.send_mail(
  55. cr, uid, et.id, ru.id, True, context=context)
  56. return {}