Skip to content

Commit 111c4aa

Browse files
author
Milan Topuzov
committed
[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 4556476 commit 111c4aa

File tree

6 files changed

+29
-32
lines changed

6 files changed

+29
-32
lines changed

base_user_role_company/controllers/main.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88

99
class HomeExtended(Home):
1010
@http.route()
11-
def web_load_menus(self, unique):
12-
response = super().web_load_menus(unique)
13-
# On logout & re-login we could see wrong menus being rendered
14-
# To avoid this, menu http cache must be disabled
15-
response.headers.remove("Cache-Control")
11+
def web_load_menus(self, lang=None):
12+
# v19 signature: (lang=None). Keep behavior of disabling menu HTTP cache.
13+
response = super().web_load_menus(lang=lang)
14+
try:
15+
response.headers.pop("Cache-Control", None)
16+
except Exception:
17+
# ignore header manipulation issues
18+
pass
1619
return response

base_user_role_company/models/role.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,12 @@ def _check_company(self):
2525
and record.company_id != record.user_id.company_id
2626
and record.company_id not in record.user_id.company_ids
2727
):
28-
raise ValidationError(
29-
self.env._(
30-
'User "%(user)s" does not have access to the company '
31-
'"%(company)s"'
32-
)
33-
% {"user": record.user_id.name, "company": record.company_id.name}
34-
)
28+
msg = self.env._("User does not have access to the selected company")
29+
raise ValidationError(msg)
3530

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-
]
31+
# Override parent unique constraint to allow the same role multiple times
32+
# for a user, provided company_id differs.
33+
_user_role_uniq = models.Constraint(
34+
"UNIQUE (user_id, role_id, company_id)",
35+
"Roles can be assigned to a user only once at a time",
36+
)

base_user_role_company/models/user.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
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, credential, user_agent_env):
11+
# v19 signature: instance method with (credential, user_agent_env)
12+
auth_info = super().authenticate(credential, user_agent_env)
13+
# Ensure proper roles are applied for the logged-in user
14+
uid = auth_info.get("uid") if isinstance(auth_info, dict) else None
15+
if uid:
16+
env = api.Environment(self.env.cr, uid, {})
1817
if env.user.role_line_ids:
1918
env.user.set_groups_from_roles()
2019
return auth_info

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)

test-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
odoo-addon-base_user_role @ git+https://github.com/OCA/server-backend.git@refs/pull/382/head#subdirectory=base_user_role

0 commit comments

Comments
 (0)