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.

100 lines
3.0 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 openerp import api, fields, models
  5. from openerp.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. keychain_account_id = fields.Many2one(
  19. 'keychain.account', compute='_compute_keychain_account')
  20. @api.multi
  21. def _compute_keychain_account(self):
  22. for backend in self:
  23. backend.keychain_account_id = backend._get_existing_keychain()
  24. @api.multi
  25. def _get_technical_name(self):
  26. self.ensure_one()
  27. return '%s,%s' % (self._name, self.id)
  28. @api.multi
  29. def _get_existing_keychain(self):
  30. self.ensure_one()
  31. return self.env['keychain.account'].retrieve([
  32. ('namespace', '=', self._backend_name),
  33. ('technical_name', '=', self._get_technical_name())
  34. ])
  35. @api.multi
  36. def _prepare_keychain(self):
  37. self.ensure_one()
  38. env = config.get('running_env')
  39. return {
  40. 'name': "%s %s" % (self.name, env),
  41. 'technical_name': self._get_technical_name(),
  42. 'namespace': self._backend_name,
  43. 'environment': env,
  44. }
  45. @api.multi
  46. def _get_keychain_account(self):
  47. self.ensure_one()
  48. if not self.keychain_account_id:
  49. vals = self._prepare_keychain()
  50. self.keychain_account_id = self.env['keychain.account'].create(
  51. vals)
  52. return self.keychain_account_id
  53. @api.multi
  54. def _inverse_password(self):
  55. for record in self:
  56. account = self._get_keychain_account()
  57. if record.password and record.password != '******':
  58. account.clear_password = record.password
  59. @api.multi
  60. def _compute_password(self):
  61. for record in self:
  62. account = record._get_existing_keychain()
  63. if account and account.password:
  64. record.password = "******"
  65. else:
  66. record.password = ""
  67. @api.multi
  68. def _inverse_keychain(self):
  69. for record in self:
  70. account = record._get_keychain_account()
  71. account.data = account._serialize_data(record.data)
  72. @api.multi
  73. def _compute_keychain(self):
  74. for record in self:
  75. account = record._get_existing_keychain()
  76. if account:
  77. record.data = account.get_data()
  78. else:
  79. record.data = {}
  80. @api.multi
  81. def _get_password(self):
  82. self.ensure_one()
  83. if self.keychain_account_id:
  84. return self.keychain_account_id._get_password()
  85. else:
  86. return False