Skip to content

Commit

Permalink
[FIX] hr: always retrieve valid employee
Browse files Browse the repository at this point in the history
if you are a multi company user and you are employee
in another company than the current one you will
end up not be able to use your department as a filter
even if the department is defined for multiple companies

closes odoo#129665

X-original-commit: 8a30dbb
Signed-off-by: Sofie Gvaladze (sgv) <sgv@odoo.com>
Signed-off-by: Wolfgang Taferner <w.taferner@wtioit.at>
  • Loading branch information
wtaferner committed Jul 25, 2023
1 parent 4e344d0 commit d93efc5
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions addons/hr/models/hr_employee_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,25 @@ class HrEmployeeBase(models.AbstractModel):
('presence_undetermined', 'Undetermined')], compute='_compute_presence_icon')
show_hr_icon_display = fields.Boolean(compute='_compute_presence_icon')


def _get_valid_employee_for_user(self):
user = self.env.user
# retrieve the employee of the current active company for the user
employee = user.employee_id
if not employee:
# search for all employees as superadmin to not get blocked by multi-company rules
user_employees = user.employee_id.sudo().search([
('user_id', '=', user.id)
])
# the default company employee is most likely the correct one, but fallback to the first if not available
employee = user_employees.filtered(lambda r: r.company_id == user.company_id) or user_employees[:1]
return employee

@api.depends_context('uid', 'company')
@api.depends('department_id')
def _compute_part_of_department(self):
active_department = self.env.user.employee_id.department_id
user_employee = self._get_valid_employee_for_user()
active_department = user_employee.department_id
if not active_department:
self.member_of_department = False
else:
Expand All @@ -79,12 +94,14 @@ def get_all_children(department):
def _search_part_of_department(self, operator, value):
if operator not in ('=', '!=') or not isinstance(value, bool):
raise UserError(_('Operation not supported'))

user_employee = self._get_valid_employee_for_user()
# Double negation
if not value:
operator = '!=' if operator == '=' else '='
if not self.env.user.employee_id.department_id:
return [('id', operator, self.env.user.employee_id.id)]
return (['!'] if operator == '!=' else []) + [('department_id', 'child_of', self.env.user.employee_id.department_id.id)]
if not user_employee.department_id:
return [('id', operator, user_employee.id)]
return (['!'] if operator == '!=' else []) + [('department_id', 'child_of', user_employee.department_id.id)]

@api.depends('user_id.im_status')
def _compute_presence_state(self):
Expand Down

0 comments on commit d93efc5

Please sign in to comment.