【odoo14】【開發側】許可權配置

老韓頭的碼字生活發表於2021-06-24

歡迎轉載,但需標註出處,謝謝!

說明: 本文面向開發人員,普通使用者可參考【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操作而言,更簡單。且具有移植性。

相關文章