From 452ca009e34f02e934ba7c2c8cce45e012f8b441 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Thu, 2 Mar 2023 17:03:36 +0000 Subject: [PATCH] [FIX] mail: allow to update main attachment When replying on a document they cannot read (notably when being notified) ACLs raise when trying to check if 'main_attachment_id' is already set before updating it. This is fixed in this commit. This fixes the recently introduced 'test_post_wo_access' test that does not pass without this fix. Note that the update of 'main_attachment_id' was already done in sudo. As all read or update access is now done in sudo, better call the whole method as sudo in 'message_post' and let '_message_set_main_attachment_id' works on the given record set environment. Task-3213982 (Mail: fix 'multi-company' post support) X-original-commit: odoo/odoo@0375bb227a7fb3a00b764ee267210609405d9bcd Part-of: odoo/odoo#114511 --- addons/mail/models/mail_thread.py | 1 + addons/mail/models/mail_thread_main_attachment.py | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py index 83f708ba492..5e333cc56fb 100644 --- a/addons/mail/models/mail_thread.py +++ b/addons/mail/models/mail_thread.py @@ -2063,6 +2063,7 @@ class MailThread(models.AbstractModel): """ Hook to add custom behavior after having posted the message. Both message and computed value are given, to try to lessen query count by using already-computed values instead of having to rebrowse things. """ + return def _message_mail_after_hook(self, mails): """ Hook to add custom behavior after having sent an mass mailing. diff --git a/addons/mail/models/mail_thread_main_attachment.py b/addons/mail/models/mail_thread_main_attachment.py index 82ab9b7306f..cb1bb678f3b 100644 --- a/addons/mail/models/mail_thread_main_attachment.py +++ b/addons/mail/models/mail_thread_main_attachment.py @@ -16,7 +16,7 @@ class MailMainAttachmentMixin(models.AbstractModel): def _message_post_after_hook(self, message, msg_values): """ Set main attachment field if necessary """ super()._message_post_after_hook(message, msg_values) - self._message_set_main_attachment_id([ + self.sudo()._message_set_main_attachment_id([ attachment_command[1] for attachment_command in (msg_values['attachment_ids'] or []) ]) @@ -24,7 +24,7 @@ class MailMainAttachmentMixin(models.AbstractModel): def _message_set_main_attachment_id(self, attachment_ids): if attachment_ids and not self.message_main_attachment_id: # Assign one of the attachments as the main according to the following priority: pdf, image, other types. - self.sudo().with_context(tracking_disable=True).message_main_attachment_id = max( + self.with_context(tracking_disable=True).message_main_attachment_id = max( self.env['ir.attachment'].browse(attachment_ids), key=lambda r: (r.mimetype.endswith('pdf'), r.mimetype.startswith('image')) ).id