xmlbuttonodoousergroupsaccess-rights

Show button based on access rights


The default of the button is invisible.

Access Right Scenario:
User: The button will show if create_uid is not equal to the partner_id
Manager: This button will always display.

What I did was:

<xpath expr="//button[@name='action_custom_approve']" position="attributes">
    <attribute name="attrs">attrs="{'visible':[('groups_ids','=','group_user'),('groups_ids','=','group_manager')]}"</attribute>
</xpath>

Any idea on how to achieve it in another way?


Solution

  • Note that create_uid stores who created the record, Many2one to a res.users. You have to provide only the attrs value (do not include attrs)

    Unfortunately, you can't use partner_id in attrs domain, but you can hide the button based on a computed boolean field that implement the above logic.

    For example, if we define a boolean field like following:

    button_state = fields.Boolean(compute="_compute_button_state")
    
    @api.depends('create_uid', 'partner_id')
    def _compute_button_state(self):
        for record in self:
            record.button_state = (record.partner_id != record.create_uid and self.env.user.has_group("group_user")) or self.env.user.has_group("group_manager")
    

    To hide the button based on that field, we use the following syntax:

    <xpath expr="//button[@name='action_custom_approve']" position="attributes">
        <attribute name="attrs">{'invisible': [('button_state', '=', False)]}</attribute>
    </xpath>
    
    

    In the example above, partner_id is supposed to be a Many2one to a res.users