# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). import json import logging from lxml import etree from odoo import _, api, models from odoo.exceptions import ValidationError from odoo.tools.config import config _logger = logging.getLogger(__name__) class ServerEnvMixin(models.AbstractModel): _inherit = "server.env.mixin" def _current_env_encrypted_key_exists(self): env = self.env["encrypted.data"]._retrieve_env() key_name = "encryption_key_%s" % env key_str = config.get(key_name) key_exists = key_str and True or False if not key_exists: logging.warning( "The minimal configuration is missing. You need at least to add an " "encryption key for the current environment : %s. While the " "configuration is missing, the module has no effect", env, ) return key_exists def _compute_server_env_from_default(self, field_name, options): """First return database encrypted value then default value""" # in case of bad configuration (no encryption key for current env) the module # is useless, we do fallback directly on serven_environement behavior if not self._current_env_encrypted_key_exists(): return super()._compute_server_env_from_default(field_name, options) encrypted_data_name = "{},{}".format(self._name, self.id) env = self.env.context.get("environment", None) vals = ( self.env["encrypted.data"] .sudo() ._encrypted_read_json(encrypted_data_name, env=env) ) if vals.get(field_name): self[field_name] = vals[field_name] else: return super()._compute_server_env_from_default(field_name, options) def _inverse_server_env(self, field_name): """ When this module is installed, we store values into encrypted data env instead of a default field in database (not env dependent). """ # in case of bad configuration (no encryption key for current env) the module # is useless, we do fallback directly on serven_environement behavior if not self._current_env_encrypted_key_exists(): return super()._inverse_server_env(field_name) is_editable_field = self._server_env_is_editable_fieldname(field_name) encrypted_data_obj = self.env["encrypted.data"].sudo() env = self.env.context.get("environment", None) for record in self: if record[is_editable_field]: encrypted_data_name = "{},{}".format(record._name, record.id) values = encrypted_data_obj._encrypted_read_json( encrypted_data_name, env=env ) new_val = {field_name: record[field_name]} values.update(new_val) encrypted_data_obj._encrypted_store_json( encrypted_data_name, values, env=env ) def action_change_env_data_encrypted_fields(self): action_id = self.env.context.get("params", {}).get("action") if not action_id: # We don't know which action we are using... take default one action = self.get_formview_action() else: action = ( self.env["ir.actions.act_window"].browse(action_id).sudo().read()[0] ) action["view_mode"] = "form" action["res_id"] = self.id views_form = [] for view_id, view_type in action.get("views", []): if view_type == "form": views_form.append((view_id, view_type)) action["views"] = views_form return action def _get_extra_environment_info_div(self, current_env, extra_envs): # if the module configuration is missing (no current env encryption key) # display a warning instead as the module has no effect. if not self._current_env_encrypted_key_exists(): button_div = "