Skip to content

Commit

Permalink
[IMP] partner_hide_technical_company: Do not prevent right access bas…
Browse files Browse the repository at this point in the history
…ed on group, but based on the way the user write on partners. (directly, or via the company
  • Loading branch information
legalsylvain committed Dec 13, 2024
1 parent 9c716ff commit 58380a6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
14 changes: 13 additions & 1 deletion partner_hide_technical_company/models/res_company.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ class ResCompany(models.Model):

@api.model_create_multi
def create(self, vals_list):
return super(ResCompany, self.with_context(is_odoo_company=True)).create(
# print("===========================================res.company..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()
20 changes: 10 additions & 10 deletions partner_hide_technical_company/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -36,23 +36,23 @@ 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()
companies = ResCompany.with_context(active_test=False).search(
[("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
Expand Down
22 changes: 14 additions & 8 deletions partner_hide_technical_company/tests/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -50,9 +47,18 @@ 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()

# With Correct access right, should success
self.company.unlink()

0 comments on commit 58380a6

Please sign in to comment.