Skip to content
Snippets Groups Projects
Commit 5e81850e authored by Thibault Delavallée's avatar Thibault Delavallée
Browse files

[FIX] base: ir_attachment check logic

How to fix mail.template attachments by re-attaching them to some
template:

```sql
with template_attach as
 (select *
    from email_template_attachment_rel
   where attachment_id in (
          select id
            from ir_attachment
           where res_id = 0 and not public and res_model='mail.template')
 )
update ir_attachment a set res_id = ta.email_template_id
                      from template_attach ta
                     where a.id = ta.attachment_id;
```
parent 0ef54895
No related branches found
No related tags found
No related merge requests found
......@@ -442,9 +442,17 @@ class MailComposer(models.TransientModel):
'subject': record.subject or False,
'body_html': record.body or False,
'model_id': model.id or False,
'attachment_ids': [(6, 0, [att.id for att in record.attachment_ids])],
}
template = self.env['mail.template'].create(values)
if record.attachment_ids:
# transfer pending attachments to the new template
attachments = self.env['ir.attachment'].sudo().browse(record.attachment_ids.ids).filtered(
lambda a: a.res_model == 'mail.compose.message' and a.create_uid.id == self._uid)
if attachments:
attachments.write({'res_model': template._name, 'res_id': template.id})
template.attachment_ids |= record.attachment_ids
# generate the saved template
record.write({'template_id': template.id})
record.onchange_template_id_wrapper()
......
......@@ -261,7 +261,9 @@ class TestMessageAccess(common.BaseFunctionalTest, common.MockEmails):
def test_mail_message_access_read_notification(self):
attachment = self.env['ir.attachment'].create({
'datas': base64.b64encode(b'My attachment'),
'name': 'doc.txt'})
'name': 'doc.txt',
'res_model': self.message._name,
'res_id': self.message.id})
# attach the attachment to the message
self.message.write({'attachment_ids': [(4, attachment.id)]})
self.message.write({'partner_ids': [(4, self.user_employee.partner_id.id)]})
......
......@@ -1013,7 +1013,7 @@
<t t-set="fileupload_action" t-translation="off">/web/binary/upload_attachment</t>
<t t-set="multi_upload" t-value="true"/>
<input type="hidden" name="model" t-att-value="widget.model"/>
<input type="hidden" name="id" value="0"/>
<input type="hidden" name="id" t-att-value="widget.res_id or 0"/>
</t>
</div>
</div>
......
......@@ -347,10 +347,10 @@ class IrAttachment(models.Model):
self.env['ir.attachment'].flush(['res_model', 'res_id', 'create_uid', 'public', 'res_field'])
self._cr.execute('SELECT res_model, res_id, create_uid, public, res_field FROM ir_attachment WHERE id IN %s', [tuple(self.ids)])
for res_model, res_id, create_uid, public, res_field in self._cr.fetchall():
if not self.env.is_system() and res_field:
raise AccessError(_("Sorry, you are not allowed to access this document."))
if public and mode == 'read':
continue
if not self.env.is_system() and (res_field or (not res_id and create_uid != self.env.uid)):
raise AccessError(_("Sorry, you are not allowed to access this document."))
if not (res_model and res_id):
if create_uid != self._uid:
require_employee = True
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment