问题
I used this xml code to add a button inside the 'Action',But i need to restrict the button to some user groups,
<record id="specialist_no_show_action" model="ir.actions.server">
<field name="name">No Show </field>
<field name="type">ir.actions.server</field>
<field name="binding_model_id" ref="second_opinion.model_consultation"/>
<field name="model_id" ref="second_opinion.model_consultation"/>
<field name="state">code</field>
<field name="code">
action = model.update_no_show()
</field>
</record>
回答1:
The ir.actions.actions
get_bindings method will try to retrieve the list of actions bound to the given model and discard unauthorized actions then read action definitions.
The method will use groups_id field to check if the user may not perform an action.
groups_id
Many2many field to the groups allowed to view/use the current report
So a groups_id field is added to ir.actions.report
to allow groups to view/use the current report
Unfortunately, the groups_id
field is not implemented in ir.actions.server
Fortunately, the get_bindings
method is implemented in the ir.actions.actions
model which is the base model for both models ir.actions.report
and ir.actions.server
, so to use the same logic in server actions just add a groups_id
field in ir.action.server
and use it from the XML definition to restrict access to some groups.
Use the groups_id
field for server action:
Inherit server action model and add the
groups_id
field:class IrActionsServer(models.Model): _inherit = 'ir.actions.server' groups_id = fields.Many2many('res.groups', 'res_groups_server_rel', 'uid', 'gid', string='Groups')
Then set the
groups_id
field value from XML, the following example will use the special commands format to add theAdministration/Access Rights
group to thegroups_id
field:<field name='groups_id' eval="[(4, ref('base.group_erp_manager'))]"/>
回答2:
I don't think it is possible to restrict its visibility in the action menu, but as a workaround you could do the following in the code
of the server action:
if not env.user.has_group('base.group_erp_manager'):
raise Warning("You do not have access to trigger this action")
Replace "base.group_erp_manager" with your user group's XML ID.
来源:https://stackoverflow.com/questions/63750535/how-to-give-user-groupsxml-in-the-model-ir-actions-server-odoo-12