odoo10 权限控制用户只允许看到自己的字段

假设一个小区管理员用户,只想看到自己小区的信息。

首先添加一个用户信息选项卡界面,如下图的 用户 > 隶属信息:

我们在自己创建的user模块中,views文件夹下添加base_user.xml

xml 复制代码
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
    <record id="ev_01_base_res_users_inherit_form" model="ir.ui.view">
        <field name="name">res.users.simple.form.inherit</field>
        <field name="model">res.users</field>
        <field name="inherit_id" ref="base.view_users_form"/>
        <field name="arch" type="xml">
            <xpath expr="//page[@name='access_rights']" position="after">
                <page string="隶属信息">
                    <group col="4">
                        <field name="use_community_id"/>
                    </group>
                </page>
            </xpath>
        </field>
    </record>
</odoo>

上述代码中,添加的use_community_id字段是引用的user模型层,如下:

python 复制代码
# -*- coding: utf-8 -*-

from odoo import models, fields, api


class user(models.Model):
    _inherit = 'res.users'


class ResUsers(models.Model):
    """扩展用户类型"""
    _name = "res.users"
    _inherit = "res.users"

    use_community_id = fields.Many2one("community", string=u"所属小区")

    @api.model
    # @tools.ormcache('self._uid')
    def context_get(self):
        # 扩展context,方便xml里面写domain
        user = self.env.user
        result = super(ResUsers, self).context_get()
        result["self_community_id"] = user.use_community_id.id

        return result

user模块的最后一项工作就是在__manifest__.py中添加依赖项,在depends属性中添加需要被权限控制的模块名,添加刚才创建的base_user.xml

在需要被控制的模块的views.xml的action中添加一个名为domain的字段,来控制是否为与当前用户关联的数据。换言之就是,只显示自己数据。

xml 复制代码
<!-- 小区 Action -->
<record id="action_community" model="ir.actions.act_window">
    <field name="name">小区信息</field>
    <field name="res_model">community</field>
    <field name="view_type">form</field>
    <field name="view_mode">tree,form</field>
    <field name="help" type="html">
        <p class="oe_view_nocontent_create">
            创建第一个小区信息
        </p>
    </field>
    <field name="domain">[('id','=',self_community_id)]</field>
</record>

上述代码中的self_community_idResUsers类的context_get方法注册来的。做完这一步,就是注册菜单了,如下代码:

xml 复制代码
<!-- 小区 Menuitem -->
<menuitem id="menu_community_root" name="小区" groups="ev_01.group_tw_use_xq_user"/>
<menuitem id="menu_community" name="小区信息" parent="menu_community_root" action="action_community" sequence="10" groups="ev_01.group_tw_use_xq_user"/>

通过配置多个action和菜单,可以让不同的用户显示不同的菜单,例如超级管理员的菜单应该显示全部小区信息,而小区用户只能显示自己小区的信息。最后记得升级user模块和被权限控制的模块,效果如下:

相关推荐
曲幽1 小时前
我用FastAPI接ollama大模型,差点被asyncio整崩溃(附对话窗口实战)
python·fastapi·web·async·httpx·asyncio·ollama
两万五千个小时5 小时前
落地实现 Anthropic Multi-Agent Research System
人工智能·python·架构
哈里谢顿7 小时前
Python 高并发服务限流终极方案:从原理到生产落地(2026 实战指南)
python
用户83562907805121 小时前
无需 Office:Python 批量转换 PPT 为图片
后端·python
markfeng81 天前
Python+Django+H5+MySQL项目搭建
python·django
GinoWi1 天前
Chapter 2 - Python中的变量和简单的数据类型
python
JordanHaidee1 天前
Python 中 `if x:` 到底在判断什么?
后端·python
ServBay1 天前
10分钟彻底终结冗长代码,Python f-string 让你重获编程自由
后端·python
闲云一鹤1 天前
Python 入门(二)- 使用 FastAPI 快速生成后端 API 接口
python·fastapi
Rockbean1 天前
用40行代码搭建自己的无服务器OCR
服务器·python·deepseek