简体   繁体   English

限制字段的写入权限 - Odoo 15

[英]Restrict write permissions for a field - Odoo 15

How can I restrict the write permissions for a field to a specific group?如何将字段的写入权限限制为特定组?

I want to check if a user is in a specific group with id 46. If the user is in this group, he should be allowed to write in this field.我想检查用户是否在 id 为 46 的特定组中。如果用户在该组中,则应允许他在此字段中写入。 If he is not in this group, he should not be allowed to write.如果他不在这个组里,就不应该让他写。

The field is a custom field, editing the domain with the studio app I think I should avoid.该字段是一个自定义字段,使用我认为应该避免的工作室应用程序编辑域。

My field:我的领域:

<field name="customer_codename" placeholder="Codename" attrs="{'invisible':['|',('customer_rank','=', 0),('is_company','=', False)]}"/>

I tried the following, but it did not work: I created a new field using the studio app.我尝试了以下方法,但没有用:我使用工作室应用程序创建了一个新字段。 Field type is boolean. In the advanced properties I wanted to define the compute for the field.字段类型为 boolean。在高级属性中,我想为该字段定义计算。 In dependencies I gave "user_id" and in the compute field I gave在依赖项中,我给出了“user_id”,在我给出的计算字段中

for record in self:
  user_id.has_group('__export__.res_groups_46_eff9dc52')

The boolean field should be set to true if the user is in a certain group.如果用户在某个组中,则 boolean 字段应设置为 true。

Not sure if I can give you the best answer there is.不确定我是否能给你最好的答案。 But for me, I'd personally create a Boolean field in the view's associated model, with its default field a lambda function checking if the user belongs to the groups you mentioned.但对我来说,我会在视图的关联 model 中亲自创建一个 Boolean 字段,其默认字段为 lambda function 检查用户是否属于您提到的组。

Assuming groups_id is the name of the user groups in model res.users , we have:假设groups_id是 model res.users中用户组的名称,我们有:

class ResUsers(models.Model): 
    _inherit = "res.users" 

    can_write_codename = fields.Boolean(default=lambda self: self.groups_id in ("model_name.group_name"))

Then in your xml file, you can include can_write_codename inside attrs , like this:然后在您的 xml 文件中,您可以在attrs中包含can_write_codename ,如下所示:

  <field name="customer_codename" placeholder="Codename" attrs="{'invisible':['|',('customer_rank','=', 0),('is_company','=', False)], 'readonly': [('can_write_codename', '=', 'True')]}"}"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM