【问题标题】:Odoo fields access rights/rulesOdoo 字段访问权限/规则
【发布时间】:2015-08-13 12:05:05
【问题描述】:

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

【问题讨论】:

标签: python xml python-2.7 odoo


【解决方案1】:

这里有两个不同的问题:

  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

【讨论】:

    【解决方案2】:

    这里的基本思想是,
    1) 继承视图
    2) 指定要限制字段的组
    3) 然后修改字段属性。
    我在这里粘贴示例代码,当数据团队组用户登录时,这将使员工贡献字段只读。

    <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>
    

    这样您可以在特定用户登录时修改字段属性。

    【讨论】:

    • 如果我不想按组限制它怎么办。我想按字段值限制字段访问。如果在forbriden_user 字段中选择了用户,我想限制此用户写入某些字段。
    • 好的,然后简单地继承表单 2) 继承字段 3) 然后应用如下属性。 {'readonly':[('forbidden_​​user','=','some_value')]}
    • 这就是问题所在。 some_value 应该是什么?我试过uiduser.id。两者都不起作用。
    • 好的,如果两者都不起作用..然后尝试 context.get('uid') 代替 some_value。由于上下文将存储当前的uid
    • 使用attrs 无论如何都不是一个安全的选择。它将使字段apear为只读,但经验丰富的用户可以通过在开发人员工具中使其可编辑或直接使用 Odoo 的 Web API 轻松更改该值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    • 2012-12-27
    相关资源
    最近更新 更多