Skip to content

Commit

Permalink
[FIX] base_user_role_company: actually only allow roles with companies
Browse files Browse the repository at this point in the history
when this role is set for all currently active companies
  • Loading branch information
hbrunn committed Feb 19, 2024
1 parent a2e03bf commit 28c9cec
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
3 changes: 1 addition & 2 deletions base_user_role_company/models/ir_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ def session_info(self):
if self.env.user.role_line_ids:
cids_str = request.httprequest.cookies.get("cids", str(self.env.company.id))

Check warning on line 19 in base_user_role_company/models/ir_http.py

View check run for this annotation

Codecov / codecov/patch

base_user_role_company/models/ir_http.py#L19

Added line #L19 was not covered by tests
cids = [int(cid) for cid in cids_str.split(",")]
# The first element of cids is the currently selected company
self.env.user.set_groups_from_roles(company_id=cids[0])
self.env.user.with_context(allowed_company_ids=cids).set_groups_from_roles()
return result

Check warning on line 22 in base_user_role_company/models/ir_http.py

View check run for this annotation

Codecov / codecov/patch

base_user_role_company/models/ir_http.py#L21-L22

Added lines #L21 - L22 were not covered by tests
15 changes: 8 additions & 7 deletions base_user_role_company/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ def _get_enabled_roles(self):
# Enable only the Roles corresponing to the currently selected company
if self.role_line_ids:
res = res.filtered(
lambda x: not x.company_id or x.company_id == self.env.company
lambda x: not x.company_id
or all(
cid
in res.filtered(lambda y: y.role_id == x.role_id).mapped(
"company_id.id"
)
for cid in self.env.companies.ids
)
)
return res

def set_groups_from_roles(self, force=False, company_id=False):
# When using the Company Switcher widget, the self.env.company is not yet set
if company_id:
self = self.with_company(company_id)
return super().set_groups_from_roles(force=force)
22 changes: 20 additions & 2 deletions base_user_role_company/tests/test_role_per_company.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,32 @@ def setUp(self):

def test_110_company_1(self):
"Company 1 selected: Roles A, B and C are enabled"
self.test_user.set_groups_from_roles(company_id=self.company1.id)
self.test_user.with_context(
allowed_company_ids=self.company1.ids
).set_groups_from_roles()
expected = self.groupA | self.groupB | self.groupC
found = self.test_user.groups_id.filtered(lambda x: x in expected)
self.assertEqual(expected, found)

def test_120_company_2(self):
"Company 2 selected: Roles A and C are enabled"
self.test_user.set_groups_from_roles(company_id=self.company2.id)
self.test_user.with_context(
allowed_company_ids=self.company2.ids
).set_groups_from_roles()
enabled = self.test_user.groups_id
expected = self.groupA | self.groupC
found = enabled.filtered(lambda x: x in expected)
self.assertEqual(expected, found)

not_expected = self.groupB
found = enabled.filtered(lambda x: x in not_expected)
self.assertFalse(found)

def test_130_company_1_and_2(self):
"Company 1+2 selected: Roles A and C are enabled"
self.test_user.with_context(
allowed_company_ids=(self.company1 + self.company2).ids
).set_groups_from_roles()
enabled = self.test_user.groups_id
expected = self.groupA | self.groupC
found = enabled.filtered(lambda x: x in expected)
Expand Down

0 comments on commit 28c9cec

Please sign in to comment.