From 2b4e7b284a18ac8edb600a5261fb3f16ad85efd0 Mon Sep 17 00:00:00 2001 From: Jairo Llopis Date: Wed, 9 Nov 2022 09:24:20 +0000 Subject: [PATCH] [FIX] mis_builder: let non-admins print reports Before this patch, normal users would get `AccessError` when trying to preview or print reports. Fixes https://github.com/OCA/mis-builder/issues/415. @moduon MT-1527 --- mis_builder/models/mis_report.py | 8 +++++--- mis_builder/models/mis_report_instance.py | 2 +- mis_builder/readme/CONTRIBUTORS.rst | 1 + mis_builder/readme/newsfragments/access-error.fix | 1 + mis_builder/tests/test_mis_report_instance.py | 4 ++++ 5 files changed, 12 insertions(+), 4 deletions(-) create mode 100644 mis_builder/readme/newsfragments/access-error.fix diff --git a/mis_builder/models/mis_report.py b/mis_builder/models/mis_report.py index 146c0abcc..dd04bbcce 100644 --- a/mis_builder/models/mis_report.py +++ b/mis_builder/models/mis_report.py @@ -475,9 +475,11 @@ def _compute_all_kpi_ids(self): @api.depends("move_lines_source") def _compute_account_model(self): for record in self: - record.account_model = record.move_lines_source.field_id.filtered( - lambda r: r.name == "account_id" - ).relation + record.account_model = ( + record.move_lines_source.sudo() + .field_id.filtered(lambda r: r.name == "account_id") + .relation + ) @api.onchange("subkpi_ids") def _on_change_subkpi_ids(self): diff --git a/mis_builder/models/mis_report_instance.py b/mis_builder/models/mis_report_instance.py index 42ce48fdc..ca41731fe 100644 --- a/mis_builder/models/mis_report_instance.py +++ b/mis_builder/models/mis_report_instance.py @@ -369,7 +369,7 @@ def _onchange_source(self): def _get_aml_model_name(self): self.ensure_one() if self.source == SRC_ACTUALS: - return self.report_id.move_lines_source.model + return self.report_id.move_lines_source.sudo().model elif self.source == SRC_ACTUALS_ALT: return self.source_aml_model_name return False diff --git a/mis_builder/readme/CONTRIBUTORS.rst b/mis_builder/readme/CONTRIBUTORS.rst index 72531f707..4f4aa67b8 100644 --- a/mis_builder/readme/CONTRIBUTORS.rst +++ b/mis_builder/readme/CONTRIBUTORS.rst @@ -24,3 +24,4 @@ * `CorporateHub `__ * Alexey Pelykh +* Jairo Llopis (https://www.moduon.team/) diff --git a/mis_builder/readme/newsfragments/access-error.fix b/mis_builder/readme/newsfragments/access-error.fix new file mode 100644 index 000000000..e739379fd --- /dev/null +++ b/mis_builder/readme/newsfragments/access-error.fix @@ -0,0 +1 @@ +Fix access error when previewing or printing report. diff --git a/mis_builder/tests/test_mis_report_instance.py b/mis_builder/tests/test_mis_report_instance.py index 55b23583c..a294740da 100644 --- a/mis_builder/tests/test_mis_report_instance.py +++ b/mis_builder/tests/test_mis_report_instance.py @@ -586,3 +586,7 @@ def test_raise_when_unknown_kpi_value_type(self): def test_raise_when_wrong_tuple_length_with_subkpis(self): with self.assertRaises(SubKPITupleLengthError): self.report_instance_3.compute() + + def test_unprivileged(self): + test_user = common.new_test_user(self.env, "mis_you") + self.report_instance.with_user(test_user).compute()