From 7147d7774e48c37061c0121feb89f27e89746f8d Mon Sep 17 00:00:00 2001 From: "Abdelouahab (abla)" Date: Thu, 24 Aug 2023 11:31:04 +0000 Subject: [PATCH] [FIX] mail: fix current user finding in `_mail_find_partner_from_emails` To reproduce ============ - login as Mitchell Admin - change the email of a portal user, ex: Joel Willis, to the same email as Mitchell Admin. Do this through the Contacts App - always connected as Mitchell Admin, create a sale order and send it by email to client, in chatter the sender will be Joel Willis Problem ======= when setting the author, `_mail_find_partner_from_emails` is called, when searching for users with the given eamil, two results are found and the first one is taken as author Solution ======== give the priority to the current user when it matches the given conditions opw-3455520 closes odoo/odoo#134609 X-original-commit: b4a9095497fb77e6963103fa2a27d20e00172ea1 Signed-off-by: Thibault Delavallee (tde) Signed-off-by: Abdelouahab Laaroussi (abla) --- addons/mail/models/mail_thread.py | 3 +++ addons/mail/tests/test_mail_tools.py | 8 +++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py index 6e78e9c63fd..2e424380c37 100644 --- a/addons/mail/models/mail_thread.py +++ b/addons/mail/models/mail_thread.py @@ -1865,6 +1865,9 @@ class MailThread(models.AbstractModel): done_partners += [partner for partner in partners] remaining = [email for email in normalized_emails if email not in [partner.email_normalized for partner in done_partners]] + # prioritize current user if exists in list + done_partners.sort(key=lambda p: self.env.user.partner_id != p) + # iterate and keep ordering partners = [] for contact in emails: diff --git a/addons/mail/tests/test_mail_tools.py b/addons/mail/tests/test_mail_tools.py index f66a586a93e..a2b220fa69e 100644 --- a/addons/mail/tests/test_mail_tools.py +++ b/addons/mail/tests/test_mail_tools.py @@ -46,7 +46,7 @@ class TestMailTools(MailCommon): '', ] - @users('employee') + @users('admin') def test_find_partner_from_emails(self): Partner = self.env['res.partner'] test_partner = Partner.browse(self.test_partner.ids) @@ -107,6 +107,12 @@ class TestMailTools(MailCommon): found = Partner._mail_find_partner_from_emails(['alfred_astaire@test.example.com']) self.assertEqual(found, [self.env['res.partner']]) + # test users with same email, priority given to current user + # -------------------------------------------------------------- + self.user_employee.sudo().write({'email': '"Alfred Astaire" <%s>' % self.env.user.partner_id.email_normalized}) + found = self.env['res.partner']._mail_find_partner_from_emails([self.env.user.partner_id.email_formatted]) + self.assertEqual(found, [self.env.user.partner_id]) + @users('employee') def test_tools_email_re(self): expected = [