[FIX] mail: allow the admin to open any emails

Bug
===
When the admin open an email, he might get an error message if he can
not access the mail message.

Technical
=========
To allow to bypass mail message ACl without adding extra SQL queries, we
need to overwrite "_add_inherited_fields".

This trick add a related_sudo on the inherits fields, it can't be done with
>>> subject = fields.Char(related='mail_message_id.subject', related_sudo=True)
because the field of <mail.message> will be fetched two times (one time before of
the inherits, and a second time because of the related), and so it will add extra
SQL queries.

Task-2464212

Part-of: odoo/odoo#78734
This commit is contained in:
std-odoo
2022-05-17 10:10:06 +02:00
parent 0ad4209b6d
commit 0afd1f672f
2 changed files with 22 additions and 2 deletions
+20
View File
@@ -40,6 +40,7 @@ class MailMail(models.Model):
# content
mail_message_id = fields.Many2one('mail.message', 'Message', required=True, ondelete='cascade', index=True, auto_join=True)
mail_message_id_int = fields.Integer(compute='_compute_mail_message_id_int', compute_sudo=True)
body_html = fields.Text('Rich-text Contents', help="Rich-text/HTML message")
references = fields.Text('References', help='Message references, such as identifiers of previous messages', readonly=1)
headers = fields.Text('Headers', copy=False)
@@ -84,6 +85,10 @@ class MailMail(models.Model):
scheduled_date = fields.Char('Scheduled Send Date',
help="If set, the queue manager will send the email after the date. If not set, the email will be send as soon as possible.")
def _compute_mail_message_id_int(self):
for mail in self:
mail.mail_message_id_int = mail.mail_message_id.id
@api.depends('attachment_ids')
def _compute_restricted_attachments(self):
"""We might not have access to all the attachments of the emails.
@@ -135,6 +140,21 @@ class MailMail(models.Model):
self.env['mail.message'].browse(mail_msg_cascade_ids).unlink()
return res
@api.model
def _add_inherited_fields(self):
"""Allow to bypass ACLs for some mail message fields.
This trick add a related_sudo on the inherits fields, it can't be done with
>>> subject = fields.Char(related='mail_message_id.subject', related_sudo=True)
because the field of <mail.message> will be fetched two times (one time before of
the inherits, and a second time because of the related), and so it will add extra
SQL queries.
"""
super()._add_inherited_fields()
cls = type(self)
for field in ('email_from', 'reply_to', 'subject'):
cls._fields[field].related_sudo = True
def action_retry(self):
self.filtered(lambda mail: mail.state == 'exception').mark_outgoing()
+2 -2
View File
@@ -13,13 +13,13 @@
<field name="state" widget="statusbar" statusbar_visible="outgoing,sent,received,exception,cancel"/>
</header>
<sheet>
<field name="mail_message_id" required="0" invisible="1"/>
<field name="mail_message_id_int" required="0" invisible="1"/>
<label for="subject" class="oe_edit_only"/>
<h2><field name="subject"/></h2>
<div style="vertical-align: top;">
by <field name="author_id" class="oe_inline" string="User"/> on <field name="date" readonly="1" class="oe_inline"/>
<button name="%(action_email_compose_message_wizard)d" string="Reply" type="action" icon="fa-reply text-warning"
context="{'default_composition_mode':'comment', 'default_parent_id': mail_message_id}" states='received,sent,exception,cancel'/>
context="{'default_composition_mode':'comment', 'default_parent_id': mail_message_id_int}" states='received,sent,exception,cancel'/>
</div>
<group>
<field name="email_from"/>