odoo_account_ebics/account_ebics/wizards/ebics_change_passphrase.py

121 lines
4.5 KiB
Python
Raw Normal View History

2024-03-29 08:17:06 +00:00
# Copyright 2009-2024 Noviat.
2023-05-28 15:01:06 +00:00
# License LGPL-3 or later (http://www.gnu.org/licenses/lgpl).
2018-08-14 15:04:39 +00:00
import logging
2020-06-24 21:39:33 +00:00
from odoo import _, fields, models
2018-08-14 15:04:39 +00:00
from odoo.exceptions import UserError
_logger = logging.getLogger(__name__)
try:
import fintech
from fintech.ebics import EbicsKeyRing
2022-05-10 19:40:54 +00:00
fintech.cryptolib = "cryptography"
2018-08-14 15:04:39 +00:00
except ImportError:
2022-05-10 19:40:54 +00:00
_logger.warning("Failed to import fintech")
2018-08-14 15:04:39 +00:00
class EbicsChangePassphrase(models.TransientModel):
2022-05-10 19:40:54 +00:00
_name = "ebics.change.passphrase"
_description = "Change EBICS keys passphrase"
2018-08-14 15:04:39 +00:00
2020-07-14 21:18:32 +00:00
ebics_userid_id = fields.Many2one(
2022-05-10 19:40:54 +00:00
comodel_name="ebics.userid", string="EBICS UserID", readonly=True
)
old_pass = fields.Char(string="Old Passphrase")
new_pass = fields.Char(string="New Passphrase")
new_pass_check = fields.Char(string="New Passphrase (verification)")
old_sig_pass = fields.Char(string="Old Signature Passphrase")
new_sig_pass = fields.Char(string="New Signature Passphrase")
new_sig_pass_check = fields.Char(string="New Signature Passphrase (verification)")
ebics_sig_passphrase_invisible = fields.Boolean(
compute="_compute_ebics_sig_passphrase_invisible"
)
2022-05-10 19:40:54 +00:00
note = fields.Text(string="Notes", readonly=True)
2018-08-14 15:04:39 +00:00
def _compute_ebics_sig_passphrase_invisible(self):
for rec in self:
if fintech.__version_info__ < (7, 3, 1):
rec.ebics_sig_passphrase_invisible = True
else:
rec.ebics_sig_passphrase_invisible = False
2018-08-14 15:04:39 +00:00
def change_passphrase(self):
self.ensure_one()
self.note = ""
if (
self.ebics_userid_id.ebics_passphrase_store
and self.old_pass
and self.old_pass != self.ebics_userid_id.ebics_passphrase
):
2022-05-10 19:40:54 +00:00
raise UserError(_("Incorrect old passphrase."))
2018-08-14 15:04:39 +00:00
if self.new_pass != self.new_pass_check:
2022-05-10 19:40:54 +00:00
raise UserError(_("New passphrase verification error."))
if self.new_pass and self.new_pass == self.ebics_userid_id.ebics_passphrase:
2022-05-10 19:40:54 +00:00
raise UserError(_("New passphrase equal to old passphrase."))
if (
self.new_sig_pass
and self.old_sig_pass
and self.new_sig_pass == self.old_sig_pass
):
raise UserError(
_("New signature passphrase equal to old signature passphrase.")
2022-05-10 19:40:54 +00:00
)
if self.new_sig_pass != self.new_sig_pass_check:
raise UserError(_("New signature passphrase verification error."))
passphrase = (
self.ebics_userid_id.ebics_passphrase_store
and self.ebics_userid_id.ebics_passphrase
or self.old_pass
)
try:
keyring_params = {
"keys": self.ebics_userid_id.ebics_keys_fn,
"passphrase": passphrase,
}
if self.new_sig_pass:
2024-03-29 08:17:06 +00:00
keyring_params["sig_passphrase"] = self.old_sig_pass or None
keyring = EbicsKeyRing(**keyring_params)
change_params = {}
if self.new_pass:
change_params["passphrase"] = self.new_pass
if self.new_sig_pass:
change_params["sig_passphrase"] = self.new_sig_pass
if change_params:
keyring.change_passphrase(**change_params)
except (ValueError, RuntimeError) as err:
2022-10-27 21:48:16 +00:00
raise UserError(str(err)) from err
if self.new_pass:
self.ebics_userid_id.ebics_passphrase = (
self.ebics_userid_id.ebics_passphrase_store and self.new_pass
)
self.note += "The EBICS Passphrase has been changed."
if self.new_sig_pass:
# removing ebics_sig_passphrase from db should not be required
# but we do it for double safety
if self.ebics_userid_id.ebics_sig_passphrase:
self.ebics_userid_id.ebics_sig_passphrase = False
self.note += "The EBICS Signature Passphrase has been changed."
2018-08-14 15:04:39 +00:00
2022-05-10 19:40:54 +00:00
module = __name__.split("addons.")[1].split(".")[0]
2018-08-14 15:04:39 +00:00
result_view = self.env.ref(
2022-05-10 19:40:54 +00:00
"%s.ebics_change_passphrase_view_form_result" % module
)
2018-08-14 15:04:39 +00:00
return {
2022-05-10 19:40:54 +00:00
"name": _("EBICS Keys Change Passphrase"),
"res_id": self.id,
"view_type": "form",
"view_mode": "form",
"res_model": "ebics.change.passphrase",
"view_id": result_view.id,
"target": "new",
"type": "ir.actions.act_window",
2018-08-14 15:04:39 +00:00
}
def button_close(self):
self.ensure_one()
2022-05-10 19:40:54 +00:00
return {"type": "ir.actions.act_window_close"}