From c1a6df22067788387d9c42be00b5f31eecd59e88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B9=8F=E5=AE=87?= Date: Fri, 10 Apr 2026 11:46:43 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=92=E8=89=B2=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../models/yuthon_hr_employee.py | 37 +++++++++++++++++++ .../views/yuthon_official_views.xml | 2 +- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/yuthon_hr_employee/models/yuthon_hr_employee.py b/yuthon_hr_employee/models/yuthon_hr_employee.py index 1a0e30006..b3021fa57 100644 --- a/yuthon_hr_employee/models/yuthon_hr_employee.py +++ b/yuthon_hr_employee/models/yuthon_hr_employee.py @@ -338,6 +338,43 @@ class YuthonHrEmployee(models.Model): else: self.role_group_ids = [(5,)] + @api.constrains('user_role_id', 'user_role_ids', 'role_group_ids') + def _sync_roles_to_res_groups(self): + """ + 当员工的主角色(user_role_id)、辅助角色(user_role_ids)或角色组(role_group_ids)发生变更时, + 仅将该员工的关联用户写入对应权限组的用户列表,不修改 res.groups 的角色/角色组字段配置。 + 匹配规则: + - 员工的主角色/辅助角色 与 res.groups.user_role_ids 有交集 → 写入该组用户列表 + - 员工的角色组 与 res.groups.users_role_group_ids 有交集 → 写入该组用户列表 + """ + for employee in self: + if not employee.user_id: + continue + + # 收集该员工所有角色ID(主角色 + 辅助角色) + role_ids = [] + if employee.user_role_id: + role_ids.append(employee.user_role_id.id) + if employee.user_role_ids: + role_ids += employee.user_role_ids.ids + + # 收集该员工所有角色组ID + role_group_ids = employee.role_group_ids.ids + + # 1. 找到 res.groups.user_role_ids 与员工角色有交集的权限组,仅追加用户 + if role_ids: + groups_by_role = self.env['res.groups'].search([ + ('user_role_ids', 'in', role_ids) + ]) + groups_by_role.write({'users': [(4, employee.user_id.id)]}) + + # 2. 找到 res.groups.users_role_group_ids 与员工角色组有交集的权限组,仅追加用户 + if role_group_ids: + groups_by_role_group = self.env['res.groups'].search([ + ('users_role_group_ids', 'in', role_group_ids) + ]) + groups_by_role_group.write({'users': [(4, employee.user_id.id)]}) + def update_trade_union(self): records = self.env['hr.employee'].search([('is_membership', '=', True)]) for record in records: diff --git a/yuthon_official/views/yuthon_official_views.xml b/yuthon_official/views/yuthon_official_views.xml index 77959942c..db902eb95 100644 --- a/yuthon_official/views/yuthon_official_views.xml +++ b/yuthon_official/views/yuthon_official_views.xml @@ -91,7 +91,7 @@ - +