Skip to content

Commit

Permalink
[FIX] mail: allow to update main attachment
Browse files Browse the repository at this point in the history
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)

Part-of: odoo#114175
  • Loading branch information
tde-banana-odoo committed Mar 6, 2023
1 parent bab0549 commit 0375bb2
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions addons/mail/models/mail_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -2009,8 +2009,10 @@ def message_post(self, *,

new_message = self._message_create(msg_values)

# Set main attachment field if necessary
self._message_set_main_attachment_id(msg_values['attachment_ids'])
# Set main attachment field if necessary. Call as sudo as people may post
# without read access on the document, notably when replying on a
# notification, which makes attachments check crash.
self.sudo()._message_set_main_attachment_id(msg_values['attachment_ids'])

if msg_values['author_id'] and msg_values['message_type'] != 'notification' and not self._context.get('mail_create_nosubscribe'):
if self.env['res.partner'].browse(msg_values['author_id']).active: # we dont want to add odoobot/inactive as a follower
Expand All @@ -2020,13 +2022,21 @@ def message_post(self, *,
self._notify_thread(new_message, msg_values, **notif_kwargs)
return new_message

def _message_set_main_attachment_id(self, attachment_ids): # todo move this out of mail.thread
def _message_set_main_attachment_id(self, attachment_ids):
""" Update record's main attachment. If not set, take first interesting
attachment and link it on record.
TODO: move this out of mail.thread. """
if not self._abstract and attachment_ids and not self.message_main_attachment_id:
all_attachments = self.env['ir.attachment'].browse([attachment_tuple[1] for attachment_tuple in attachment_ids])
all_attachments = self.env['ir.attachment'].browse([
attachment_tuple[1]
for attachment_tuple in attachment_ids
if attachment_tuple[0] == 4
])
prioritary_attachments = all_attachments.filtered(lambda x: x.mimetype.endswith('pdf')) \
or all_attachments.filtered(lambda x: x.mimetype.startswith('image')) \
or all_attachments
self.sudo().with_context(tracking_disable=True).write({'message_main_attachment_id': prioritary_attachments[0].id})
self.with_context(tracking_disable=True).write({'message_main_attachment_id': prioritary_attachments[0].id})

def _message_post_after_hook(self, message, msg_vals):
""" Hook to add custom behavior after having posted the message. Both
Expand Down

0 comments on commit 0375bb2

Please sign in to comment.