From 4334f195d24e6a821f19b7dc90f92c4893a47003 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B9=8F=E5=AE=87?= Date: Thu, 7 May 2026 16:37:59 +0800 Subject: [PATCH] =?UTF-8?q?=E8=96=AA=E8=B5=84=E7=AE=A1=E7=90=86=E6=9D=83?= =?UTF-8?q?=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app_user_admin/models/res_users.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/app_user_admin/models/res_users.py b/app_user_admin/models/res_users.py index 9518b552e..28cbee6a1 100644 --- a/app_user_admin/models/res_users.py +++ b/app_user_admin/models/res_users.py @@ -59,6 +59,22 @@ class ResUsers(models.Model): If no role is defined on the user, its groups are let untouched unless the `force` parameter is `True`. """ + # 定义受保护的群组,这些群组不会被角色自动添加或移除 + protected_group_refs = [ + 'account.group_account_manager', + 'account.group_account_invoice', + 'hr_payroll.group_hr_payroll_manager', + 'yuthon_salary.group_jtbb_user', + 'yuthon_salary.group_wg_user', + 'yuthon_salary.group_lc_user', + 'yuthon_salary.salary_rule_confirm_group', + ] + protected_group_ids = [] + for ref in protected_group_refs: + group = self.env.ref(ref, raise_if_not_found=False) + if group: + protected_group_ids.append(group.id) + role_groups = {} # We obtain all the groups associated to each role first, so that # it is faster to compare later with each user's groups. @@ -78,8 +94,15 @@ class ResUsers(models.Model): role = role_line.role_id group_ids += role_groups[role] group_ids = list(set(group_ids)) # Remove duplicates IDs - groups_to_add = list(set(group_ids) - set(user.groups_id.ids)) - groups_to_remove = list(set(user.groups_id.ids) - set(group_ids)) + + # 移除受保护的群组,不让角色管理它们 + group_ids = [gid for gid in group_ids if gid not in protected_group_ids] + + # 获取用户当前非受保护的群组 + user_non_protected_groups = [g.id for g in user.groups_id if g.id not in protected_group_ids] + + groups_to_add = list(set(group_ids) - set(user_non_protected_groups)) + groups_to_remove = list(set(user_non_protected_groups) - set(group_ids)) to_add = [(4, gr) for gr in groups_to_add] to_remove = [(3, gr) for gr in groups_to_remove] groups = to_remove + to_add