odoo_account_ebics/account_ebics/wizards/ebics_change_passphrase.py

77 lines
2.6 KiB
Python
Raw Normal View History

# Copyright 2009-2023 Noviat.
2019-12-22 09:54:00 +00:00
# License LGPL-3 or later (http://www.gnu.org/licenses/lpgl).
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", required=True)
new_pass = fields.Char(string="New Passphrase", required=True)
new_pass_check = fields.Char(string="New Passphrase (verification)", required=True)
note = fields.Text(string="Notes", readonly=True)
2018-08-14 15:04:39 +00:00
def change_passphrase(self):
self.ensure_one()
if (
self.ebics_userid_id.ebics_passphrase_store
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."))
2020-07-14 21:18:32 +00:00
if 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."))
2018-08-14 15:04:39 +00:00
try:
passphrase = (
self.ebics_userid_id.ebics_passphrase_store
and self.ebics_userid_id.ebics_passphrase
or self.old_pass
)
2018-08-14 15:04:39 +00:00
keyring = EbicsKeyRing(
2020-07-14 21:18:32 +00:00
keys=self.ebics_userid_id.ebics_keys_fn,
passphrase=passphrase,
2022-05-10 19:40:54 +00:00
)
2018-08-14 15:04:39 +00:00
keyring.change_passphrase(self.new_pass)
2022-10-27 21:48:16 +00:00
except ValueError as err:
raise UserError(str(err)) from err
2023-04-11 19:53:13 +00:00
self.ebics_userid_id.ebics_passphrase = self.new_pass
2018-08-14 15:04:39 +00:00
self.note = "The EBICS Passphrase has been changed."
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"}