diff --git a/partner_hide_technical_company/hooks.py b/partner_hide_technical_company/hooks.py index 597faaec..cd8cea12 100644 --- a/partner_hide_technical_company/hooks.py +++ b/partner_hide_technical_company/hooks.py @@ -10,4 +10,6 @@ def post_init_hook(cr, pool): env = Environment(cr, SUPERUSER_ID, {}) ResCompany = env["res.company"] companies = ResCompany.with_context(active_test=False).search([]) - companies.mapped("partner_id").write({"is_odoo_company": True}) + companies.mapped("partner_id").with_context(action_from_res_company=True).write( + {"is_odoo_company": True} + ) diff --git a/partner_hide_technical_company/i18n/fr.po b/partner_hide_technical_company/i18n/fr.po index 1a759ad5..f9568e6c 100644 --- a/partner_hide_technical_company/i18n/fr.po +++ b/partner_hide_technical_company/i18n/fr.po @@ -36,7 +36,7 @@ msgstr "Est une société dans Odoo" #: code:addons/partner_hide_technical_company/models/res_partner.py:0 #, python-format msgid "" -"You must be part of the group Administration / Access Rights to update partners associated to companies.\n" +"You can only update company partners via company form. Companies: \n" "- %s" -msgstr "Vous devez être membre du groupe 'Administration / Groupe d'accès' pour mettre à jour les partenaires associés aux sociétés.\n" +msgstr "Vous pouvez seulement mettre à jour les partenaires de société via le formulaire de société. Sociétés: \n" "- %s" diff --git a/partner_hide_technical_company/models/res_company.py b/partner_hide_technical_company/models/res_company.py index a234bf1e..26441da2 100644 --- a/partner_hide_technical_company/models/res_company.py +++ b/partner_hide_technical_company/models/res_company.py @@ -11,6 +11,17 @@ class ResCompany(models.Model): @api.model_create_multi def create(self, vals_list): - return super(ResCompany, self.with_context(is_odoo_company=True)).create( + res = super(ResCompany, self.with_context(action_from_res_company=True)).create( vals_list ) + return res.with_context(action_from_res_company=False) + + def write(self, vals): + return super(ResCompany, self.with_context(action_from_res_company=True)).write( + vals + ) + + def unlink(self): + return super( + ResCompany, self.with_context(action_from_res_company=True) + ).unlink() diff --git a/partner_hide_technical_company/models/res_partner.py b/partner_hide_technical_company/models/res_partner.py index 46ca6c8c..d226cf7e 100644 --- a/partner_hide_technical_company/models/res_partner.py +++ b/partner_hide_technical_company/models/res_partner.py @@ -21,7 +21,7 @@ class ResPartner(models.Model): # Overload Section @api.model_create_multi def create(self, vals_list): - if self.env.context.get("is_odoo_company"): + if self.env.context.get("action_from_res_company"): for vals in vals_list: vals["is_odoo_company"] = True return super().create(vals_list) @@ -36,6 +36,9 @@ def unlink(self): # Custom section def _check_technical_partner_access_company(self): + if self.env.context.get("action_from_res_company"): + return + # We use SUPERUSER_ID to be sure to not skip some users, due to # some custom access rules deployed on databases ResCompany = self.env["res.company"].sudo() @@ -43,16 +46,13 @@ def _check_technical_partner_access_company(self): [("partner_id", "in", self.ids)] ) if len(companies): - # Check if current user has correct access right - if not self.env.user.has_group("base.group_erp_manager"): - raise UserError( - _( - "You must be part of the group Administration / Access" - " Rights to update partners associated to" - " companies.\n- %s" - ) - % ("\n- ".join(companies.mapped("name"))) + raise UserError( + _( + "You can only update company partners via company form." + " Companies: \n- %s" ) + % ("\n- ".join(companies.mapped("name"))) + ) # Overload the private _search function: # This function is used by the other ORM functions diff --git a/partner_hide_technical_company/tests/test_module.py b/partner_hide_technical_company/tests/test_module.py index 0526013b..d5c1385d 100644 --- a/partner_hide_technical_company/tests/test_module.py +++ b/partner_hide_technical_company/tests/test_module.py @@ -11,13 +11,10 @@ class TestModule(TransactionCase): def setUpClass(cls): super().setUpClass() cls.ResPartner = cls.env["res.partner"] - cls.ResCompany = cls.env["res.company"] - cls.demo_user = cls.env.ref("base.user_demo") - cls.main_company = cls.env.ref("base.main_company") cls.company_name = "Users technical_partner_access - res.company" + cls.company = cls.env["res.company"].create({"name": cls.company_name}) - def test_01_company_part(self): - company = self.ResCompany.create({"name": self.company_name}) + def test_01_search_partner(self): # Check access without context (by search) result = self.ResPartner.search([("name", "=", self.company_name)]) self.assertEqual( @@ -50,9 +47,15 @@ def test_01_company_part(self): "Name Search company partner should return result with context", ) - # Without Correct access right, should fail + def test_02_write_partner(self): + # With incorrect way, should fail with self.assertRaises(UserError): - company.partner_id.with_user(self.demo_user).write({"name": "Test"}) + self.company.partner_id.write({"name": "RENAMED"}) # With Correct access right, should success - company.partner_id.write({"name": "Test"}) + self.company.write({"name": "RENAMED"}) + + def test_03_unlink_partner(self): + # With incorrect way, should fail + with self.assertRaises(UserError): + self.company.partner_id.unlink()