[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@0375bb227a
Part-of: odoo/odoo#114511
This commit is contained in:
Thibault Delavallée
2023-03-07 18:03:03 +01:00
parent 37c100a87d
commit 452ca009e3
2 changed files with 3 additions and 2 deletions
+1
View File
@@ -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.
@@ -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