@@ -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
|
||||
|
||||
Reference in New Issue
Block a user