【odoo14】【开发侧】权限配置

欢迎转载,但需标注出处,谢谢!

说明: 本文面向开发人员,普通用户可参考【odoo14】【用户侧】权限配置。文章结构与用户侧一致。

一、 odoo中的对象

菜单、视图、访问权限(对应 模型)、记录规则(对应 模型记录)

二、 权限控制

总的来说,odoo中的权限控制颗粒度还是非常细的。最小可以到模型中的某个具体的字段,以及在odoo系统中的每一条记录。

2.1 实现原理

以上提到的所有的对象,都是以权限组为最小单位进行控制的。有点类似于库存中商品与变体的感觉。

2.2 代码方式实现权限控制

以下内容以account模块为例

  1. 新建权限组所属类型,可添加到现有类别。一般情况是一个模块一个类别做,该模块所属的权限组属于该模块的类别中。
<record model="ir.module.category" id="base.module_category_accounting_accounting">
	<field name="description">Helps you handle your accounting needs, if you are not an accountant, we suggest you to install only the Invoicing.</field>
	<field name="sequence">7</field>
</record>
  1. 新建权限组
<record id="group_show_line_subtotals_tax_included" model="res.groups">
	<field name="name">Tax display B2C</field>
	<field name="comment">Show line subtotals with taxes included (B2C)</field>
	<field name="category_id" ref="base.module_category_hidden"/>
</record>

权限组中设计的核心字段介绍

  • category_id:当前权限组所属的类别
  • name:权限组名称
  • implied_ids:继承的其他群组,数据当前群组的用户将添加为所继承群组的用户
  • users:属于当前群组的用户

说明
implied_ids及users字段在初始化的时候遵循一对多、多对多的数据更新策略。

  1. 我们在新建菜单的时候,可将该菜单配置为特定组可见。
<menuitem id="menu_board_journal_1" name="Dashboard" action="open_account_journal_dashboard_kanban" groups="account.group_account_readonly" sequence="1"/>
  1. 视图,对groups添加初始值
<record id="analytic_rule_action_user" model="ir.actions.act_window">
	<field name="name">Analytic Rules</field>
	<field name="res_model">account.analytic.default</field>
	<field name="context">{'search_default_user_id': [active_id], 'default_user_id': active_id}</field>
	<field name="binding_model_id" ref="base.model_res_users"/>
	<field name="binding_view_types">form</field>
	<field name="groups_id" eval="[(4, ref('analytic.group_analytic_accounting'))]"/>
</record>
  1. 访问权限,对groups添加初始值
 <record id="account_move_rule_group_readonly" model="ir.rule">
	<field name="name">Readonly Move</field>
	<field name="model_id" ref="model_account_move"/>
	<field name="domain_force">[(1, '=', 1)]</field>
	<field name="groups" eval="[(4, ref('account.group_account_readonly'))]"/>
	<field name="perm_write" eval="False"/>
	<field name="perm_create" eval="False"/>
	<field name="perm_unlink" eval="False"/>
</record>
  1. 模型字段的控制
invoice_payments_widget = fields.Text(groups="account.group_account_invoice,account.group_account_readonly",
        compute='_compute_payments_widget_reconciled_info')

综上,其实在实际使用中,通过代码层面去实现权限的控制相对于UI操作而言,更简单。且具有移植性。

posted @ 2021-06-24 07:49  老韩头的开发日常  阅读(681)  评论(0编辑  收藏  举报