From fcfd72a5e5663fa6b08c80a8a22ec15c8a09456f Mon Sep 17 00:00:00 2001
From: niyasraphy <niyasraphyk@gmail.com>
Date: Wed, 24 May 2023 03:40:08 +0000
Subject: [PATCH] [FIX] google_gmail, microsoft_outlook : move UserError to
 ir_mail_server
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The orginal PR and it's forward ports https://github.com/odoo/odoo/pull/121048
intorduced an unexpected AttributeError when using OAuth for incoming
mail servers (fetchmail.server). Since `smtp_user` is not a defined field
in fetchmail.server (it uses the field `user` instead), we had to change
the approach.

To prevent this error, we move the UserError call into the respective
ir_mail_server models, which should check the contrains at that level.
This means that before the form gets saved, trying to connect using an
OAuth account, should prompt the user to first specify an smtp_user before
proceeding.

closes odoo/odoo#122222

Signed-off-by: Stéphane Debauche (std) <std@odoo.com>
---
 addons/google_gmail/i18n/google_gmail.pot              |  2 +-
 addons/google_gmail/models/google_gmail_mixin.py       |  5 -----
 addons/google_gmail/models/ir_mail_server.py           | 10 +++++++++-
 addons/microsoft_outlook/i18n/microsoft_outlook.pot    |  2 +-
 addons/microsoft_outlook/models/ir_mail_server.py      |  5 +++++
 .../models/microsoft_outlook_mixin.py                  |  5 -----
 6 files changed, 16 insertions(+), 13 deletions(-)

diff --git a/addons/google_gmail/i18n/google_gmail.pot b/addons/google_gmail/i18n/google_gmail.pot
index e23d386865eb..bbbcc8fca823 100644
--- a/addons/google_gmail/i18n/google_gmail.pot
+++ b/addons/google_gmail/i18n/google_gmail.pot
@@ -147,7 +147,7 @@ msgid "Please configure your Gmail credentials."
 msgstr ""
 
 #. module: google_gmail
-#: code:addons/google_gmail/models/google_gmail_mixin.py:0
+#: code:addons/google_gmail/models/ir_mail_server.py:0
 #, python-format
 msgid ""
 "Please fill the \"Username\" field with your Gmail username (your email "
diff --git a/addons/google_gmail/models/google_gmail_mixin.py b/addons/google_gmail/models/google_gmail_mixin.py
index 7254c915f1d1..289a0443fb81 100644
--- a/addons/google_gmail/models/google_gmail_mixin.py
+++ b/addons/google_gmail/models/google_gmail_mixin.py
@@ -72,11 +72,6 @@ class GoogleGmailMixin(models.AbstractModel):
         if not self.google_gmail_uri:
             raise UserError(_('Please configure your Gmail credentials.'))
 
-        if not self.smtp_user:
-            raise UserError(_(
-                        'Please fill the "Username" field with your Gmail username (your email address). '
-                        'This should be the same account as the one used for the Gmail OAuthentication Token.'))
-
         return {
             'type': 'ir.actions.act_url',
             'url': self.google_gmail_uri,
diff --git a/addons/google_gmail/models/ir_mail_server.py b/addons/google_gmail/models/ir_mail_server.py
index 93b51e24e319..701fcc965581 100644
--- a/addons/google_gmail/models/ir_mail_server.py
+++ b/addons/google_gmail/models/ir_mail_server.py
@@ -3,7 +3,8 @@
 
 import base64
 
-from odoo import models, api
+from odoo import _, models, api
+from odoo.exceptions import UserError
 
 
 class IrMailServer(models.Model):
@@ -12,6 +13,13 @@ class IrMailServer(models.Model):
     _name = 'ir.mail_server'
     _inherit = ['ir.mail_server', 'google.gmail.mixin']
 
+    @api.constrains('use_google_gmail_service')
+    def _check_use_google_gmail_service(self):
+        if self.filtered(lambda server: server.use_google_gmail_service and not server.smtp_user):
+            raise UserError(_(
+                            'Please fill the "Username" field with your Gmail username (your email address). '
+                            'This should be the same account as the one used for the Gmail OAuthentication Token.'))
+
     @api.onchange('smtp_encryption')
     def _onchange_encryption(self):
         """Do not change the SMTP configuration if it's a Gmail server
diff --git a/addons/microsoft_outlook/i18n/microsoft_outlook.pot b/addons/microsoft_outlook/i18n/microsoft_outlook.pot
index 1888907c77c8..4f3d9f978d3d 100644
--- a/addons/microsoft_outlook/i18n/microsoft_outlook.pot
+++ b/addons/microsoft_outlook/i18n/microsoft_outlook.pot
@@ -171,7 +171,7 @@ msgid "Please configure your Outlook credentials."
 msgstr ""
 
 #. module: microsoft_outlook
-#: code:addons/microsoft_outlook/models/microsoft_outlook_mixin.py:0
+#: code:addons/microsoft_outlook/models/ir_mail_server.py:0
 #, python-format
 msgid ""
 "Please fill the \"Username\" field with your Outlook/Office365 username "
diff --git a/addons/microsoft_outlook/models/ir_mail_server.py b/addons/microsoft_outlook/models/ir_mail_server.py
index e988569fd637..14aa20ccc6de 100644
--- a/addons/microsoft_outlook/models/ir_mail_server.py
+++ b/addons/microsoft_outlook/models/ir_mail_server.py
@@ -33,6 +33,11 @@ class IrMailServer(models.Model):
                     'Please set it to "TLS (STARTTLS)".')
                     % server.name)
 
+            if not server.smtp_user:
+                raise UserError(_(
+                            'Please fill the "Username" field with your Outlook/Office365 username (your email address). '
+                            'This should be the same account as the one used for the Outlook OAuthentication Token.'))
+
     @api.onchange('smtp_encryption')
     def _onchange_encryption(self):
         """Do not change the SMTP configuration if it's a Outlook server
diff --git a/addons/microsoft_outlook/models/microsoft_outlook_mixin.py b/addons/microsoft_outlook/models/microsoft_outlook_mixin.py
index abd331ff208f..2bd78018be0c 100644
--- a/addons/microsoft_outlook/models/microsoft_outlook_mixin.py
+++ b/addons/microsoft_outlook/models/microsoft_outlook_mixin.py
@@ -81,11 +81,6 @@ class MicrosoftOutlookMixin(models.AbstractModel):
         if not self.use_microsoft_outlook_service or not self.is_microsoft_outlook_configured:
             raise UserError(_('Please configure your Outlook credentials.'))
 
-        if not self.smtp_user:
-            raise UserError(_(
-                        'Please fill the "Username" field with your Outlook/Office365 username (your email address). '
-                        'This should be the same account as the one used for the Outlook OAuthentication Token.'))
-
         return {
             'type': 'ir.actions.act_url',
             'url': self.microsoft_outlook_uri,
-- 
GitLab