Skip to content

Commit 6504848

Browse files
committed
Merge PR OCA#186 into 18.0
Signed-off-by yajo
2 parents 77f3d8f + ae9652f commit 6504848

5 files changed

Lines changed: 43 additions & 5 deletions

File tree

mail_notification_custom_subject/README.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ Contributors
101101

102102
- Eduardo de Miguel
103103

104+
- Codeforward <https://www.codeforward.nl>
105+
106+
- Sander Lienaerts
107+
104108
Maintainers
105109
-----------
106110

mail_notification_custom_subject/__manifest__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
{
66
"name": "Mail Notification Custom Subject",
77
"summary": "Apply a custom subject to mail notifications",
8-
"version": "18.0.1.0.0",
8+
"version": "18.0.1.0.1",
99
"category": "Social Network",
1010
"website": "https://github.com/OCA/mail",
1111
"author": "Tecnativa, Odoo Community Association (OCA)",

mail_notification_custom_subject/models/mail_thread.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ def message_post(
4646
if subject:
4747
# If it already had a defined subject, we must respect it
4848
domain += [("position", "!=", "replace")]
49+
# Read templates with sudo: they are admin-managed configuration
50+
# records, akin to mail.template. Portal and public users may
51+
# trigger message_post (e.g. via portal controllers) and must be
52+
# able to iterate the templates without hitting ACL errors.
4953
custom_subjects = (
50-
self.env["mail.message.custom.subject"]
51-
.sudo()
52-
.search(domain)
53-
.sudo(False)
54+
self.env["mail.message.custom.subject"].sudo().search(domain)
5455
)
5556
if not subject:
5657
record_name = (

mail_notification_custom_subject/readme/CONTRIBUTORS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@
1010

1111
- Moduon \<<https://www.moduon.team>\>
1212
- Eduardo de Miguel
13+
14+
- Codeforward \<<https://www.codeforward.nl>\>
15+
- Sander Lienaerts

mail_notification_custom_subject/tests/test_mail_notification_custom_subject.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def setUpClass(cls):
2020
)
2121
cls.admin = new_test_user(cls.env, "boss", "base.group_system")
2222
new_test_user(cls.env, "worker_custom_subject")
23+
new_test_user(cls.env, "portal_custom_subject", "base.group_portal")
2324
cls.subject_model = cls.env["mail.message.custom.subject"].with_user(cls.admin)
2425

2526
@users("worker_custom_subject")
@@ -210,3 +211,32 @@ def test_no_template_default_result(self):
210211
# Get message and check subject
211212
# No exception should be raised but subject should remain as original.
212213
self.assertEqual(mail_message_1.subject, "Test partner 1")
214+
215+
@users("portal_custom_subject")
216+
def test_email_subject_template_portal_user(self):
217+
"""Portal users triggering message_post via sudo (e.g. from a portal
218+
controller) must render custom subject templates without hitting ACL
219+
errors on mail.message.custom.subject — templates are admin-managed
220+
configuration records and should be read with sudo."""
221+
self.subject_model.create(
222+
{
223+
"name": "Test portal template",
224+
"model_id": self.env.ref("base.model_res_partner").id,
225+
"subtype_ids": [(6, 0, [self.env.ref("mail.mt_comment").id])],
226+
"subject_template": "{{object.name or 'n/a'}} - portal",
227+
}
228+
)
229+
portal_user = self.env.user
230+
self.assertTrue(portal_user.has_group("base.group_portal"))
231+
self.assertFalse(portal_user.has_group("base.group_user"))
232+
# Mimic the portal controller pattern: the caller is the portal user,
233+
# the record is sudo'd for message_post, and author_id is set to the
234+
# portal user's partner.
235+
partner = self.env["res.partner"].browse(self.partner_1.id)
236+
mail_message = partner.sudo().message_post(
237+
body="Test from portal",
238+
subtype_xmlid="mail.mt_comment",
239+
author_id=portal_user.partner_id.id,
240+
)
241+
self.assertEqual(mail_message.author_id, portal_user.partner_id)
242+
self.assertEqual(mail_message.subject, "Test partner 1 - portal")

0 commit comments

Comments
 (0)