diff --git a/dbfilter_from_config/__init__.py b/dbfilter_from_config/__init__.py new file mode 100644 index 0000000..4d2d9ec --- /dev/null +++ b/dbfilter_from_config/__init__.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- + +# Copyright 2022 Fabien Bourgeois <fabien@yaltik.com> +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +from . import override diff --git a/dbfilter_from_config/__manifest__.py b/dbfilter_from_config/__manifest__.py new file mode 100644 index 0000000..ae4bdbb --- /dev/null +++ b/dbfilter_from_config/__manifest__.py @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- + +# Copyright 2022 Fabien Bourgeois <fabien@yaltik.com> +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +{ + 'name': 'DBFilter from configuration', + 'summary': 'Database filtering from configuration', + 'description': """ Database filtering from configuration : + - use Odoo own filtering mechanism fist ; + - then apply configuration : `domains_to_databases` : string in form of `database:domain1,domain2|database2:domain3`. + + Needs to be added to `server_wide_modules` on configuration or `--load`, + for example `server_wide_modules = base,web,dbfilter_from_config`. + Also required : `proxy_mode` to `True` + + Inspired from OCA `dbfilter_from_header`.""", + 'version': '12.0.0.0.1', + 'category': 'Yaltik', + 'author': 'Fabien Bourgeois', + 'license': 'AGPL-3', + 'application': False, + 'installable': True, + 'depends': ['base'] +} diff --git a/dbfilter_from_config/override.py b/dbfilter_from_config/override.py new file mode 100644 index 0000000..4b4b432 --- /dev/null +++ b/dbfilter_from_config/override.py @@ -0,0 +1,63 @@ +# -*- coding: utf-8 -*- + +# Copyright 2022 Fabien Bourgeois <fabien@yaltik.com> +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +""" Override of databse filtering to allow new features """ + +from re import match +from json import loads +from odoo import http +from odoo.tools import config + +db_filter_odoo = http.db_filter + +# Cache usage +db_filter_dict = {} +all_hosts = [] +db_filter_str = config.get('database_from_hosts', False) +if db_filter_str: + for entry in db_filter_str.split('|'): + database, hosts = entry.split(':') + hosts = hosts.split(',') + db_filter_dict[database] = hosts + all_hosts += hosts +http_host_resolv = {} + +def db_filter(dbs, httprequest=None): + """ Override db_filter """ + if db_filter_str: + httprequest = httprequest or http.request.httprequest + http_host = httprequest.environ.get('HTTP_HOST', '').split(':')[0] + if http_host in http_host_resolv: + dbs = http_host_resolv[http_host] + elif http_host in all_hosts: + for database, hosts in db_filter_dict.items(): + for host in hosts: + if host == http_host: + dbs = [i for i in dbs if match(database, i)] + break + else: + dbs = db_filter_odoo(dbs, httprequest) + else: + dbs = db_filter_odoo(dbs, httprequest) + if http_host not in http_host_resolv: + http_host_resolv[http_host] = dbs + return dbs + + +if (config.get('proxy_mode') + and 'dbfilter_from_config' in config.get('server_wide_modules')): + http.db_filter = db_filter