谁能向我解释 Odoo 中的规则行为?
Can anyone explain me the rules' behaviour in Odoo?
我一如既往地受制于规则,因为我还不能理解它们。
这就是我们所说的:
Interaction between rules
Global rules (non group-specific) are restrictions, and cannot be
bypassed. Group-local rules grant additional permissions, but are
constrained within the bounds of global ones. The first group rules
restrict further than global rules, but any additional group rule will
add more permissions.
Detailed algorithm:
1. Global rules are combined together with a logical AND operator, and with the result of the following steps
2. Group-specific rules are combined together with a logical OR operator
3. If user belongs to several groups, the results from step 2 are combined with logical OR operator
Example: GLOBAL_RULE_1 AND GLOBAL_RULE_2 AND ((GROUP_A_RULE_1 OR
GROUP_A_RULE_2) OR (GROUP_B_RULE_1 OR GROUP_B_RULE_2))
但我总是对规则有疑问,上面的文字对我来说不正确(除非我误解了什么)。
现在我遇到了这个简单的情况:我有一个模型,它的记录可以被任何用户读取,但只能由与该记录属于同一公司的用户创建、编辑和删除。
所以我需要一个全局规则(它将适用于所有人,而不仅仅是一个群体)。
<record model="ir.rule" id="my_custom_rule_a">
<field name="name">My custom rule A</field>
<field name="model_id" ref="my_module.model_my_model"/>
<field name="domain_force">[('company_id', '=', user.company_id.id)]</field>
<field name="perm_read" eval="True"/>
<field name="perm_write" eval="True"/>
<field name="perm_create" eval="True"/>
<field name="perm_unlink" eval="True"/>
</record>
如果我只创建这条规则,与记录属于同一公司的用户可以读取、创建、编辑和删除它,这没问题,但如果用户与记录不属于同一公司记录,他们连记录都看不懂
所以让我们添加另一条规则以允许他们读取属于其他公司的那些记录:
<record model="ir.rule" id="my_custom_rule_b">
<field name="name">My custom rule B</field>
<field name="model_id" ref="my_module.model_my_model"/>
<field name="domain_force">[('company_id', '!=', user.company_id.id)]</field>
<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>
当我添加这条规则时,任何人都无法读取任何记录,用户是否属于同一记录的公司无关紧要...所以我将第二条规则修改为这样:
<record model="ir.rule" id="my_custom_rule_b">
<field name="name">My custom rule B</field>
<field name="model_id" ref="my_module.model_my_model"/>
<field name="domain_force">['|', ('company_id', '=', user.company_id.id), ('company_id', '!=', user.company_id.id)]</field>
<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>
但现在的行为与我只添加了第一条规则完全一样:属于同一公司的用户可以对记录执行任何操作,而不属于同一公司的用户不能'连记录都没看过
有人能帮帮我吗?我在规则方面遇到了更大的问题,所以开始理解这个小问题以面对其他问题会很棒。
全球规则始终是 AND,正如您在问题中复制的文档中所读到的那样。每条记录都将根据这些规则进行检查。
所以你的第一次尝试是互斥的,这就是为什么没有找到记录的原因。
你第二次尝试的结果也是正确的,因为只有用户公司的记录同时满足两个记录规则。
你必须做什么才能得到你想要的结果:你必须使用非全局记录规则。
一个非常简单的例子:
型号 -> my.model
Employees 组(Odoo 中的默认用户组,但您可以创建自己的用户组)的模型访问权限 (ir.model.access) -> CRUD 1111(每个人对每条记录的所有权利,将受到记录规则)
现在您还需要为组 Employees 设置两条记录规则:
A 域 [('company_id', '=', user.company_id.id)]
和 CRUD 1111(用户公司记录的所有权利)
B 域 [(1, '=', 1)]
和 CRUD 0100(每条记录的读取权限)
实际上就是这样。作为旁注:客户在这种情况下表现非常糟糕。公司 B 的用户将看到公司 A 的记录(根据需要),起初用户似乎可以更改它们,因为显示了编辑按钮和 "functional",但在保存时会弹出访问权限警告。
我一如既往地受制于规则,因为我还不能理解它们。
这就是我们所说的:
Interaction between rules
Global rules (non group-specific) are restrictions, and cannot be bypassed. Group-local rules grant additional permissions, but are constrained within the bounds of global ones. The first group rules restrict further than global rules, but any additional group rule will add more permissions.
Detailed algorithm: 1. Global rules are combined together with a logical AND operator, and with the result of the following steps 2. Group-specific rules are combined together with a logical OR operator 3. If user belongs to several groups, the results from step 2 are combined with logical OR operator
Example: GLOBAL_RULE_1 AND GLOBAL_RULE_2 AND ((GROUP_A_RULE_1 OR GROUP_A_RULE_2) OR (GROUP_B_RULE_1 OR GROUP_B_RULE_2))
但我总是对规则有疑问,上面的文字对我来说不正确(除非我误解了什么)。
现在我遇到了这个简单的情况:我有一个模型,它的记录可以被任何用户读取,但只能由与该记录属于同一公司的用户创建、编辑和删除。
所以我需要一个全局规则(它将适用于所有人,而不仅仅是一个群体)。
<record model="ir.rule" id="my_custom_rule_a">
<field name="name">My custom rule A</field>
<field name="model_id" ref="my_module.model_my_model"/>
<field name="domain_force">[('company_id', '=', user.company_id.id)]</field>
<field name="perm_read" eval="True"/>
<field name="perm_write" eval="True"/>
<field name="perm_create" eval="True"/>
<field name="perm_unlink" eval="True"/>
</record>
如果我只创建这条规则,与记录属于同一公司的用户可以读取、创建、编辑和删除它,这没问题,但如果用户与记录不属于同一公司记录,他们连记录都看不懂
所以让我们添加另一条规则以允许他们读取属于其他公司的那些记录:
<record model="ir.rule" id="my_custom_rule_b">
<field name="name">My custom rule B</field>
<field name="model_id" ref="my_module.model_my_model"/>
<field name="domain_force">[('company_id', '!=', user.company_id.id)]</field>
<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>
当我添加这条规则时,任何人都无法读取任何记录,用户是否属于同一记录的公司无关紧要...所以我将第二条规则修改为这样:
<record model="ir.rule" id="my_custom_rule_b">
<field name="name">My custom rule B</field>
<field name="model_id" ref="my_module.model_my_model"/>
<field name="domain_force">['|', ('company_id', '=', user.company_id.id), ('company_id', '!=', user.company_id.id)]</field>
<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>
但现在的行为与我只添加了第一条规则完全一样:属于同一公司的用户可以对记录执行任何操作,而不属于同一公司的用户不能'连记录都没看过
有人能帮帮我吗?我在规则方面遇到了更大的问题,所以开始理解这个小问题以面对其他问题会很棒。
全球规则始终是 AND,正如您在问题中复制的文档中所读到的那样。每条记录都将根据这些规则进行检查。
所以你的第一次尝试是互斥的,这就是为什么没有找到记录的原因。 你第二次尝试的结果也是正确的,因为只有用户公司的记录同时满足两个记录规则。
你必须做什么才能得到你想要的结果:你必须使用非全局记录规则。
一个非常简单的例子:
型号 -> my.model
Employees 组(Odoo 中的默认用户组,但您可以创建自己的用户组)的模型访问权限 (ir.model.access) -> CRUD 1111(每个人对每条记录的所有权利,将受到记录规则)
现在您还需要为组 Employees 设置两条记录规则:
A 域 [('company_id', '=', user.company_id.id)]
和 CRUD 1111(用户公司记录的所有权利)
B 域 [(1, '=', 1)]
和 CRUD 0100(每条记录的读取权限)
实际上就是这样。作为旁注:客户在这种情况下表现非常糟糕。公司 B 的用户将看到公司 A 的记录(根据需要),起初用户似乎可以更改它们,因为显示了编辑按钮和 "functional",但在保存时会弹出访问权限警告。