social/mail_notification_custom_subject/models/mail_thread.py
Jairo Llopis ba8cf6ec7a
[FIX] mail_notification_custom_subject: do not require being admin to send messages
After installing the module, any user was required to have admin rights to be able to search across models.

This fix does just that search with sudo and returns back to non-sudo mode immediately after. This way, we don't need to increase permissions for everybody.

Tests now run unprivileged, to assert this problem never happens again.

@moduon MT-1644 MT-1645
2022-12-02 08:57:08 +00:00

87 lines
2.9 KiB
Python

# Copyright 2020-2021 Tecnativa - João Marques
# Copyright 2021 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import api, models
class MailThread(models.AbstractModel):
_inherit = "mail.thread"
@api.returns("mail.message", lambda value: value.id)
def message_post(
self,
*,
body="",
subject=None,
message_type="notification",
email_from=None,
author_id=None,
parent_id=False,
subtype_xmlid=None,
subtype_id=False,
partner_ids=None,
attachments=None,
attachment_ids=None,
add_sign=True,
record_name=False,
**kwargs
):
if not subtype_id and subtype_xmlid:
subtype_id = self.env["ir.model.data"]._xmlid_to_res_id(
subtype_xmlid,
raise_if_not_found=False,
)
if subtype_id:
custom_subjects = (
self.env["mail.message.custom.subject"]
.sudo()
.search(
[
("model_id.model", "=", self._name),
("subtype_ids", "=", subtype_id),
]
)
.sudo(False)
)
if not subject:
subject = "Re: %s" % self.env["mail.message"].with_context(
default_model=self._name,
default_res_id=self.id,
)._get_record_name({})
for template in custom_subjects:
try:
rendered_subject_template = self.env[
"mail.template"
]._render_template(
template_src=template.subject_template,
model=self._name,
res_ids=[self.id],
)[
self.id
]
if template.position == "replace":
subject = rendered_subject_template
elif template.position == "append_before":
subject = rendered_subject_template + subject
elif template.position == "append_after":
subject += rendered_subject_template
except Exception:
rendered_subject_template = False
return super().message_post(
body=body,
subject=subject,
message_type=message_type,
email_from=email_from,
author_id=author_id,
parent_id=parent_id,
subtype_xmlid=subtype_xmlid,
subtype_id=subtype_id,
partner_ids=partner_ids,
attachments=attachments,
attachment_ids=attachment_ids,
add_sign=add_sign,
record_name=record_name,
**kwargs,
)