diff --git a/mail_env_whitelist/__init__.py b/mail_env_whitelist/__init__.py new file mode 100644 index 000000000..0650744f6 --- /dev/null +++ b/mail_env_whitelist/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/mail_env_whitelist/__manifest__.py b/mail_env_whitelist/__manifest__.py new file mode 100644 index 000000000..76fceb97c --- /dev/null +++ b/mail_env_whitelist/__manifest__.py @@ -0,0 +1,13 @@ +{ + "name": "Mail env whitelist", + "version": "14.0.1.0.0", + "license": "AGPL-3", + "author": "Akretion", + "website": "https://github.com/akretion/ak-odoo-incubator", + "category": "Custom", + "summary": "send email on stagging env when the receiver is in the whitelist", + "depends": ["mail"], + "data": [ + "data/ir_config_parameter_data.xml", + ], +} diff --git a/mail_env_whitelist/data/ir_config_parameter_data.xml b/mail_env_whitelist/data/ir_config_parameter_data.xml new file mode 100644 index 000000000..225c791a0 --- /dev/null +++ b/mail_env_whitelist/data/ir_config_parameter_data.xml @@ -0,0 +1,7 @@ + + + + mail_env_whitelist.test_env_email_to_whitelist + + + diff --git a/mail_env_whitelist/models/__init__.py b/mail_env_whitelist/models/__init__.py new file mode 100644 index 000000000..abbcb50bd --- /dev/null +++ b/mail_env_whitelist/models/__init__.py @@ -0,0 +1 @@ +from . import ir_mail_server diff --git a/mail_env_whitelist/models/ir_mail_server.py b/mail_env_whitelist/models/ir_mail_server.py new file mode 100644 index 000000000..bc6ff6a51 --- /dev/null +++ b/mail_env_whitelist/models/ir_mail_server.py @@ -0,0 +1,41 @@ +# Copyright 2023 Akretion (https://www.akretion.com). +# @author Matthieu SAISON +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +import re + +from odoo import _, api, models +from odoo.exceptions import UserError +from odoo.tools.config import config + + +class IrMailServer(models.Model): + _inherit = "ir.mail_server" + + @api.model + def send_email(self, message, *args, **kwargs): + if config["running_env"] != "prod": + whitelist = self.env["ir.config_parameter"].get_param( + "mail_env_whitelist.test_env_email_to_whitelist" + ) + if not whitelist: + raise UserError( + _( + "Whitelist is not configured. Configure the following parameter " + "'mail_env_whitelist.test_env_email_to_whitelist'" + ) + ) + for key in ["To", "cc", "Bcc"]: + if message[key]: + for email in message[key].split(";"): + match = re.search("<(.*)>", email) + if match: + # in case of full syntax "foo" + # extract the email + email = match.group(1) + email = email.lower() + if email not in whitelist: + raise UserError( + _("Test env: following mail is not allowed %s") % email + ) + return super().send_email(message, *args, **kwargs) diff --git a/setup/mail_env_whitelist/odoo/addons/mail_env_whitelist b/setup/mail_env_whitelist/odoo/addons/mail_env_whitelist new file mode 120000 index 000000000..3642a68db --- /dev/null +++ b/setup/mail_env_whitelist/odoo/addons/mail_env_whitelist @@ -0,0 +1 @@ +../../../../mail_env_whitelist \ No newline at end of file diff --git a/setup/mail_env_whitelist/setup.py b/setup/mail_env_whitelist/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/mail_env_whitelist/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)