如果当前编辑的用户具有更高的权限,则将字段设置为只读



所以我的目标是让一组用户能够添加新用户,但他们不应该有其他管理员权限。

我想解决这个问题的方法是让有权只添加用户的组和另一个具有所有管理员权限的组。为此,我需要防止第一组中的用户编辑第二组中的用户。

我有带有此代码的模块:

<record model="ir.ui.view" id="view_user_readonly" >
    <field name="name">res.users.form.readonly</field>
    <field name="model">res.users</field>
    <field name="inherit_id" ref="base.view_users_form"/>
    <field name="arch" type="xml">
        <xpath expr="//field[@name='login']" position="attributes">
            <attribute name="readonly">True</attribute>
        </xpath>
    </field>
</record>

它将所有用户的字段设置为只读。现在我想改变的是,当我们尝试编辑的用户具有组base.group_system而我们没有时,它是只读的。

我尝试添加基于互联网上的 anwsers 的代码,如下所示:

<record model="ir.ui.view" id="view_user_readonly" >
    <field name="name">res.users.form.readonly</field>
    <field name="model">res.users</field>
    <field name="inherit_id" ref="base.view_users_form"/>
    <field name="arch" type="xml">
        <xpath expr="//field[@name='login']" position="attributes">
            <attribute name="readonly">True</attribute>
        </xpath>
    </field>
</record>
<record model="ir.ui.view" id="view_user_readonly_settings">
    <field name="name">res.users.form.readonly.settings</field>
    <field name="model">res.users</field>
    <field name="inherit_id" ref="custom_user_fields.view_user_readonly" />
    <field name="groups_id" eval="[(6, 0, [ref('base.group_system')])]"/>
    <field name="arch" type="xml">
        <xpath expr="//field[@name='login']" position="attributes">
            <attribute name="readonly">False</attribute>
        </xpath>
    </field>
</record>

但它没有按预期工作。它限制没有base.group_system的每个人进行编辑,我需要允许其他用户在某些情况下编辑此字段(例如在创建新用户时(。或者,如果可能的话 - 当我们正在编辑的用户具有"base.group_system"而我们没有"时,将此字段设置为只读。

#EDIT内部用户不应能够编辑其他用户。管理员(具有组访问权限的用户 - group_erp_manager (应该能够编辑内部用户和其他管理员,但不能编辑管理员。管理员(具有组设置 - group_system 的用户(应有权编辑所有人。

如果编辑的意思是阻止用户编辑任何字段 你真的有很多案例,因为你需要检查 例如:

1. manager cannot create an admin user
2. manger cannot make him self admin but can create other manager or normal users

我为此创建了一个应用程序,但实际上现在不是 它在工作。

好的,我将尝试一个简单的解决方案,允许经理 编辑任何用户,但如果他尝试编辑管理员,则通过错误消息阻止他。

@api.multi
def check_editing_previlage(self):
    """ check if users is allowed to edit this record."""
    # admin can do what ever he wants
    if self.env.user.has_group('base.group_system'):
        return True
    if self.env.user.has_group('base.group_erp_manager'):
        # 1. admin can create a user
        if isinstance(self.id, models.NewId):
            return True
        # 2. he is not allowed to edit and admin
        if self.has_group('base.group_system'):
            return False
        # 3. he is allowed to edit this users
        return True
    # other user are not allowed to edit any user
    return False

@api.mutli
def write(self, vals):
    for rec in self:
        if not rec.check_editing_previlage():
            # show a nice error that tells the user that he is not allowed to edit 
            # this user
            raise exceptions.ValidationError(_('''You are not allowed to edit user %s contact your manager
                                               if you think that is an error.
                                               ''') % rec.name)
    # continute the editing otherwise 
    return super(ClassName, self).write(vals)

此解决方案的问题在于,经理可以使他自己或其他人使用管理员。 为了让您停止此操作,您需要检查添加的组是什么 并删除并在非管理员用户尝试时引发异常 以添加或删除管理员权限。您可以发布另一个问题并指定什么 您正在使用的Odoo版本。

我认为您需要某些用户的管理员权限,因为我们已经这样做了,对于某些用户,您只需要创建权限而不是编辑。如果您需要具有组"group_system"的用户仅创建权限而不编辑,那么请按照以下代码操作:

<record model="ir.ui.view" id="view_user_readonly_settings">
<field name="name">res.users.form.readonly.settings</field>
<field name="model">res.users</field>
<field name="inherit_id" ref="custom_user_fields.view_user_readonly" />
<field name="groups_id" eval="[(6, 0, [ref('base.group_system')])]"/>
<field name="arch" type="xml">
    <xpath expr="//form" position="attributes">
        <attribute name="create">1</attribute>
        <attribute name="edit">0</attribute>
    </xpath>
</field>

具有组"group_system"的用户将仅具有具有"某些登录名(可编辑(的创建"权限。他们无法编辑旧记录。

最新更新