[ADD] base: add support for custom imbus & cron_trigger postgres notifications

This patch provides the possibility to implement a custom security layer on top
of Odoo's imbus notification system and ir_cron live triggering system.

The key addition is the `ODOO_NOTIFY_FUNCTION` environment variable (opt-in),
which can now define a custom postgresql function to be called instead of the
default `pg_notify` function. This allows for greater flexibility and control
over the notification and triggering mechanisms within Odoo.

closes odoo/odoo#140089

Signed-off-by: Fabien Meghazi (fme) <fme@odoo.com>
This commit is contained in:
Fabien Meghazi
2023-10-28 19:59:03 +00:00
parent b28c4f2b1d
commit 2738f8972e
2 changed files with 13 additions and 3 deletions
+7 -2
View File
@@ -3,11 +3,12 @@ import contextlib
import datetime
import json
import logging
import os
import random
import selectors
import threading
import time
from psycopg2 import InterfaceError
from psycopg2 import InterfaceError, sql
import odoo
from odoo import api, fields, models
@@ -20,6 +21,9 @@ _logger = logging.getLogger(__name__)
# longpolling timeout connection
TIMEOUT = 50
# custom function to call instead of default PostgreSQL's `pg_notify`
ODOO_NOTIFY_FUNCTION = os.getenv('ODOO_NOTIFY_FUNCTION', 'pg_notify')
#----------------------------------------------------------
# Bus
#----------------------------------------------------------
@@ -77,7 +81,8 @@ class ImBus(models.Model):
@self.env.cr.postcommit.add
def notify():
with odoo.sql_db.db_connect('postgres').cursor() as cr:
cr.execute("notify imbus, %s", (json_dump(list(channels)),))
query = sql.SQL("SELECT {}('imbus', %s)").format(sql.Identifier(ODOO_NOTIFY_FUNCTION))
cr.execute(query, (json_dump(list(channels)), ))
@api.model
def _sendone(self, channel, notification_type, message):