Skip to content

Commit 3e564da

Browse files
Milan Topuzovclement-gouin
authored andcommitted
[MIG] base_user_role_company: migrate to 19.0
- Replace groups_id with group_ids in views/actions - Bump version to 19.0.1.0.0; adjust manifests/assets - Use env attributes and Domain expressions - Adopt new Constraint/Index APIs where applicable Functional changes
1 parent 71b8f47 commit 3e564da

8 files changed

Lines changed: 61 additions & 31 deletions

File tree

base_user_role_company/controllers/main.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88

99
class HomeExtended(Home):
1010
@http.route()
11-
def web_load_menus(self, unique):
12-
response = super().web_load_menus(unique)
11+
def web_load_menus(self, *args, **kwargs):
12+
response = super().web_load_menus(*args, **kwargs)
1313
# On logout & re-login we could see wrong menus being rendered
1414
# To avoid this, menu http cache must be disabled
15-
response.headers.remove("Cache-Control")
15+
# Using .pop() as Werkzeug Headers no longer supports __delitem__ (del)
16+
response.headers.pop("Cache-Control", None)
1617
return response

base_user_role_company/models/ir_http.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
class IrHttp(models.AbstractModel):
99
_inherit = "ir.http"
1010

11-
def session_info(self):
11+
def session_info(self, *args, **kwargs):
1212
"""
1313
Based on the selected companies (cids),
1414
calculate the roles to enable.
1515
A role should be enabled only when it applies to all selected companies.
1616
"""
17-
result = super().session_info()
17+
result = super().session_info(*args, **kwargs)
1818
if self.env.user.role_line_ids:
1919
cids_str = request.httprequest.cookies.get("cids", str(self.env.company.id))
2020
cids = [int(cid) for cid in cids_str.split("-")]

base_user_role_company/models/role.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ def _check_company(self):
2828
raise ValidationError(
2929
self.env._(
3030
'User "%(user)s" does not have access to the company '
31-
'"%(company)s"'
31+
'"%(company)s"',
32+
user=record.user_id.name,
33+
company=record.company_id.name,
3234
)
33-
% {"user": record.user_id.name, "company": record.company_id.name}
3435
)
3536

36-
_sql_constraints = [
37-
(
38-
"user_role_uniq",
39-
"unique (user_id,role_id,company_id)",
40-
"Roles can be assigned to a user only once at a time",
41-
)
42-
]
37+
# Override parent unique constraint to allow the same role multiple times
38+
# for a user, provided company_id differs.
39+
_user_role_uniq = models.Constraint(
40+
"UNIQUE (user_id, role_id, company_id)",
41+
"Roles can be assigned to a user only once at a time",
42+
)

base_user_role_company/models/user.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,18 @@
77
class ResUsers(models.Model):
88
_inherit = "res.users"
99

10-
@classmethod
11-
def authenticate(cls, db, credential, user_agent_env):
12-
auth_info = super().authenticate(db, credential, user_agent_env)
13-
# On login, ensure the proper roles are applied
14-
# The last Role applied may not be the correct one,
15-
# sonce the new session current company can be different
16-
with cls.pool.cursor() as cr:
17-
env = api.Environment(cr, auth_info["uid"], {})
10+
def authenticate(self, *args, **kwargs):
11+
auth_info = super().authenticate(*args, **kwargs)
12+
# Ensure proper roles are applied for the logged-in user
13+
uid = auth_info.get("uid") if isinstance(auth_info, dict) else None
14+
if uid:
15+
env = api.Environment(self.env.cr, uid, {})
1816
if env.user.role_line_ids:
1917
env.user.set_groups_from_roles()
2018
return auth_info
2119

22-
def _get_enabled_roles(self):
23-
res = super()._get_enabled_roles()
20+
def _get_enabled_roles(self, *args, **kwargs):
21+
res = super()._get_enabled_roles(*args, **kwargs)
2422
if self.role_line_ids:
2523
active_roles = self.env["res.users.role.line"]
2624
if self.env.context.get("active_company_ids"):
@@ -32,8 +30,9 @@ def _get_enabled_roles(self):
3230
active_roles |= role_line
3331
elif role_line.company_id.id in company_ids:
3432
role_line_companies = self.role_line_ids.filtered(
35-
lambda x, rl=role_line: x.role_id == rl.role_id
36-
and x.company_id.id in company_ids
33+
lambda x, rl=role_line: (
34+
x.role_id == rl.role_id and x.company_id.id in company_ids
35+
)
3736
)
3837
if len(role_line_companies) == len(company_ids):
3938
active_roles |= role_line
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
from . import test_role_per_company
22
from . import test_use_only_enabled_roles
3+
from . import test_load_menus
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright 2026 Open Source Integrators
2+
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
3+
4+
from odoo.tests.common import HttpCase
5+
6+
7+
class LoadMenusTests(HttpCase):
8+
def setUp(self):
9+
super().setUp()
10+
self.authenticate("admin", "admin")
11+
12+
def test_load_menus(self):
13+
# expect no errors on HomeExtended.web_load_menus controller
14+
self.url_open("/web/webclient/load_menus")

base_user_role_company/tests/test_role_per_company.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Copyright 2021 Open Source Integrators
22
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
33

4+
from odoo.exceptions import ValidationError
45
from odoo.tests.common import TransactionCase
56

67

@@ -11,6 +12,7 @@ def setUp(self):
1112
self.Company = self.env["res.company"]
1213
self.company1 = self.env.ref("base.main_company")
1314
self.company2 = self.Company.create({"name": "company2"})
15+
self.company3 = self.Company.create({"name": "company3"})
1416
# GROUPS for roles
1517
self.groupA = self.env.ref("base.group_user")
1618
self.groupB = self.env.ref("base.group_system")
@@ -49,15 +51,15 @@ def test_110_company_1(self):
4951
active_company_ids=self.company1.ids
5052
).set_groups_from_roles()
5153
expected = self.groupA | self.groupB | self.groupC
52-
found = self.test_user.groups_id.filtered(lambda x: x in expected)
54+
found = self.test_user.group_ids.filtered(lambda x: x in expected)
5355
self.assertEqual(expected, found)
5456

5557
def test_120_company_2(self):
5658
"Company 2 selected: Roles A and C are enabled"
5759
self.test_user.with_context(
5860
active_company_ids=self.company2.ids
5961
).set_groups_from_roles()
60-
enabled = self.test_user.groups_id
62+
enabled = self.test_user.group_ids
6163
expected = self.groupA | self.groupC
6264
found = enabled.filtered(lambda x: x in expected)
6365
self.assertEqual(expected, found)
@@ -71,11 +73,24 @@ def test_130_all_company(self):
7173
self.test_user.with_context(
7274
active_company_ids=[self.company1.id, self.company2.id]
7375
).set_groups_from_roles()
74-
enabled = self.test_user.groups_id
76+
enabled = self.test_user.group_ids
7577
expected = self.groupA | self.groupC
7678
found = enabled.filtered(lambda x: x in expected)
7779
self.assertEqual(expected, found)
7880

7981
not_expected = self.groupB
8082
found = enabled.filtered(lambda x: x in not_expected)
8183
self.assertFalse(found)
84+
85+
def test_140_company_3(self):
86+
"Company 3 selected: ValidationError"
87+
user_vals = {
88+
"name": "ROLES TEST USER 2",
89+
"login": "test_user_2",
90+
"company_ids": [(6, 0, [self.company1.id, self.company2.id])],
91+
"role_line_ids": [
92+
(0, 0, {"role_id": self.roleA.id, "company_id": self.company3.id}),
93+
],
94+
}
95+
with self.assertRaises(ValidationError):
96+
self.User.create(user_vals)

base_user_role_company/tests/test_use_only_enabled_roles.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def test_110_enabled_role_is_used(self):
4646
active_company_ids=self.company1.ids
4747
).set_groups_from_roles()
4848
expected = self.groupA | self.groupB
49-
found = self.test_user.groups_id.filtered(lambda x: x in expected)
49+
found = self.test_user.group_ids.filtered(lambda x: x in expected)
5050
self.assertEqual(expected, found)
5151

5252
def test_120_disabled_role_is_not_used(self):
@@ -58,5 +58,5 @@ def test_120_disabled_role_is_not_used(self):
5858
active_company_ids=self.company1.ids
5959
).set_groups_from_roles()
6060
expected = self.groupA
61-
found = self.test_user.groups_id.filtered(lambda x: x in expected)
61+
found = self.test_user.group_ids.filtered(lambda x: x in expected)
6262
self.assertEqual(expected, found)

0 commit comments

Comments
 (0)