Skip to content

Commit a4aff05

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 a4aff05

6 files changed

Lines changed: 31 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

base_user_role_company/tests/test_role_per_company.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ def test_110_company_1(self):
4949
active_company_ids=self.company1.ids
5050
).set_groups_from_roles()
5151
expected = self.groupA | self.groupB | self.groupC
52-
found = self.test_user.groups_id.filtered(lambda x: x in expected)
52+
found = self.test_user.group_ids.filtered(lambda x: x in expected)
5353
self.assertEqual(expected, found)
5454

5555
def test_120_company_2(self):
5656
"Company 2 selected: Roles A and C are enabled"
5757
self.test_user.with_context(
5858
active_company_ids=self.company2.ids
5959
).set_groups_from_roles()
60-
enabled = self.test_user.groups_id
60+
enabled = self.test_user.group_ids
6161
expected = self.groupA | self.groupC
6262
found = enabled.filtered(lambda x: x in expected)
6363
self.assertEqual(expected, found)
@@ -71,7 +71,7 @@ def test_130_all_company(self):
7171
self.test_user.with_context(
7272
active_company_ids=[self.company1.id, self.company2.id]
7373
).set_groups_from_roles()
74-
enabled = self.test_user.groups_id
74+
enabled = self.test_user.group_ids
7575
expected = self.groupA | self.groupC
7676
found = enabled.filtered(lambda x: x in expected)
7777
self.assertEqual(expected, found)

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)