Odoo 字段访问 rights/rules

Odoo fields access rights/rules

我想让记录中的某些字段对于在字段 forbridden_user 中被选中的用户不可编辑。但不是所有领域。某些字段必须仍然可以为他编辑。我怎样才能做到这一点?

这里的基本思想是,
1) 继承视图
2) 指定要将字段限制到的组
3) 然后修改字段属性。
我在这里粘贴了示例代码,这将使 Employee contirbution 字段在 Data Team 组用户登录时只读。

<record id="view_contribution_fields_form" model="ir.ui.view">
            <field name="name">member.contribution.form.editable.list</field>
            <field name="model">member.contribution</field>
            <field name="inherit_id" ref="contribution_view_form"/> <!-- ref = 'module_name.form_view_id'-->
            <field name="groups_id" eval="[(6, 0, [ref('group_data_team')])]"/>
            <field name="arch" type="xml">
                  <field name="contribution_employee" position="attributes">
                      <attribute name="readonly">1</attribute>
                  </field>
            </field>
         </record>

像这样可以修改特定用户登录时的字段属性

这里有两个不同的问题:

  1. 使该字段在表单中显示为只读。
  2. 保护它,使其真正不能被修改。

这些是不同的问题,如果只解决第一点,您将来可能会遇到非常不愉快的惊喜。

不幸的是,Odoo 没有提供每个字段的权限框架 (you can read my rant about this here)。

如果你愿意,你可以use a module I created while working on a project, that addresses this very issue

下载模块并将 protected_fields 添加到模块的依赖项后,您可以执行如下操作:

class YourModel(models.Model):
    _name = 'your.model'
    _inherit = [
        'protected_fields.mixin',
    ]
    _protected_fields = ['field_you_want_to_protect']

    field_you_want_to_protect = fields.Char()
    forbridden_user = fields.Many2one('res.users')
    current_user_forbidden = fields.Boolean(compute="_compute_current_user_forbidden")

    @api.one
    @api.depends('forbridden_user')
    def _compute_current_user_forbidden(self):
        """
        Compute a field indicating whether the current user
        shouldn't be able to edit some fields.
        """
        self.current_user_forbidden = (self.forbridden_user == self.env.user)

    @api.multi
    def _is_permitted(self):
        """
        Allow only authorised users to modify protected fields
        """
        permitted = super(DetailedReport, self)._is_permitted()
        return permitted or not self.current_user_forbidden

这将负责安全地保护服务器端的字段并另外创建一个 current_user_forbidden 字段。当 当前 用户等于 forbridden_user 时,该字段将设置为 True。我们可以在客户端使用它来使受保护的字段显示为只读。

将计算字段添加到您的视图(作为一个不可见的字段 - 我们只需要它的值可用)并将 attrs 属性添加到您要保护的字段,带有域当 current_user_forbidden 字段为 True:

时,这将使该字段显示为只读
<field name="current_user_forbidden" invisible="1"/>
<field name="field_you_want_to_protect" attrs="{'readonly': [('current_user_forbidden', '=', True)]}"/>

当然,您应该使用自己想要保护的字段,而不是 field_you_want_to_protect