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.

85 lines
2.5 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Akretion Sebastien Beau
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo import api, fields, models
  5. from odoo.tools.config import config
  6. class KeychainBackend(models.AbstractModel):
  7. _name = 'keychain.backend'
  8. _backend_name = None
  9. name = fields.Char(required=True)
  10. password = fields.Char(
  11. compute="_compute_password",
  12. inverse="_inverse_password",
  13. required=True)
  14. data = fields.Serialized(
  15. compute="_compute_keychain",
  16. inverse="_inverse_keychain",
  17. help="Additionnal data as json")
  18. @api.multi
  19. def _get_technical_name(self):
  20. self.ensure_one()
  21. return '%s,%s' % (self._name, self.id)
  22. @api.multi
  23. def _get_existing_keychain(self):
  24. self.ensure_one()
  25. return self.env['keychain.account'].retrieve([
  26. ('namespace', '=', self._backend_name),
  27. ('technical_name', '=', self._get_technical_name())
  28. ])
  29. @api.multi
  30. def _prepare_keychain(self):
  31. self.ensure_one()
  32. env = config.get('running_env')
  33. return {
  34. 'name': "%s %s" % (self.name, env),
  35. 'technical_name': self._get_technical_name(),
  36. 'namespace': self._backend_name,
  37. 'environment': env,
  38. }
  39. @api.multi
  40. def _get_keychain_account(self):
  41. self.ensure_one()
  42. account = self._get_existing_keychain()
  43. if not account:
  44. vals = self._prepare_keychain()
  45. account = self.env['keychain.account'].create(vals)
  46. return account
  47. @api.multi
  48. def _inverse_password(self):
  49. for record in self:
  50. account = self._get_keychain_account()
  51. if record.password and record.password != '******':
  52. account.clear_password = record.password
  53. @api.multi
  54. def _compute_password(self):
  55. for record in self:
  56. account = record._get_existing_keychain()
  57. if account and account.password:
  58. record.password = "******"
  59. else:
  60. record.password = ""
  61. @api.multi
  62. def _inverse_keychain(self):
  63. for record in self:
  64. account = record._get_keychain_account()
  65. account.data = account._serialize_data(record.data)
  66. @api.multi
  67. def _compute_keychain(self):
  68. for record in self:
  69. account = record._get_existing_keychain()
  70. if account:
  71. record.data = account.get_data()
  72. else:
  73. record.data = {}