Skip to content

Commit

Permalink
[14.0] base_user_role: warning when adding admin to role
Browse files Browse the repository at this point in the history
When creating a new user role, it is possible to add a user to that role without having added any groups.
If this is saved, all that user’s access rights will be wiped and he will not able to login anymore (Internal Server Error).
  • Loading branch information
ajaniszewska-dev committed Sep 6, 2024
1 parent 41b41f5 commit 1921155
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
13 changes: 13 additions & 0 deletions base_user_role/models/role.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,16 @@ def unlink(self):
res = super(ResUsersRoleLine, self).unlink()
users.set_groups_from_roles(force=True)
return res

@api.onchange("user_id")
def _onchange_user_id(self):
if self.user_id and self.user_id._is_admin():
return {

Check warning on line 182 in base_user_role/models/role.py

View check run for this annotation

Codecov / codecov/patch

base_user_role/models/role.py#L182

Added line #L182 was not covered by tests
"warning": {
"title": _("Warning"),
"message": _(
"""When adding a role to an administrator,
make sure no essential groups will be removed."""
),
}
}
15 changes: 14 additions & 1 deletion base_user_role/models/user.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright 2014 ABF OSIELL <http://osiell.com>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
from odoo import api, fields, models
from odoo import _, api, fields, models


class ResUsers(models.Model):
Expand All @@ -19,6 +19,19 @@ class ResUsers(models.Model):
compute_sudo=True,
)

@api.onchange("role_line_ids")
def _onchange_role_line_ids(self):
if self._is_admin():
return {

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

View check run for this annotation

Codecov / codecov/patch

base_user_role/models/user.py#L25

Added line #L25 was not covered by tests
"warning": {
"title": _("Warning"),
"message": _(
"""When adding a role to an administrator,
make sure no essential groups will be removed."""
),
}
}

@api.model
def _default_role_lines(self):
default_user = self.env.ref("base.default_user", raise_if_not_found=False)
Expand Down

0 comments on commit 1921155

Please sign in to comment.