Odoo Smartbutton access rights for different model

116 Views Asked by At

I have added smartbutton in res.partner form view header that opens the current partner helpdesk tickets (helpdesk.ticket model). enter image description here

Smartbutton view (If i remove this code then button is removed and users can freely open partner form view)

<odoo>
    <data>
        <record id="helpdesk_ticket_smart_button" model="ir.ui.view">
            <field name="name">partner.helpdesk.ticket.smart.buttons</field>
            <field name="model">res.partner</field>
            <field name="inherit_id" ref="base.view_partner_form" />
            <field name="arch" type="xml">
                <div name="button_box" position="inside">
                    <button class="oe_stat_button" type="object" name="action_view_helpdesk_tickets" icon="fa-ticket">
                        <field string="Tickets" name="helpdesk_ticket_count" widget="statinfo"/>
                    </button>
                </div>
            </field>
        </record>
    </data>
</odoo>

Unfortunately now, after a non-helpdesk user tries to open any partner form he gets blocked due to Access Error. enter image description here

What can i change so that any odoo user can open partner form view without getting blocked but only helpdesk users can see the Tickets smartbutton?

Any help would be appreciated. Let me know if you need more information.

2

There are 2 best solutions below

0
CZoellner On

You can set a group on your extension view:

<field name="groups_id" eval="[(4,ref('helpdesk.group_helpdesk_user'))]"/>

Will look like this:

<odoo>
    <data>
        <record id="helpdesk_ticket_smart_button" model="ir.ui.view">
            <field name="name">partner.helpdesk.ticket.smart.buttons</field>
            <field name="model">res.partner</field>
            <field name="inherit_id" ref="base.view_partner_form" />
            <field name="groups_id" eval="[(4,ref('helpdesk.group_helpdesk_user'))]"/>
            <field name="arch" type="xml">
                <div name="button_box" position="inside">
                    <button class="oe_stat_button" type="object" name="action_view_helpdesk_tickets" icon="fa-ticket">
                        <field string="Tickets" name="helpdesk_ticket_count" widget="statinfo"/>
                    </button>
                </div>
            </field>
        </record>
    </data>
</odoo>

Odoo will only load the extension view for users in the groups that are set on the view.

1
Віктор Федорів On

While the answer provided by CZoellner should indeed work, another and more robust solution would be to add a groups attribute to your button definition:

<button class="oe_stat_button" type="object" name="action_view_helpdesk_tickets" groups="helpdesk.group_helpdesk_user">
  <field string="Tickets" name="helpdesk_ticket_count" widget="statinfo"/>
</button>

The reason this solution is more robust in my opinion is because you or some of your collegues may later want to add some more changes to this view, but those changes may have to be available for everybody.