[FIX] mail: allow a normal user to view the template body

Followup of odoo/odoo@1a3e713

Bug
===
If the user doesn't have the template editor, he cannot open the template
preview.

Technical
=========
The reason for that is because the web editor moves some CSS properties,
and so when the user tries to open the preview, an access error is raised.

Ideally, the template form view should not be editable if the access
rules do not allow it. But in _postprocess_tag_field, we only check
for access right because we don't have the record. So a user without
write access rules, but having write access right can edit the template
in the UI, and gets an error when saving.

Also, the web editor should not save the HTML value if no change are
made on the field (like all other text / char field).

To mitigate the issue in stable, we add a computed field that check the
access rules and make the body readonly if he can not edit the template.
So the web editor is not loaded, the CSS properties are not moved. Other
possible solutions are way to complex technically speaking (editor internals
to update in frontend, complex comparison of html blobs in backend, cache
usage making fields_view_get override not working in all cases, ... )

As the HTML body look weird in readonly mode, add the same border as the web
editor.

Task-2845877

X-original-commit: dcf3ab5fb41aa9ef6e59b2155bfd192e551b6476
Part-of: odoo/odoo#99256
This commit is contained in:
std-odoo
2022-08-30 21:25:55 +02:00
committed by Thibault Delavallée
parent 82cebd0a6d
commit 93fc52c8e1
6 changed files with 24 additions and 4 deletions
+10
View File
@@ -69,12 +69,22 @@ class MailTemplate(models.Model):
help="Sidebar action to make this template available on records "
"of the related document model")
# access
can_write = fields.Boolean(compute='_compute_can_write',
help='The current user can edit the template.')
# Overrides of mail.render.mixin
@api.depends('model')
def _compute_render_model(self):
for template in self:
template.render_model = template.model
@api.depends_context('uid')
def _compute_can_write(self):
writable_templates = self._filter_access_rules('write')
for template in self:
template.can_write = template in writable_templates
# ------------------------------------------------------------
# CRUD
# ------------------------------------------------------------
@@ -175,3 +175,10 @@
background-color: rgba($white, 0.1);
}
}
.o_mail_composer_form {
.oe-bordered-editor[name=body] .o_readonly {
border: 1px solid $o-gray-300;
padding: 4px;
}
}
+4 -1
View File
@@ -53,7 +53,10 @@
<div class="oe_title">
<h2 style="display: inline-block;"><field name="subject" placeholder='e.g. "Welcome to MyCompany" or "Nice to meet you, ${object.name}"'/></h2>
</div>
<field name="body_html" widget="html" class="oe-bordered-editor" options="{'style-inline': true, 'codeview': true }"/>
<field name="can_write" invisible="1"/>
<field name="body_html" widget="html" class="oe-bordered-editor"
options="{'style-inline': true, 'codeview': true }"
attrs="{'readonly': [('can_write', '=', False)]}"/>
<field name="attachment_ids" widget="many2many_binary"/>
</page>
<page string="Email Configuration" name="email_configuration">
@@ -6,7 +6,7 @@
<field name="model">mail.compose.message</field>
<field name="groups_id" eval="[Command.link(ref('base.group_user'))]"/>
<field name="arch" type="xml">
<form string="Compose Email" class="pt-0 pb-0">
<form string="Compose Email" class="pt-0 pb-0 o_mail_composer_form">
<group>
<!-- truly invisible fields for control and options -->
<field name="author_id" invisible="1"/>
+1 -1
View File
@@ -5,7 +5,7 @@
<field name="name">survey.invite.view.form</field>
<field name="model">survey.invite</field>
<field name="arch" type="xml">
<form string="Compose Email">
<form string="Compose Email" class="o_mail_composer_form">
<group col="1">
<group col="2">
<field name="survey_access_mode" invisible="1"/>
@@ -5,7 +5,7 @@
<field name="name">slide.channel.invite.view.form</field>
<field name="model">slide.channel.invite</field>
<field name="arch" type="xml">
<form string="Compose Email">
<form string="Compose Email" class="o_mail_composer_form">
<group col="1">
<group col="2">
<field name="channel_id" invisible="1"/>