Search code examples
pythonxmlodooodoo-13

Odoo 13: Check if the user has "delete access"


I have this problem where I need to check if the user has the delete access enabled then change the value of the boolean variable to hide a specific field. Every time I run the code, it does not change the value of the boolean variable. How do I implement the check_access_rule as a condition?

View (xml):

<?xml version="1.0" encoding="UTF-8"?>
<odoo>
  <record id="hr_some_id" model="ir.ui.view">
      <field name="name">hr.model.form.inherit</field>
      <field name="model">hr.model_name</field>
      <field name="inherit_id" ref="hr_model_view_form_inherit"/>
      <field name="arch" type="xml">
          <data>
              <xpath expr="//notebook/page[@name='page']/div[1]" position="before">
                  <group string="Some Info">
                    <field name="some_ids" readonly="True" nolabel="1">
                      <tree default_order="create_date desc">
                        <field name="has_delete_access" invisible="1"/>
                        <button name="unlink" type="object" string="Delete" class="oe_stat_button" icon="fa-times" groups="base.group_user" attrs="{'invisible':[('has_delete_access','=',False)]}"/>
                      </tree>
                    </field>
                  </group>
              </xpath>
          </data>
      </field>
  </record>
</odoo>

Python:

class ModelName(models.Model):
    _name = 'hr.model_name'
    _description = 'Some Info'

    has_delete_access = fields.Boolean(compute="_compute_check_access_history", string='Has Delete Access?', default=False)

    def _compute_check_access_history(self):
        if self.env.user.check_access_rule('perm_unlink'):
            self.has_delete_access = False
        else:
            self.has_delete_access = True

Solution

  • You can create two groups one of which has extra privilege to delete and then associate the button with the group of deletion privilege so if the user has this group then he will see the button, for example: in the security file create two groups:

    <record id="group_read_write_create" model="res.groups">
        <field name="name">Reading, writing, and creation permisions</field>
        <field name="implied_ids" eval="[(4, ref('base.group_user'))]"/>
    </record>
    
    <record id="group_read_write_create_unlink" model="res.groups">
        <field name="name">Reading, writing, creation, and unlinking permisions</field>
        <field name="implied_ids" eval="[(4, ref('group_read_write_create'))]"/>
    </record>
    

    Then in the .csv file define two access rights:

    access_hr_model_name_read_write_create,hr.model.name.read.write.create,model_hr_model_name,group_read_write_create,1,1,1,0
    access_hr_model_name_read_write_create_unlink,hr.model.name.read.write.create.unlink,model_hr_model_name,group_read_write_create_unlink,1,1,1,1
    

    And in the view associate the button with the group of deletion privilege:

    <xpath expr="//notebook/page[@name='page']/div[1]" position="before">
      <group string="Some Info">
        <field name="some_ids" readonly="True" nolabel="1">
          <tree default_order="create_date desc">
            <button name="unlink" type="object" string="Delete" class="oe_stat_button" icon="fa-times" groups="module_name.group_read_write_create_unlink"/>
          </tree>
        </field>
      </group>
    </xpath>