Skip to content

Commit

Permalink
Merge pull request #280 from akretion/14.0-mail_env_white_list
Browse files Browse the repository at this point in the history
mail_send override
  • Loading branch information
sebastienbeau authored Oct 30, 2023
2 parents 6805049 + 8f849b9 commit 0e75ca7
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions mail_env_whitelist/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
13 changes: 13 additions & 0 deletions mail_env_whitelist/__manifest__.py
Original file line number Diff line number Diff line change
@@ -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",
],
}
7 changes: 7 additions & 0 deletions mail_env_whitelist/data/ir_config_parameter_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo noupdate="1">
<record id="test_env_email_to_whitelist" model="ir.config_parameter">
<field name="key">mail_env_whitelist.test_env_email_to_whitelist</field>
<field name="value" />
</record>
</odoo>
1 change: 1 addition & 0 deletions mail_env_whitelist/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import ir_mail_server
41 changes: 41 additions & 0 deletions mail_env_whitelist/models/ir_mail_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright 2023 Akretion (https://www.akretion.com).
# @author Matthieu SAISON <[email protected]>
# 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" <[email protected]>
# 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)
1 change: 1 addition & 0 deletions setup/mail_env_whitelist/odoo/addons/mail_env_whitelist
6 changes: 6 additions & 0 deletions setup/mail_env_whitelist/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)

0 comments on commit 0e75ca7

Please sign in to comment.