Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fix: second-round review findings on the email template editor

Damian Legawiec6fa54fd4126b
diff --git a/packages/dashboard/src/routes/_authenticated/$storeId/settings/emails/templates/$templateId.tsx b/packages/dashboard/src/routes/_authenticated/$storeId/settings/emails/templates/$templateId.tsx
index 17265622ec..a3fe086ae9 100644
--- a/packages/dashboard/src/routes/_authenticated/$storeId/settings/emails/templates/$templateId.tsx
+++ b/packages/dashboard/src/routes/_authenticated/$storeId/settings/emails/templates/$templateId.tsx
@@ -301,13 +301,17 @@ function EmailTemplateEditor({
confirmLabel: t('admin.email_templates.actions.revert'),
variant: 'destructive',
},
- // The shared version has no draft anyone edits here, so there is no copy
- // to check against; this language's draft is not the one it affects.
- () =>
- revert.mutateAsync({
- language: versionLanguage,
- lock_version: forEveryLanguage ? undefined : lockVersion,
- }),
+ async () => {
+ if (!forEveryLanguage) return revert.mutateAsync({ language, lock_version: lockVersion })
+
+ // The shared version has no draft anyone edits here, so there is no
+ // copy to check; its answer is for every language, so this language's
+ // own state (and its draft) is read again instead.
+ await revert.mutateAsync({ language: versionLanguage })
+ const latest = await onFetchLatest()
+ if (!latest) throw new Error(t('admin.errors.failed_to_load'))
+ return latest
+ },
)
const handleRestore = async (revisionId: string) => {
diff --git a/spree/api/app/controllers/spree/api/v3/admin/email_templates/revisions_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/email_templates/revisions_controller.rb
index 47dadea03d..d35f81c1d8 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/email_templates/revisions_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/email_templates/revisions_controller.rb
@@ -8,6 +8,8 @@ module Spree
class RevisionsController < ResourceController
include Spree::Api::V3::Admin::EmailTemplateLookup
+ before_action { authorize! :read, Spree::EmailTemplate }
+
protected
def model_class
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/email_templates/revisions_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/email_templates/revisions_controller_spec.rb
index 060b1fb20d..24085ca237 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/email_templates/revisions_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/email_templates/revisions_controller_spec.rb
@@ -23,4 +23,23 @@ RSpec.describe Spree::Api::V3::Admin::EmailTemplates::RevisionsController, type:
expect(json_response['data'].map { |revision| revision['id'] }).to eq([newer.prefixed_id, older.prefixed_id])
expect(json_response['data'].last['published_by_type']).to eq('admin_user')
end
+
+ context 'as a staffer without the email templates permission' do
+ let(:staffer) do
+ create(:admin_user, :without_admin_role).tap do |user|
+ create(:role_user, user: user, role: create(:role, name: 'orders_only', permissions: %w[write_orders]))
+ end
+ end
+ let(:headers) do
+ { 'Authorization' => "Bearer #{Spree::Api::V3::TestingSupport.generate_jwt(staffer, audience: Spree::Api::V3::JwtAuthentication::JWT_AUDIENCE_ADMIN)}" }
+ end
+
+ it 'cannot read the history' do
+ create(:email_template_revision, email_template: template)
+
+ get :index, params: { email_template_id: id }, as: :json
+
+ expect(response).to have_http_status(:forbidden)
+ end
+ end
end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/email_templates_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/email_templates_controller_spec.rb
index 4d08204748..7f3de97e0a 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/email_templates_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/email_templates_controller_spec.rb
@@ -52,6 +52,17 @@ RSpec.describe Spree::Api::V3::Admin::EmailTemplatesController, type: :controlle
expect(json_response).to include('body' => 'For everyone', 'customized' => true, 'published_language' => 'any')
end
+ it 'stops reporting a changed default once the draft keeps its own version against the new one' do
+ create(:email_template, store: store, key: editable_key, body: 'Published', base_body: 'An older default')
+ current = Spree::Emails::TemplateResolver.for_mailers.find_default(editable_key)
+ create(:email_template_draft, store: store, key: editable_key, body: 'Mine',
+ base_subject: current.subject, base_body: current.body)
+
+ get :show, params: { id: id }, as: :json
+
+ expect(json_response).to include('default_changed' => false, 'base_body' => nil)
+ end
+
it 'reads the version for the language asked for' do
create(:email_template, store: store, key: editable_key, locale: 'de', body: 'German')
diff --git a/spree/core/app/models/spree/email_templates/entry.rb b/spree/core/app/models/spree/email_templates/entry.rb
index 1f338e8fc2..7aef27a983 100644
--- a/spree/core/app/models/spree/email_templates/entry.rb
+++ b/spree/core/app/models/spree/email_templates/entry.rb
@@ -73,7 +73,7 @@ module Spree
# What customers receive now.
def subject
- readable(customized? ? published.subject : default&.subject)
+ readable(customized? ? published.subject : default&.subject, escape: false)
end
def body
@@ -81,7 +81,7 @@ module Spree
end
def default_subject
- readable(default&.subject)
+ readable(default&.subject, escape: false)
end
def default_body
@@ -89,7 +89,7 @@ module Spree
end
def base_subject
- readable(outdated_version&.base_subject)
+ readable(outdated_version&.base_subject, escape: false)
end
def base_body
@@ -101,15 +101,17 @@ module Spree
#
# @return [Spree::EmailTemplate, Spree::EmailTemplateDraft, nil]
def outdated_version
- [draft, published].compact.find { |version| version.default_changed?(default) }
+ # An open draft is what gets published next, so it alone decides.
+ version = draft || published
+ version if version&.default_changed?(default)
end
private
- def readable(text)
+ def readable(text, escape: true)
return text if locale == Spree::EmailTemplate::ANY_LOCALE
- Spree::Emails::TranslationInliner.call(text, locale: locale)
+ Spree::Emails::TranslationInliner.call(text, locale: locale, escape: escape)
end
end
end
diff --git a/spree/core/app/services/spree/emails/translation_inliner.rb b/spree/core/app/services/spree/emails/translation_inliner.rb
index 21fae2c697..bb9b54b851 100644
--- a/spree/core/app/services/spree/emails/translation_inliner.rb
+++ b/spree/core/app/services/spree/emails/translation_inliner.rb
@@ -21,13 +21,16 @@ module Spree
# @param source [String, nil] a template's subject or body
# @param locale [String, Symbol] the language to write the text in
+ # @param escape [Boolean] whether the text is HTML: false for a subject,
+ # which renders unescaped as a mail header
# @return [String, nil]
- def self.call(source, locale:)
- new(locale).call(source)
+ def self.call(source, locale:, escape: true)
+ new(locale, escape).call(source)
end
- def initialize(locale)
+ def initialize(locale, escape = true)
@locale = locale
+ @escape = escape
end
def call(source)
@@ -50,7 +53,7 @@ module Spree
"{{ '#{text}' #{filters.strip} }}"
else
- interpolate(text, arguments) { |literal| ERB::Util.html_escape(literal) }
+ interpolate(text, arguments) { |literal| literal_text(literal) }
end
end
@@ -62,7 +65,7 @@ module Spree
if arguments.blank? && text.exclude?("'") && text !~ INTERPOLATION
"{% assign #{variable} = '#{text}' %}"
else
- body = interpolate(text, arguments) { |literal| ERB::Util.html_escape(literal) }
+ body = interpolate(text, arguments) { |literal| literal_text(literal) }
body && "{% capture #{variable} %}#{body}{% endcapture %}"
end
end
@@ -79,6 +82,10 @@ module Spree
end.join
end
+ def literal_text(literal)
+ @escape ? ERB::Util.html_escape(literal) : literal
+ end
+
def parse_arguments(arguments)
return {} if arguments.blank?
diff --git a/spree/core/app/workflows/spree/email_templates/save_draft.rb b/spree/core/app/workflows/spree/email_templates/save_draft.rb
index fc72508b39..3186924942 100644
--- a/spree/core/app/workflows/spree/email_templates/save_draft.rb
+++ b/spree/core/app/workflows/spree/email_templates/save_draft.rb
@@ -60,7 +60,7 @@ module Spree
published = store.email_templates.published.where(key: key, locale: [draft.locale, Spree::EmailTemplate::ANY_LOCALE]).
min_by { |template| template.locale == draft.locale ? 0 : 1 }
- draft.subject = readable(published&.subject || default&.subject)
+ draft.subject = readable(published&.subject || default&.subject, escape: false)
draft.body = readable(published&.body || default&.body)
draft.base_subject = published ? published.base_subject : default&.subject
draft.base_body = published ? published.base_body : default&.body
@@ -71,10 +71,10 @@ module Spree
draft.base_body = default&.body
end
- def readable(text)
+ def readable(text, escape: true)
return text if draft.locale == Spree::EmailTemplate::ANY_LOCALE
- Spree::Emails::TranslationInliner.call(text, locale: draft.locale)
+ Spree::Emails::TranslationInliner.call(text, locale: draft.locale, escape: escape)
end
def default
diff --git a/spree/core/spec/services/spree/emails/translation_inliner_spec.rb b/spree/core/spec/services/spree/emails/translation_inliner_spec.rb
index 70eaa9dc1d..68b2c35e9a 100644
--- a/spree/core/spec/services/spree/emails/translation_inliner_spec.rb
+++ b/spree/core/spec/services/spree/emails/translation_inliner_spec.rb
@@ -1,8 +1,8 @@
require 'spec_helper'
describe Spree::Emails::TranslationInliner do
- def inline(source, locale: :en)
- described_class.call(source, locale: locale)
+ def inline(source, locale: :en, escape: true)
+ described_class.call(source, locale: locale, escape: escape)
end
around do |example|
@@ -31,6 +31,10 @@ describe Spree::Emails::TranslationInliner do
expect(inline("{{ 'inliner_spec.markup' | t }}")).to eq('Tom &amp; Jerry &lt;shop&gt;')
end
+ it 'leaves text unescaped for a subject, which renders as a plain mail header' do
+ expect(inline("{{ 'inliner_spec.quoted' | t }} & more", escape: false)).to eq("Don't wait & more")
+ end
+
it 'keeps later filters on text without values' do
expect(inline("{{ 'inliner_spec.plain' | t | upcase }}")).to eq("{{ 'Thanks for shopping' | upcase }}")
end