Skip to content

Commit ad9281e

Browse files
authored
Merge pull request #382 from OCA/16.0
Syncing from upstream OCA/server-backend (16.0)
2 parents b793ea1 + c756460 commit ad9281e

File tree

12 files changed

+131
-18
lines changed

12 files changed

+131
-18
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ addon | version | maintainers | summary
3131
[base_import_match](base_import_match/) | 16.0.1.0.0 | | Try to avoid duplicates before importing
3232
[base_portal_type](base_portal_type/) | 16.0.1.0.0 | [![hbrunn](https://github.com/hbrunn.png?size=30px)](https://github.com/hbrunn) | Base module to allow different types of portals
3333
[base_user_effective_permissions](base_user_effective_permissions/) | 16.0.1.0.0 | [![hbrunn](https://github.com/hbrunn.png?size=30px)](https://github.com/hbrunn) | Inspect effective permissions applying to a user
34-
[base_user_role](base_user_role/) | 16.0.1.4.2 | [![sebalix](https://github.com/sebalix.png?size=30px)](https://github.com/sebalix) [![jcdrubay](https://github.com/jcdrubay.png?size=30px)](https://github.com/jcdrubay) [![novawish](https://github.com/novawish.png?size=30px)](https://github.com/novawish) | User roles
35-
[base_user_role_company](base_user_role_company/) | 16.0.1.2.1 | | User roles by company
34+
[base_user_role](base_user_role/) | 16.0.1.4.3 | [![sebalix](https://github.com/sebalix.png?size=30px)](https://github.com/sebalix) [![jcdrubay](https://github.com/jcdrubay.png?size=30px)](https://github.com/jcdrubay) [![novawish](https://github.com/novawish.png?size=30px)](https://github.com/novawish) | User roles
35+
[base_user_role_company](base_user_role_company/) | 16.0.1.2.2 | | User roles by company
3636
[base_user_role_history](base_user_role_history/) | 16.0.1.0.0 | [![ThomasBinsfeld](https://github.com/ThomasBinsfeld.png?size=30px)](https://github.com/ThomasBinsfeld) | This module allows to track the changes on users roles.
3737
[server_action_navigate](server_action_navigate/) | 16.0.1.0.0 | [![legalsylvain](https://github.com/legalsylvain.png?size=30px)](https://github.com/legalsylvain) [![ashishhirpara](https://github.com/ashishhirpara.png?size=30px)](https://github.com/ashishhirpara) | Navigate between any items of any Odoo Models
3838
[server_action_sort](server_action_sort/) | 16.0.1.0.0 | [![legalsylvain](https://github.com/legalsylvain.png?size=30px)](https://github.com/legalsylvain) | Sort any lines of any models by any criterias

base_user_role/README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ User roles
77
!! This file is generated by oca-gen-addon-readme !!
88
!! changes will be overwritten. !!
99
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10-
!! source digest: sha256:bedebf94569063b6e3e5d0d56361045ce2f33fb17ebe96e2e6851c41907d5eca
10+
!! source digest: sha256:aa45c0a49bc7d34f53210530575d414b89030caee34f48536323309ac28df5c7
1111
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1212
1313
.. |badge1| image:: https://img.shields.io/badge/maturity-Production%2FStable-green.png

base_user_role/__manifest__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
{
66
"name": "User roles",
7-
"version": "16.0.1.4.2",
7+
"version": "16.0.1.4.3",
88
"category": "Tools",
99
"author": "ABF OSIELL, Odoo Community Association (OCA)",
1010
"license": "LGPL-3",

base_user_role/models/user.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,21 @@ def _default_role_lines(self):
3434
default_user = self.env.ref("base.default_user", raise_if_not_found=False)
3535
default_values = []
3636
if default_user:
37-
for role_line in default_user.with_context(active_test=False).role_line_ids:
38-
default_values.append(
39-
{
40-
"role_id": role_line.role_id.id,
41-
"date_from": role_line.date_from,
42-
"date_to": role_line.date_to,
43-
"is_enabled": role_line.is_enabled,
44-
}
45-
)
37+
default_values = default_user._get_role_lines_vals_from_user()
38+
return default_values
39+
40+
def _get_role_lines_vals_from_user(self):
41+
self.ensure_one()
42+
default_values = []
43+
for role_line in self.with_context(active_test=False).role_line_ids:
44+
default_values.append(
45+
{
46+
"role_id": role_line.role_id.id,
47+
"date_from": role_line.date_from,
48+
"date_to": role_line.date_to,
49+
"is_enabled": role_line.is_enabled,
50+
}
51+
)
4652
return default_values
4753

4854
@api.depends("role_line_ids.role_id")
@@ -97,3 +103,15 @@ def set_groups_from_roles(self, force=False):
97103
vals = {"groups_id": groups}
98104
super(ResUsers, user).write(vals)
99105
return True
106+
107+
def copy(self, default=None):
108+
self.ensure_one()
109+
portal_user = self.env.ref(
110+
"base.template_portal_user_id", raise_if_not_found=False
111+
)
112+
if portal_user and self == portal_user:
113+
default["role_line_ids"] = [
114+
(0, 0, role_vals)
115+
for role_vals in portal_user._get_role_lines_vals_from_user()
116+
]
117+
return super().copy(default=default)

base_user_role/static/description/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ <h1 class="title">User roles</h1>
367367
!! This file is generated by oca-gen-addon-readme !!
368368
!! changes will be overwritten. !!
369369
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
370-
!! source digest: sha256:bedebf94569063b6e3e5d0d56361045ce2f33fb17ebe96e2e6851c41907d5eca
370+
!! source digest: sha256:aa45c0a49bc7d34f53210530575d414b89030caee34f48536323309ac28df5c7
371371
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
372372
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Production/Stable" src="https://img.shields.io/badge/maturity-Production%2FStable-green.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/licence-LGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/server-backend/tree/16.0/base_user_role"><img alt="OCA/server-backend" src="https://img.shields.io/badge/github-OCA%2Fserver--backend-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/server-backend-16-0/server-backend-16-0-base_user_role"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/server-backend&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
373373
<p>This module was written to extend the standard functionality regarding users

base_user_role/tests/test_user_role.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,35 @@ def test_group_groups_into_role(self):
274274
self.assertEqual(new_role.name, "Test Role")
275275
# Check that the role has the correct groups (even if the order is not equal)
276276
self.assertEqual(set(new_role.implied_ids.ids), set(user_group_ids))
277+
278+
def test_role_default_user(self):
279+
"""
280+
Test that portal user has the right role and groups based on the portal
281+
template.
282+
"""
283+
self.default_user.write(
284+
{
285+
"role_line_ids": [
286+
(0, 0, {"role_id": self.role1_id.id}),
287+
(0, 0, {"role_id": self.role2_id.id}),
288+
]
289+
}
290+
)
291+
292+
portal_template = self.env.ref("base.template_portal_user_id")
293+
portal_group = self.env.ref("base.group_portal")
294+
vals = {
295+
"name": "Portal Role",
296+
"implied_ids": [(6, 0, [portal_group.id])],
297+
}
298+
portal_role = self.role_model.create(vals)
299+
portal_template.write({"role_line_ids": [(0, 0, {"role_id": portal_role.id})]})
300+
portal_user = portal_template.copy(
301+
{
302+
"name": "New portal user",
303+
"active": True,
304+
}
305+
)
306+
portal_user = portal_user.with_context(active_test=False)
307+
self.assertNotIn(self.group_user_id, set(portal_user.groups_id))
308+
self.assertIn(portal_role.group_id, set(portal_user.groups_id))

base_user_role_company/README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ User roles by company
77
!! This file is generated by oca-gen-addon-readme !!
88
!! changes will be overwritten. !!
99
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10-
!! source digest: sha256:052116258bc0120ce711f5653d1175632c4da4d09e61cfe6f4029ce7e3f2d180
10+
!! source digest: sha256:ccf57421a8b2d55598cffef87eb074983438bc7f352b8b6cc86ef150638049e0
1111
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1212
1313
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png

base_user_role_company/__manifest__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
{
55
"name": "User roles by company",
6-
"version": "16.0.1.2.1",
6+
"version": "16.0.1.2.2",
77
"category": "Tools",
88
"author": "Open Source Integrators, Odoo Community Association (OCA)",
99
"license": "AGPL-3",

base_user_role_company/models/user.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def _get_enabled_roles(self):
2727
company_ids = self.env.context.get("active_company_ids")
2828
else:
2929
company_ids = self.company_id.ids
30-
for role_line in self.role_line_ids:
30+
for role_line in res:
3131
if not role_line.company_id:
3232
active_roles |= role_line
3333
elif role_line.company_id.id in company_ids:

base_user_role_company/static/description/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ <h1 class="title">User roles by company</h1>
367367
!! This file is generated by oca-gen-addon-readme !!
368368
!! changes will be overwritten. !!
369369
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
370-
!! source digest: sha256:052116258bc0120ce711f5653d1175632c4da4d09e61cfe6f4029ce7e3f2d180
370+
!! source digest: sha256:ccf57421a8b2d55598cffef87eb074983438bc7f352b8b6cc86ef150638049e0
371371
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
372372
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/server-backend/tree/16.0/base_user_role_company"><img alt="OCA/server-backend" src="https://img.shields.io/badge/github-OCA%2Fserver--backend-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/server-backend-16-0/server-backend-16-0-base_user_role_company"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/server-backend&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
373373
<p>Enable User Roles depending on the Companies selected.</p>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
from . import test_role_per_company
2+
from . import test_use_only_enabled_roles
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Copyright 2021 Open Source Integrators
2+
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
3+
from datetime import timedelta
4+
5+
from odoo import fields
6+
from odoo.tests.common import TransactionCase
7+
8+
9+
class TestUserRoleCompany(TransactionCase):
10+
@classmethod
11+
def setUpClass(cls):
12+
super().setUpClass()
13+
cls.company1 = cls.env.ref("base.main_company")
14+
# GROUPS for roles
15+
cls.groupA = cls.env.ref("base.group_user")
16+
cls.groupB = cls.env.ref("base.group_system")
17+
# ROLES
18+
cls.Role = cls.env["res.users.role"]
19+
cls.roleA = cls.Role.create({"name": "Role internal user"})
20+
cls.roleA.implied_ids |= cls.groupA
21+
cls.roleB = cls.Role.create({"name": "Role system user"})
22+
# USER with roles
23+
cls.roleB.implied_ids |= cls.groupB
24+
cls.User = cls.env["res.users"]
25+
user_vals = {
26+
"name": "Role test user",
27+
"login": "role_test_user",
28+
"company_ids": [(fields.Command.set([cls.company1.id]))],
29+
"role_line_ids": [
30+
(fields.Command.create({"role_id": cls.roleA.id})),
31+
(
32+
fields.Command.create(
33+
{
34+
"role_id": cls.roleB.id,
35+
"date_to": fields.Date.today() + timedelta(days=1),
36+
}
37+
)
38+
),
39+
],
40+
}
41+
cls.test_user = cls.User.create(user_vals)
42+
43+
def test_110_enabled_role_is_used(self):
44+
# User should be in group A and B because date_to is in the future
45+
self.test_user.with_context(
46+
active_company_ids=self.company1.ids
47+
).set_groups_from_roles()
48+
expected = self.groupA | self.groupB
49+
found = self.test_user.groups_id.filtered(lambda x: x in expected)
50+
self.assertEqual(expected, found)
51+
52+
def test_120_disabled_role_is_not_used(self):
53+
# User should not be in group B because date_to is in the past
54+
self.test_user.role_line_ids.filtered(
55+
lambda x: x.role_id == self.roleB
56+
).date_to = fields.Date.today() - timedelta(days=1)
57+
self.test_user.with_context(
58+
active_company_ids=self.company1.ids
59+
).set_groups_from_roles()
60+
expected = self.groupA
61+
found = self.test_user.groups_id.filtered(lambda x: x in expected)
62+
self.assertEqual(expected, found)

0 commit comments

Comments
 (0)