Odoo字段访问权限/规则

2024-10-01 11:29:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我想让记录中的一些字段对于在字段forbridden_user被选中的用户不可编辑。但不是所有的领域。有些字段必须对他是可编辑的。我怎么能做到呢?在


Tags: 用户编辑记录领域userforbridden
2条回答

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

<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字段。当当前用户等于^{时,该字段将被设置为True。我们可以在客户端使用它使受保护字段显示为只读。在

将计算字段添加到您的视图中(作为一个不可见的字段-我们只需要它的值可用),并将attrs属性添加到要保护的字段中,其中的域将使该字段在current_user_forbidden字段为True时显示为只读:

^{pr2}$

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

相关问题 更多 >