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模块和被权限控制的模块,效果如下:

相关推荐
豆沙沙包?23 分钟前
2025年- H61-Lc169--74.搜索二维矩阵(二分查找)--Java版
python·线性代数·矩阵
AntBlack24 分钟前
计算机视觉 : 端午无事 ,图像处理入门案例一文速通
后端·python·计算机视觉
Freshman小白38 分钟前
基于python脚本进行Maxwell自动化仿真
python·自动化
编程有点难2 小时前
Python训练打卡Day39
人工智能·python·深度学习
(・Д・)ノ2 小时前
python打卡day42
开发语言·python
椰椰椰耶3 小时前
[网页五子棋][匹配模块]处理开始匹配/停止匹配请求(匹配算法,匹配器的实现)
java·python·websocket·spring·java-ee
冰轮a3 小时前
Python打卡 DAY 42
python
FAQEW3 小时前
爬虫工具链的详细分类解析
爬虫·python
汤姆yu3 小时前
基于python大数据的音乐可视化与推荐系统
大数据·开发语言·python
何双新3 小时前
第14讲、Odoo 18 实现一个Markdown Widget模块
python·状态模式