Skip to content

Commit f2b1370

Browse files
committed
[UPD] Prevents deletion of roles in use
- Raises an error when attempting to delete a role that is still assigned to users. - Provides a user-friendly error message indicating the roles in use, improving data integrity and preventing accidental data loss.
1 parent f3f8ecf commit f2b1370

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

base_user_role/i18n/de.po

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,18 @@ msgstr "Ansichten"
436436
msgid "Wizard to add multiple users to a role"
437437
msgstr ""
438438

439+
#. module: base_user_role
440+
#. odoo-python
441+
#: code:addons/base_user_role/models/role.py:0
442+
msgid ""
443+
"You cannot delete a role that is still assigned to one or "
444+
"more users. Please remove the users from the role(s) "
445+
"before deleting. Roles in use: %s"
446+
msgstr ""
447+
"Sie können eine Rolle nicht löschen, die noch einem oder mehreren Benutzern "
448+
"zugewiesen ist. Bitte entfernen Sie die Benutzer aus der/den Rolle(n), "
449+
"bevor Sie löschen. Rollen in Verwendung: %s"
450+
439451
#. module: base_user_role
440452
#: model_terms:ir.ui.view,arch_db:base_user_role.create_from_user_wizard_view
441453
msgid "or"

base_user_role/models/role.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import logging
55

66
from odoo import SUPERUSER_ID, _, api, fields, models
7+
from odoo.exceptions import UserError
78

89
_logger = logging.getLogger(__name__)
910

@@ -98,6 +99,21 @@ def write(self, vals):
9899
return res
99100

100101
def unlink(self):
102+
# Check if any of the roles being deleted are still assigned to users
103+
role_lines = self.env["res.users.role.line"].search(
104+
[("role_id", "in", self.ids)]
105+
)
106+
if role_lines:
107+
# Get role names for better error message
108+
role_names = ", ".join(role_lines.mapped("role_id.name"))
109+
raise UserError(
110+
_(
111+
"You cannot delete a role that is still assigned to one or "
112+
"more users. Please remove the users from the role(s) "
113+
"before deleting. Roles in use: %s"
114+
)
115+
% role_names
116+
)
101117
users = self.mapped("user_ids")
102118
res = super().unlink()
103119
users.set_groups_from_roles(force=True)

base_user_role/tests/test_user_role.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import datetime
44

55
from odoo import fields
6-
from odoo.exceptions import AccessError
6+
from odoo.exceptions import AccessError, UserError
77
from odoo.tests import tagged
88
from odoo.tests.common import TransactionCase
99

@@ -154,12 +154,20 @@ def test_role_unlink(self):
154154
# Check user has groups from role1 and role2
155155
self.assertLessEqual(role1_groups, self.user_id.groups_id)
156156
self.assertLessEqual(role2_groups, self.user_id.groups_id)
157-
# Remove role2
157+
# Remove role2 from user before unlinking
158+
self.user_id.role_line_ids.filtered(
159+
lambda rl: rl.role_id.id == self.role2_id.id
160+
).unlink()
161+
# Now unlink role2
158162
self.role2_id.unlink()
159163
# Check user has groups from only role1
160164
self.assertLessEqual(role1_groups, self.user_id.groups_id)
161165
self.assertFalse(role2_groups <= self.user_id.groups_id)
162-
# Remove role1
166+
# Remove role1 from user before unlinking
167+
self.user_id.role_line_ids.filtered(
168+
lambda rl: rl.role_id.id == self.role1_id.id
169+
).unlink()
170+
# Now unlink role1
163171
self.role1_id.unlink()
164172
# Check user has no groups from role1 and role2
165173
self.assertFalse(role1_groups <= self.user_id.groups_id)
@@ -197,6 +205,18 @@ def test_role_line_unlink(self):
197205
self.assertFalse(role1_groups <= self.user_id.groups_id)
198206
self.assertFalse(role2_groups <= self.user_id.groups_id)
199207

208+
def test_role_unlink_with_users_assigned(self):
209+
"""Test that deleting a role with assigned users raises UserError."""
210+
# Assign role1 to a user
211+
self.user_id.write({"role_line_ids": [(0, 0, {"role_id": self.role1_id.id})]})
212+
213+
# Try to delete the role - should raise UserError
214+
with self.assertRaisesRegex(
215+
UserError,
216+
"You cannot delete a role that is still assigned to one or " "more users",
217+
):
218+
self.role1_id.unlink()
219+
200220
def test_default_user_roles(self):
201221
self.default_user.write(
202222
{

0 commit comments

Comments
 (0)