Odoo的权限的核心是权限组(res_groups)。对每个权限组,可以设置权限组的菜单表示,对象表示,记录规则表示,字段表示。
1.菜单/对象级别
设置哪些人可以访问哪些菜单/对象,对象的访问权限包括创建、读、写、删除。
2.记录级别
设置哪些人可以访问哪些记录,也就是设置表的查询条件。
3.字段级别
设置表中的字段的访问权限。
4.工作流级别
在工作流的每一步迁移中,设置哪些角色允许触发本迁移
菜单/对象级别:
模块下 security 目录有两个文件:xxx_security.xml、ir.model.access.csv。 其中: xxx_security.xml文件定义组和组对菜单的访问权限, ir.model.access.csv定义组对对象的权限矩阵。
x_security.xml文件:
<data noupdate="0"> <record model="ir.module.category" id="module_category_test"> <field name="name">测试</field> </record> <record model="res.groups" id="group_test_user"> <field name="name">测试用户</field> <field name="category_id" ref="module_category_test"/> </record> <record model="res.groups" id="group_test_manager"> <field name="name">测试管理</field> <field name="implied_ids" eval="[(4, ref('group_test_user'))]"/> <field name="category_id" ref="module_category_test"/> </record> </data>
Noupdate 表示,当模块升级时是否更新本条数据。
对于demo 数据,通常设置成noupdate=”1”,即不更新,不指定noupdate 的话,默认值是noupdate=”0”。
category_id表示:
group_test_user和group_test_manager属于module_category_test分组,并且只能选择其中一个。
ir.model.access.csv 文件:
示例:
id name model_id:id group_id:id perm_read perm_write perm_create perm_unlink
access_xxx xxxxx model_website_menu base.group_website_designer 1 1 1 1
model_id:id 对应的对象模型,
写法示例:website.model_website_config_settings
如果内容本身在website模块中则可以省略website.
后面则为模型的name将”.”替换成”-“的结果,在前面加model_
group_id:id 哪个组
perm_read、perm_write、perm_create、perm_unlink 增删改查权限。1 有权限 0 无权限
记录规则表示:
记录规则是一个记录模型”ir.rule”,关联到一个模型。
<record id="stock_picking_multi_company_rule" model="ir.rule"> <field name="name">stock.picking multi-company</field> <field name="model_id" ref="stock.model_stock_picking"/> <field name="global" eval="True"/> <field name="active" eval="False"/> <field name="domain_force">['|', '|', ('company_id', '=', False), ('company_id','child_of',[user.company_id.id]),('company_id','in',[c.id for c in user.company_ids])]</field> </record> <record id="product_template_public" model="ir.rule"> <field name="name">Public product template</field> <field name="model_id" ref="product.model_product_template"/> <field name="domain_force">[('website_published', '=', True), ("sale_ok", "=", True)]</field> <field name="groups" eval="[(4, ref('base.group_public')), (4, ref('base.group_portal'))]"/> <field name="perm_read" eval="True"/> <field name="perm_write" eval="False"/> <field name="perm_create" eval="False"/> <field name="perm_unlink" eval="False"/> </record> Domain_force表示自定义Domain 表达式,用于过滤条件,含义是只能访问符合本过滤条件的记录。这里过滤条件是操作人必须是当前用户。
字段表示 :
字段表示权限可以指定权限组能访问记录里的哪个字段,可以在视图和对象上指定字段访问权限。
在视图上指定字段访问权限:
<field name="arch" type="xml"> <form string="Scheduled Products"> <group col="4"> <field name="name"/> <field name="product_id"/> <field name="product_qty"/> <field name="product_uom" groups="product.group_uom"/> <field name="product_uos_qty" groups="product.group_uos"/> <field name="product_uos" groups="product.group_uos"/> </group> </form> </field> 在字段对象定义时指定字段访问权限: _columns = { "gengo_private_key": fields.text("Gengo Private Key", copy=False, groups="base.group_system"), "gengo_public_key": fields.text("Gengo Public Key", copy=False, groups="base.group_user"), "gengo_comment": fields.text("Comments", help="This comment will be automatically be enclosed in each an every request sent to Gengo", groups="base.group_user"), "gengo_auto_approve": fields.boolean("Auto Approve Translation ?", help="Jobs are Automatically Approved by Gengo.", groups="base.group_user"), "gengo_sandbox": fields.boolean("Sandbox Mode", help="Check this box if you're using the sandbox mode of Gengo, mainly used for testing purpose."), }
隐藏的常用技巧:
* 直接隐藏 <group name="owner" position="attributes"> <attribute name="invisible">True</attribute> </group> * 满足某些条件的隐藏 <xpath expr="//field[@name='parent_id']" position='attributes'> <attribute name="attrs">{'invisible': [('passenger','=', True)]}</attribute> </xpath> <group col="4" string='旅客信息' attrs="{'invisible': [('supplier','=', True)]}"></group> * 通过组来隐藏 <xpath expr="//field[@name='type']" position="attributes"> <attribute name="groups">base.group_no_one</attribute> </xpath> * 菜单的隐藏 <record model="ir.ui.menu" id="crm.menu_crm_opportunities"> <field eval="[(6,0, [ref('base.group_no_one'),])]" name="groups_id"/> </record>