You may be very familiar with the Odoo parent-child relationship. In Odoo we are using this relationship in almost all models like product categories and many more places. In an organization, there always be a structure. And this structure will be in a hierarchical model. There will be an owner for the company, under him there will be different departments, and for each department, there will be different department heads. Under these department heads, there will be different managers, under these managers there will be different supervisors, under supervisors there will be many workers. This is just an example of an organizational structure. Here you can see different levels of management that handle their subordinates so that the daily work will move on without any interruption. If something happens an employee should be directly reported to his line manager. So in an organization, there will always be a manager to an employee. Odoo handles this relation in a parent-child format.
In an employee form, there will be a field to choose his line manager. And odoo will prepare an organizational chart and show it as on the right side of the employee page.
In default odoo, if an employee is logged in and clicks on the employee module he will be able to see all the employees in the organization. There will be different user groups that are available and it will deal with the edit permissions.
What would you do if you want to set something like the manager needs to see their subordinate employees only. It is a simple issue that is raising in almost all organizations. If he is a normal employee he only needs to see his data and if he is a manager then he needs to see his data as well as his subordinate’s data also. If there are different departments in a company, then it is not a good idea that an employee can see the data of all other employees.
So in this blog, we will discuss how to set the manager to see his and his subordinate’s data. If he is a normal employee and doesn't have any subordinates, then he needs to see only his data. The only thing that we need to do is to set a domain rule to all the employees in XML.
Let us have a look at how we can do that.
<record id="restrict_employee" model="ir.rule">
<field name="name">Restrict Employee</field>
<field ref="hr.model_hr_employee" name="model_id"/>
<field name="domain_force"> ['|',('user_id','=',user.id),('parent_id.user_id.id', '=', user.id)] </field>
<field name="groups" eval="[(4, ref('base.group_user'))]"/>
</record>
The main point of the rule is the domain. We are setting the domain to the user group. Here the user group is the base user group(base.group_user) where a normal employee will come under this group.
The other point that we need to note is that if we add a rule like this will get affected to all groups. If we add a rule to a user group it will be automatically affected to the child user groups which are the hr user and the manager(hr.group_hr_user, hr.group_hr_manager). The result is that the HR user and the manager will only see their and their child details instead of seeing details of all employees. To avoid this we need to add another rule for the hr user.
<record id="remove_restriction" model="ir.rule">
<field name="name">Remove Restriction</field>
<field ref="hr.model_hr_employee" name="model_id"/>
<field name="domain_force">[]</field>
<field name="groups" eval="[(4, ref('base.group_hr_user))]"/>
</record>
As we added the group rule to the HR user, it will be automatically applied to the HR manager.
In Odoo we also have the provision to manage the employee by departments. We can create different departments, select departments for employees and can also set department managers. If you want to add a rule like a department manager who also wants to see all the employees under his department then we need to write the below rule.
<record id="employee_restriction_department" model="ir.rule">
<field name="name">Employee Restriction Department Manager</field>
<field ref="hr.model_hr_employee" name="model_id"/>
<field name="domain_force"> ['|',('user_id','=',user.id),('department_id.manager_id.user_id.id', '=', user.id)] </field>
<field name="groups" eval="[(4, ref('base.group_user'))]"/>
</record>
I hope you understand how to make the employees view only their and their subordinate in odoo.