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.

58 lines
2.3 KiB

8 years ago
8 years ago
  1. # coding: utf-8
  2. # Copyright 2014 David BEAL @ Akretion <david.beal@akretion.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from openerp import _, models, fields, api
  5. from openerp.exceptions import Warning as UserError
  6. from openerp.tools.config import config
  7. def _get_authorized_password():
  8. """ You can define your own authorized keys
  9. """
  10. return [config.get("secure_uninstall"), config.get("admin_passwd")]
  11. class BaseModuleUpgrade(models.TransientModel):
  12. _inherit = 'base.module.upgrade'
  13. uninstall_password = fields.Char(
  14. string='Password',
  15. help="'secure_uninstall' value from Odoo configuration file ")
  16. @api.multi
  17. def upgrade_module(self):
  18. for elm in self:
  19. if not config.get("secure_uninstall"):
  20. self.rollback_state_modules()
  21. raise UserError(_(
  22. "Missing configuration key\n--------------------\n"
  23. "'secure_uninstall' configuration key "
  24. "is not set in \n"
  25. "your Odoo server configuration file: "
  26. "please set it a value"))
  27. if elm.uninstall_password not in _get_authorized_password():
  28. self.rollback_state_modules()
  29. raise UserError(_(
  30. "Password Error\n--------------------\n"
  31. "Provided password '%s' doesn't match with "
  32. "'Master Password'\n('secure_uninstall' key) found in "
  33. "the Odoo server configuration file ."
  34. "\n\nResolution\n-------------\n"
  35. "Please check your password and retry or cancel")
  36. % elm.uninstall_password)
  37. # keep this password in db is insecure, then we remove it
  38. elm.uninstall_password = False
  39. return super(BaseModuleUpgrade, self).upgrade_module()
  40. def rollback_state_modules(self):
  41. modules = self.env['ir.module.module'].browse(
  42. self._context.get('active_ids'))
  43. if modules:
  44. module_ids = modules.downstream_dependencies(
  45. exclude_states=['uninstalled', 'uninstallable'])
  46. module_ids = module_ids + list(modules._ids)
  47. self.env['ir.module.module'].browse(
  48. module_ids).button_uninstall_cancel()
  49. self._cr.commit()
  50. return True