第 4 章:安全性 - 简介¶
在 previous chapter 中,我们创建了第一个用于存储业务数据的表。在 Odoo 等业务应用程序中,首先要考虑的问题之一是谁[#who]_可以访问数据。 Odoo 提供了一种安全机制,允许特定用户组访问数据。
限制对数据的访问 中更详细地介绍了安全主题。本章旨在涵盖新模块所需的最低要求。
数据文件 (CSV)¶
Odoo 是一个高度数据驱动的系统。尽管行为是使用 Python 代码自定义的,但模块的部分值位于其加载时设置的数据中。加载数据的一种方法是通过 CSV 文件。一个示例是 list of country states,它是在安装 base 模块时加载的。
"id","country_id:id","name","code"
state_au_1,au,"Australian Capital Territory","ACT"
state_au_2,au,"New South Wales","NSW"
state_au_3,au,"Northern Territory","NT"
state_au_4,au,"Queensland","QLD"
...
id是一个 external identifier。它可用于引用记录(无需知道其数据库内标识符)。country_id:id使用 external identifier 指代国家/地区。name是州名。code是州代码。
这三个字段是“res.country.state”模型中的`defined <https://github.com/odoo/odoo/blob/2ad2f3d6567b6266fc42c6d2999d11f3066b282c/odoo/addons/base/models/res_country.py#L108-L111>`__。
按照惯例,导入数据的文件位于基本模块 <https://github.com/odoo/odoo/blob/e8697f609372cd61b045c4ee2c7f0fcfb496f58a/odoo/addons/base/__manifest__.py#L29>`__ 清单中的“data` folder of a module. When the data is related to security, it is located in the security folder. When the data is related to views and actions (we will cover this later), it is located in the views folder. Additionally, all of these files must be declared in the data list within the __manifest__.py file. Our example file is defined `”中。
另请注意,数据文件的内容仅在安装或更新模块时加载。
警告
数据文件按照“__manifest__.py` file. This means that if data A refers to data B, you must make sure that B is loaded before ``A`”中的顺序依次加载。
对于国家/地区,您会注意到 list of countries 在 list of country states 之前 加载。这是因为州指的是国家。
为什么这一切对于安全如此重要?因为模型的所有安全配置都是通过数据文件加载的,我们将在下一节中看到。
访问权¶
参考:与此主题相关的文档可以在 访问权 中找到。
注解
目标:在本节末尾,不应再出现以下警告:
WARNING rd-demo odoo.modules.loading: The models ['estate.property'] have no access rules...
当模型上没有定义访问权限时,Odoo 确定没有用户可以访问数据。甚至在日志中也有通知:
WARNING rd-demo odoo.modules.loading: The models ['estate.property'] have no access rules in module estate, consider adding some, like:
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
访问权限定义为模型“ir.model.access`. Each access right is associated with a model, a group (or no group for global access) and a set of permissions: create, read, write and unlink2. Such access rights are usually defined in a CSV file named ``ir.model.access.csv`”的记录。
这是我们之前的 test_model 的示例:
id,name,model_id/id,group_id/id,perm_read,perm_write,perm_create,perm_unlink
access_test_model,access_test_model,model_test_model,base.group_user,1,0,0,0
id是一个 external identifier。nameis the name of their.model.access。model_id/idrefers to the model which the access right applies to. The standard way to refer to the model ismodel_<model_name>, where<model_name>is the_nameof the model with the.replaced by_。看起来很麻烦?确实是…group_id/id指访问权限适用的组。perm_read,perm_write,perm_create,perm_unlink:读、写、创建和取消链接权限
Exercise
添加访问权限。
创建“ir.model.access.csv` file in the appropriate folder and define it in the ``__manifest__.py`”文件。
向组“base.group_user”授予读取、写入、创建和取消链接权限。
提示:日志中的警告消息为您提供了大部分解决方案;-)
重新启动服务器,警告消息应该就消失了!
现在终于 interact with the UI 了!