From 3f62f1298fdc122531244d60c680b8ac936ecbf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Mart=C3=ADnez?= Date: Mon, 5 Aug 2024 09:26:39 +0200 Subject: [PATCH] [FIX] dms_field: Avoid access error in dms.access.groups linked to another company's records TT50390 --- dms_field/models/dms_access_group.py | 15 +++++++++++++++ dms_field/security/security.xml | 8 ++++++++ dms_field/tests/test_dms_field.py | 21 +++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/dms_field/models/dms_access_group.py b/dms_field/models/dms_access_group.py index 0ba63a20f..7dc7cd097 100644 --- a/dms_field/models/dms_access_group.py +++ b/dms_field/models/dms_access_group.py @@ -12,6 +12,12 @@ class DmsAccessGroups(models.Model): selection="_selection_reference_value", string="DMS field reference", ) + company_id = fields.Many2one( + compute="_compute_company_id", + comodel_name="res.company", + string="Company", + store=True, + ) @api.model def _selection_reference_value(self): @@ -22,6 +28,15 @@ def _selection_reference_value(self): ) return [(model.model, model.name) for model in models] + @api.depends("dms_field_ref") + def _compute_company_id(self): + for item in self.filtered("dms_field_ref"): + item.company_id = ( + item.dms_field_ref.company_id + if "company_id" in item.dms_field_ref._fields + else False + ) + def _get_item_from_dms_field_ref(self, record): return self.env["dms.access.group"].search( [("dms_field_ref", "=", "%s,%s" % (record._name, record.id))] diff --git a/dms_field/security/security.xml b/dms_field/security/security.xml index 9a1dc5e79..8dcd29e16 100644 --- a/dms_field/security/security.xml +++ b/dms_field/security/security.xml @@ -8,4 +8,12 @@ name="domain_force" >['|',('company_id','=',False),('company_id','in',company_ids)] + + DMS Access Group multi-company + + + ['|',('company_id','=',False),('company_id','in',company_ids)] + diff --git a/dms_field/tests/test_dms_field.py b/dms_field/tests/test_dms_field.py index 7aa374556..a96ca43f8 100644 --- a/dms_field/tests/test_dms_field.py +++ b/dms_field/tests/test_dms_field.py @@ -111,6 +111,27 @@ def test_dms_access_group_constrains_dms_field_ref(self): with self.assertRaises(UserError): group.copy({"name": "Test 2"}) + def test_dms_access_group_company_dms_field_ref_01(self): + self.partner.company_id = False + group = self.env["dms.access.group"].create( + { + "name": "Test 1", + "dms_field_ref": "%s,%s" % (self.partner._name, self.partner.id), + } + ) + self.assertFalse(group.company_id) + + def test_dms_access_group_company_dms_field_ref_02(self): + self.company = self.env.company + self.partner.company_id = self.company + group = self.env["dms.access.group"].create( + { + "name": "Test 1", + "dms_field_ref": "%s,%s" % (self.partner._name, self.partner.id), + } + ) + self.assertEqual(group.company_id, self.company) + def test_template_directory(self): self.assertTrue(self.template.dms_directory_ids) self.assertIn(