Odoo的权限

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_usergroup_test_manager属于module_category_test分组,并且只能选择其中一个。


ir.model.access.csv 文件:

示例:

    
    
idnamemodel_id:idgroup_id:idperm_readperm_writeperm_createperm_unlink
access_xxxxxxxxmodel_website_menubase.group_website_designer1111

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>



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值