Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[15.0][FIX] change role_line_ids default value to override create #305

Open
wants to merge 1 commit into
base: 15.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions base_user_role/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
comodel_name="res.users.role.line",
inverse_name="user_id",
string="Role lines",
default=lambda self: self._default_role_lines(),
groups="base.group_erp_manager",
)
role_ids = fields.One2many(
Expand All @@ -21,21 +20,27 @@
groups="base.group_erp_manager",
)

@api.model
def _default_role_lines(self):
def _set_roles_from_default_user(self, record):
default_user = self.env.ref("base.default_user", raise_if_not_found=False)
default_values = []
if default_user:
for role_line in default_user.with_context(active_test=False).role_line_ids:
default_values.append(
role_line_model = self.env["res.users.role.line"]
if not default_user:
return
role_lines = default_user.with_context(active_test=False).role_line_ids
for user in record:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this is the most correct way todo it. But it does the job

for role_line in role_lines:
if self.env["res.users.role.line"].search_count(
[("role_id", "=", role_line.role_id.id), ("user_id", "=", user.id)]
):
continue

Check warning on line 34 in base_user_role/models/user.py

View check run for this annotation

Codecov / codecov/patch

base_user_role/models/user.py#L34

Added line #L34 was not covered by tests
role_line_model.create(
{
"role_id": role_line.role_id.id,
"date_from": role_line.date_from,
"date_to": role_line.date_to,
"is_enabled": role_line.is_enabled,
"user_id": user.id,
}
)
return default_values

@api.depends("role_line_ids.role_id")
def _compute_role_ids(self):
Expand All @@ -45,6 +50,7 @@
@api.model
def create(self, vals):
new_record = super(ResUsers, self).create(vals)
self._set_roles_from_default_user(new_record)
new_record.set_groups_from_roles()
return new_record

Expand Down
17 changes: 17 additions & 0 deletions base_user_role/tests/test_user_role.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,23 @@ def test_default_user_roles(self):
roles = self.role_model.browse([self.role1_id.id, self.role2_id.id])
self.assertEqual(user.role_ids, roles)

def test_no_default_user(self):
self.default_user.unlink()
user = self.user_model.create(
{"name": "USER TEST (DEFAULT ROLES)", "login": "user_test_default_roles"}
)
self.assertFalse(user.role_ids)

def test_already_assigned_role(self):
user = self.user_model.create(
{
"name": "USER TEST (DEFAULT ROLES)",
"login": "user_test_default_roles",
"role_line_ids": [(0, 0, {"role_id": self.role1_id.id})],
}
)
self.assertEqual(len(user.role_ids), 1)

def test_role_multicompany(self):
"""Test AccessError when admin-like user accesses a role"""
role = self.multicompany_role.with_user(self.multicompany_user_1)
Expand Down
Loading