【odoo14】【開發側】權限配置
- 2021 年 6 月 24 日
- 筆記
歡迎轉載,但需標註出處,謝謝!
說明: 本文面向開發人員,普通用戶可參考【odoo14】【用戶側】權限配置。文章結構與用戶側一致。
一、 odoo中的對象
菜單、視圖、訪問權限(對應 模型)、記錄規則(對應 模型記錄)
二、 權限控制
總的來說,odoo中的權限控制顆粒度還是非常細的。最小可以到模型中的某個具體的字段,以及在odoo系統中的每一條記錄。
2.1 實現原理
以上提到的所有的對象,都是以權限組為最小單位進行控制的。有點類似於庫存中商品與變體的感覺。
2.2 代碼方式實現權限控制
以下內容以account
模塊為例
- 新建權限組所屬類型,可添加到現有類別。一般情況是一個模塊一個類別做,該模塊所屬的權限組屬於該模塊的類別中。
<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>
- 新建權限組
<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字段在初始化的時候遵循一對多、多對多的數據更新策略。
- 我們在新建菜單的時候,可將該菜單配置為特定組可見。
<menuitem id="menu_board_journal_1" name="Dashboard" action="open_account_journal_dashboard_kanban" groups="account.group_account_readonly" sequence="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>
- 訪問權限,對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>
- 模型字段的控制
invoice_payments_widget = fields.Text(groups="account.group_account_invoice,account.group_account_readonly",
compute='_compute_payments_widget_reconciled_info')
綜上,其實在實際使用中,通過代碼層面去實現權限的控制相對於UI操作而言,更簡單。且具有移植性。