Skip to content

Commit a8b36d3

Browse files
[FIX] hr: fix access error on language change
Backport of odoo#81474 Before odoo#86889 a regular user with employees in multiple companies was not able to change his own language due to a chain of event calling onchange on all the employee_ids and employee_ids on res.users being read as sudo. The fix does work but was wrong because it gave access to the user's public employee regardless of the active company_id A domain was added to employee_ids to make force the security rules even in sudo.
1 parent ced2be6 commit a8b36d3

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

addons/hr/models/res_users.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@
88
class User(models.Model):
99
_inherit = ['res.users']
1010

11+
def _employee_ids_domain(self):
12+
# employee_ids is considered a safe field and as such will be fetched as sudo.
13+
# So try to enforce the security rules on the field to make sure we do not load employees outside of active companies
14+
return [('company_id', 'in', self.env.company.ids + self.env.context.get('allowed_company_ids', []))]
15+
1116
# note: a user can only be linked to one employee per company (see sql constraint in ´hr.employee´)
12-
employee_ids = fields.One2many('hr.employee', 'user_id', string='Related employee')
17+
employee_ids = fields.One2many('hr.employee', 'user_id', string='Related employee', domain=_employee_ids_domain)
1318
employee_id = fields.Many2one('hr.employee', string="Company employee",
1419
compute='_compute_company_employee', search='_search_company_employee', store=False)
1520

addons/hr/security/hr_security.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<record id="hr_employee_public_comp_rule" model="ir.rule">
4242
<field name="name">Employee multi company rule</field>
4343
<field name="model_id" ref="model_hr_employee_public"/>
44-
<field name="domain_force">['|','|',('user_id', '=', user.id),('company_id', '=',False),('company_id', 'in', company_ids)]</field>
44+
<field name="domain_force">['|',('company_id', '=',False),('company_id', 'in', company_ids)]</field>
4545
</record>
4646

4747
<record id="hr_job_comp_rule" model="ir.rule">

0 commit comments

Comments
 (0)