官术网_书友最值得收藏!

Row-level access rules

We can find the Record Rules option in the Technical menu, alongside Access Control List.

Record rules are defined in the ir.rule model. As usual, we need to provide a distinctive name. We also need the model they operate on and the domain filter to use for the access restriction. The domain filter uses the usual list of tuples syntax used across Odoo. We will be explaining this Domain Expression syntax in Chapter 7, Business Logic - Supporting Business Processes.

Usually, rules apply to some particular security groups. In our case, we will make it apply to the Employees group. If it applies to no security group in particular, it is considered global (the global field is automatically set to True). Global rules are different because they impose restrictions that non-global rules can't override.

To add the record rule, we should create a security/todo_access_rules.xml file with the following content:

<?xml version="1.0" encoding="utf-8"?> 
<odoo> 
  <data noupdate="1"> 
    <record id="todo_task_user_rule" model="ir.rule"> 
      <field name="name">ToDo Tasks only for owner</field> 
      <field name="model_id" ref="model_todo_task"/> 
      <field name="domain_force">
          [('create_uid','=',user.id)] 
      </field> 
      <field name="groups" eval="
      [(4,ref('base.group_user'))]"/> 
    </record> 
  </data> 
</odoo> 
Notice the noupdate="1" attribute. It means this data will not be updated in module upgrades. This will allow it to be customized later since module upgrades won't destroy user-made changes. But be aware that this will also be the case while developing, so you might want to set noupdate="0" during development until you're happy with the data file.

In the groups field, you will also find a special expression. It's a one-to-many relational field, and they have a special syntax to operate with. In this case, the (4, x) tuple indicates to append x to the records, and here x is a reference to the Employees group, identified by base.group_user. This one-to-many writing special syntax is discussed in more detail in Chapter 4, Models – Structuring the Application Data

As before, we must add the file to __manifest__.py before it can be loaded into the module:

'data': [ 
  'security/ir.model.access.csv', 
  'security/todo_access_rules.xml', 
  'views/todo_menu.xml', 
  'views/todo_view.xml',
'views/res_partner_view.xml', ],

If we did everything right, we can run the module tests and now they should pass.

主站蜘蛛池模板: 钟山县| 四子王旗| 康平县| 揭东县| 石林| 永善县| 巫山县| 高邮市| 镇康县| 金山区| 河源市| 永和县| 从化市| 和硕县| 吉安市| 嘉峪关市| 湛江市| 福海县| 磴口县| 精河县| 博野县| 余姚市| 湖州市| 博爱县| 贵阳市| 凤山市| 河北省| 利辛县| 鄢陵县| 金寨县| 天镇县| 肇州县| 忻州市| 通州区| 东莞市| 胶南市| 通化县| 兰西县| 庐江县| 航空| 阿拉尔市|