You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Odoo15中为消息发送功能添加权限的实现问题

Odoo继承mail.ChatterTopbar模板失效排查与权限控制实现

一、模板替换失效的修复方案

你的QWeb模板未生效,大概率是继承写法或加载配置的问题,按以下步骤调整:

1. 修正QWeb模板写法

改用xpath定位元素(比t-jquery更稳定,适配Odoo版本迭代):

<?xml version="1.0" encoding="utf-8"?>
<templates xml:space="preserve">
    <t t-inherit="mail.ChatterTopbar" t-inherit-mode="primary" name="custom_chatter_topbar">
        <xpath expr="//button[hasclass('o_ChatterTopbar_buttonSendMessage')]" position="replace">
            <button class="btn btn-link">Test Me</button>
        </xpath>
    </t>
</templates>

2. 确认manifest配置正确性

确保模块依赖mail,且模板资源加载顺序正确:

{
    'name': 'Custom Chatter Permission',
    'version': '1.0',
    'depends': ['mail'],  # 必须依赖mail模块
    'data': [
        'static/src/xml/sendblock_template.xml',
    ],
    'qweb': [
        'static/src/xml/sendblock_template.xml',
    ],
    'installable': True,
    'auto_install': False,
}

3. 清除缓存

模板不生效常与缓存有关,执行以下操作:

  • 重启Odoo服务
  • 浏览器按Ctrl+Shift+R强制刷新页面
  • Odoo后台进入「设置→技术→用户界面→视图」,点击「清除QWeb缓存」

二、添加特定用户发送消息权限控制

要实现仅指定用户可向合作伙伴发消息,需结合后端权限组与前端条件渲染:

1. 定义用户权限组

在模块security/groups.xml中创建权限组:

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <record id="group_send_message_to_partner" model="res.groups">
        <field name="name">可向合作伙伴发送消息</field>
        <field name="category_id" ref="base.module_category_user_management"/>
    </record>
</odoo>

在security/ir.model.access.csv中配置组权限:

id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_group_send_message,向合作伙伴发消息权限,mail.model_mail_message,your_module.group_send_message_to_partner,1,1,1,0

将上述security文件添加到manifest的data列表:

'data': [
    'security/groups.xml',
    'security/ir.model.access.csv',
    'static/src/xml/sendblock_template.xml',
],

2. 前端模板添加权限判断

修改模板,根据用户所属组渲染按钮:

<?xml version="1.0" encoding="utf-8"?>
<templates xml:space="preserve">
    <t t-inherit="mail.ChatterTopbar" t-inherit-mode="primary" name="custom_chatter_topbar">
        <xpath expr="//button[hasclass('o_ChatterTopbar_buttonSendMessage')]" position="replace">
            <!-- 仅有权限组的用户可见原发送按钮 -->
            <t t-if="user_has_group('your_module.group_send_message_to_partner')">
                <button class="btn btn-primary o_ChatterTopbar_buttonSendMessage" type="button">
                    发送消息
                </button>
            </t>
            <!-- 无权限用户显示禁用状态按钮 -->
            <t t-else="">
                <button class="btn btn-secondary" type="button" disabled>发送消息(权限受限)</button>
            </t>
        </xpath>
    </t>
</templates>

3. (可选)JavaScript方式控制

若需更灵活的逻辑(比如根据当前记录判断权限),可重写ChatterTopbar组件:
在static/src/js/chatter_topbar.js中:

odoo.define('your_module.ChatterTopbar', function (require) {
    "use strict";

    const ChatterTopbar = require('mail.ChatterTopbar');
    const session = require('web.session');

    ChatterTopbar.include({
        start: function () {
            this._super.apply(this, arguments);
            // 检查用户是否在目标权限组
            const hasPermission = session.user_has_group('your_module.group_send_message_to_partner');
            if (!hasPermission) {
                this.$('.o_ChatterTopbar_buttonSendMessage').hide();
            }
        },
    });

    return ChatterTopbar;
});

在manifest中添加JS资源:

'assets': {
    'web.assets_backend': [
        'your_module/static/src/js/chatter_topbar.js',
    ],
},

内容的提问来源于stack exchange,提问作者Fernando_Ruiz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 10:27:33