Hello,
Unfortunately, in Odoo 17 Community Edition, you cannot fully implement that logic (check if user is admin or viewing their own employee record) using XML alone.
Why Not?
-
XML views (attrs, groups, etc.) can only react to field values, not to dynamic logic like comparing current user to a record's user_id.
-
There’s no built-in XML expression like “if current user is this employee.”
For Example:
XML views don’t allow direct evaluation of expressions like: id == user_id.employee_id.id
because:
-
XML attrs only checks field values in the current record.
-
It doesn't know who the current user is unless you expose that data to the view (via a computed field).
Correct Way Using Minimal Python (with Your Insight!)
Step 1: Minimal Python Code
from odoo import models, fields
class Employee(models.Model):
_inherit = 'hr.employee'
is_self_or_admin = fields.Boolean(compute='_compute_is_self_or_admin')
def _compute_is_self_or_admin(self):
user = self.env.user
for record in self:
record.is_self_or_admin = (
record.user_id.id == user.id or user.has_group('base.group_system')
)
Step 2: Use it in XML
<odoo>
<record id="view_employee_form_inherit_hide_private_info" model="ir.ui.view">
<field name="name">hr.employee.form.hide.private.info</field>
<field name="model">hr.employee</field>
<field name="inherit_id" ref="hr.view_employee_form"/>
<field name="arch" type="xml">
<!-- Use xpath to locate the page by name -->
<xpath expr="//page[@name='personal_information']" position="attributes">
<attribute name="attrs">{'invisible': [('is_self_or_admin', '=', False)]}</attribute>
</xpath>
</field>
</record>
</odoo>
Now the tab only shows if:
-
The logged-in user is the employee linked to the record, or
-
The user is an Admin
Thanks & Regards,
Saiyed Mahedi Abbas
Odoo Developer
Email:- saiyedmehdiabbas8@gmail.com