From 2eabcad8c021e63cf774101863a6824483b21aa6 Mon Sep 17 00:00:00 2001 From: joshua160miller Date: Thu, 12 Dec 2024 22:23:56 +0530 Subject: [PATCH] added the encryption and decryption in documents --- .../static/src/js/preview_document.js | 37 ++++++++++++++++++- .../views/registrant_document_views.xml | 2 + g2p_registry_encryption/models/partner.py | 17 +++++---- .../views/res_config_view.xml | 2 +- 4 files changed, 47 insertions(+), 11 deletions(-) diff --git a/g2p_registry_documents/static/src/js/preview_document.js b/g2p_registry_documents/static/src/js/preview_document.js index 7702fb71..0aa938b4 100644 --- a/g2p_registry_documents/static/src/js/preview_document.js +++ b/g2p_registry_documents/static/src/js/preview_document.js @@ -1,17 +1,50 @@ /** @odoo-module **/ -import {Component, xml} from "@odoo/owl"; +import {Component, onWillStart, xml} from "@odoo/owl"; import {registry} from "@web/core/registry"; import {useFileViewer} from "@web/core/file_viewer/file_viewer_hook"; import {useService} from "@web/core/utils/hooks"; class Widgetpreview extends Component { - static template = xml``; + static template = xml` + + + Encrypted + + `; setup() { super.setup(); this.fileViewer = useFileViewer(); this.store = useService("mail.store"); this.rpc = useService("rpc"); + + onWillStart(async () => { + this.decryptRegistry = await this._getDecryptRegistryValue(); + this.canPreview = this._checkPreviewConditions(); + }); + } + + async _getDecryptRegistryValue() { + const result = await this.rpc("/web/dataset/call_kw/ir.config_parameter/get_param", { + model: "ir.config_parameter", + method: "get_param", + args: ["g2p_registry_encryption.decrypt_registry"], + kwargs: {}, + }); + return result === "True"; + } + + _checkPreviewConditions() { + const is_encrypted = this.props.record.data.is_encrypted; + const decrypt_registry = this.decryptRegistry; + return !is_encrypted || (is_encrypted && decrypt_registry); } clickPreview(ev) { diff --git a/g2p_registry_documents/views/registrant_document_views.xml b/g2p_registry_documents/views/registrant_document_views.xml index 549189e3..3cc4c094 100644 --- a/g2p_registry_documents/views/registrant_document_views.xml +++ b/g2p_registry_documents/views/registrant_document_views.xml @@ -13,6 +13,7 @@ + @@ -34,6 +35,7 @@ + diff --git a/g2p_registry_encryption/models/partner.py b/g2p_registry_encryption/models/partner.py index 9702a0fb..a43f195d 100644 --- a/g2p_registry_encryption/models/partner.py +++ b/g2p_registry_encryption/models/partner.py @@ -89,14 +89,15 @@ def _fetch_query(self, query, fields): ) if not is_decrypt_fields: return res - for record in self: - is_encrypted, encrypted_val = record.get_encrypted_val()[0] - if is_encrypted and encrypted_val: - decrypted_vals = json.loads(prov.decrypt_data(encrypted_val).decode()) - - for field_name in enc_fields_set: - if field_name in decrypted_vals and field_name in record and record[field_name]: - self.env.cache.set(record, self._fields[field_name], decrypted_vals[field_name]) + if len(self) == 1 and self.is_registrant: + for record in self: + is_encrypted, encrypted_val = record.get_encrypted_val()[0] + if is_encrypted and encrypted_val: + decrypted_vals = json.loads(prov.decrypt_data(encrypted_val).decode()) + + for field_name in enc_fields_set: + if field_name in decrypted_vals and field_name in record and record[field_name]: + self.env.cache.set(record, self._fields[field_name], decrypted_vals[field_name]) return res def get_encrypted_val(self): diff --git a/g2p_registry_encryption/views/res_config_view.xml b/g2p_registry_encryption/views/res_config_view.xml index 928602ca..59740b94 100644 --- a/g2p_registry_encryption/views/res_config_view.xml +++ b/g2p_registry_encryption/views/res_config_view.xml @@ -26,7 +26,7 @@