A platform for community discussion. Free, open, simple.
FEATURE: Allow post authors to localize their own posts (#36178)
Natalie Tay3f8ccda7aa33
diff --git a/app/controllers/post_localizations_controller.rb b/app/controllers/post_localizations_controller.rbindex 70edce0b6cf..37976e45142 100644--- a/app/controllers/post_localizations_controller.rb+++ b/app/controllers/post_localizations_controller.rb@@ -4,12 +4,13 @@ class PostLocalizationsController < ApplicationControllerbefore_action :ensure_logged_indef show- guardian.ensure_can_localize_content!+ post_id = params[:id] || params[:post_id]+ raise ActionController::ParameterMissing.new(:id) if post_id.blank?- params.require(:post_id)+ post = Post.find_by(id: post_id)+ return render_json_error(I18n.t("not_found"), status: :not_found) if post.blank?- post = Post.find_by(id: params[:post_id])- return render json_error(I18n.t("not_found"), status: :not_found) if post.blank?+ guardian.ensure_can_localize_post!(post)post_localizations = PostLocalization.where(post_id: post.id)@@ -36,39 +37,26 @@ class PostLocalizationsController < ApplicationControllerenddef create_or_update- guardian.ensure_can_localize_content!+ post_id, locale, raw = params.require(%i[post_id locale raw])- params.require(%i[post_id locale raw])+ guardian.ensure_can_localize_post!(post_id)- localization = PostLocalization.find_by(post_id: params[:post_id], locale: params[:locale])+ localization = PostLocalization.find_by(post_id:, locale:)if localization- PostLocalizationUpdater.update(- post_id: params[:post_id],- locale: params[:locale],- raw: params[:raw],- user: current_user,- )+ PostLocalizationUpdater.update(post_id: post_id, locale:, raw:, user: current_user)render json: success_json, status: :okelse- PostLocalizationCreator.create(- post_id: params[:post_id],- locale: params[:locale],- raw: params[:raw],- user: current_user,- )+ PostLocalizationCreator.create(post_id: post_id, locale:, raw:, user: current_user)render json: success_json, status: :createdendenddef destroy- guardian.ensure_can_localize_content!+ post_id, locale = params.require(%i[post_id locale])- params.require(%i[post_id locale])- PostLocalizationDestroyer.destroy(- post_id: params[:post_id],- locale: params[:locale],- acting_user: current_user,- )+ guardian.ensure_can_localize_post!(post_id)++ PostLocalizationDestroyer.destroy(post_id:, locale:, acting_user: current_user)head :no_contentendenddiff --git a/app/controllers/topic_localizations_controller.rb b/app/controllers/topic_localizations_controller.rbindex 97c8a11ab44..73bae61be79 100644--- a/app/controllers/topic_localizations_controller.rb+++ b/app/controllers/topic_localizations_controller.rb@@ -4,40 +4,31 @@ class TopicLocalizationsController < ApplicationControllerbefore_action :ensure_logged_indef create_or_update- guardian.ensure_can_localize_content!+ topic_id, locale, title = params.require(%i[topic_id locale title])- params.require(%i[topic_id locale title])+ guardian.ensure_can_localize_topic!(topic_id)- topic_localization =- TopicLocalization.find_by(topic_id: params[:topic_id], locale: params[:locale])+ topic_localization = TopicLocalization.find_by(topic_id: topic_id, locale: params[:locale])if topic_localizationTopicLocalizationUpdater.update(- topic_id: params[:topic_id],+ topic_id: topic_id,locale: params[:locale],title: params[:title],user: current_user,)render json: success_json, status: :okelse- TopicLocalizationCreator.create(- topic_id: params[:topic_id],- locale: params[:locale],- title: params[:title],- user: current_user,- )+ TopicLocalizationCreator.create(topic_id:, locale:, title:, user: current_user)render json: success_json, status: :createdendenddef destroy- guardian.ensure_can_localize_content!+ topic_id, locale = params.require(%i[topic_id locale])++ guardian.ensure_can_localize_topic!(topic_id)- params.require(%i[topic_id locale])- TopicLocalizationDestroyer.destroy(- topic_id: params[:topic_id],- locale: params[:locale],- acting_user: current_user,- )+ TopicLocalizationDestroyer.destroy(topic_id:, locale:, acting_user: current_user)head :no_contentendenddiff --git a/app/jobs/regular/process_localized_cooked.rb b/app/jobs/regular/process_localized_cooked.rbindex 3ff93a5d72e..b8451442333 100644--- a/app/jobs/regular/process_localized_cooked.rb+++ b/app/jobs/regular/process_localized_cooked.rb@@ -12,16 +12,13 @@ module Jobspost = post_localization.postreturn if post.blank? || post.topic.blank?- original_cooked = post_localization.cookedprocessor = LocalizedCookedPostProcessor.new(post_localization, post, {})processor.post_process- cooked = processor.html+ cooked = processor.html.strip- if cooked != original_cooked && cooked.present?- post_localization.update_column(:cooked, cooked)- MessageBus.publish("/topic/#{post.topic_id}", type: :localized, id: post.id)- end+ post_localization.update_column(:cooked, cooked) if cooked.present?+ MessageBus.publish("/topic/#{post.topic_id}", type: :localized, id: post.id)endendenddiff --git a/app/serializers/post_serializer.rb b/app/serializers/post_serializer.rbindex b1d99712529..26a86dcace9 100644--- a/app/serializers/post_serializer.rb+++ b/app/serializers/post_serializer.rb@@ -96,6 +96,7 @@ class PostSerializer < BasicPostSerializer:mentioned_users,:post_url,:post_localizations_count,+ :can_localize_post,:locale,:is_localized,:language,@@ -659,7 +660,15 @@ class PostSerializer < BasicPostSerializerenddef include_post_localizations_count?- scope.can_localize_content?+ SiteSetting.content_localization_enabled && scope.can_localize_post?(object)+ end++ def can_localize_post+ true+ end++ def include_can_localize_post?+ SiteSetting.content_localization_enabled && scope.can_localize_post?(object)enddef rawdiff --git a/app/serializers/topic_view_serializer.rb b/app/serializers/topic_view_serializer.rbindex 39b56e54ef2..e9a593cf2e7 100644--- a/app/serializers/topic_view_serializer.rb+++ b/app/serializers/topic_view_serializer.rb@@ -79,6 +79,7 @@ class TopicViewSerializer < ApplicationSerializer:is_shared_draft,:slow_mode_enabled_until,:has_localized_content,+ :can_localize_topic,)has_one :details, serializer: TopicViewDetailsSerializer, root: false, embed: :objects@@ -329,4 +330,12 @@ class TopicViewSerializer < ApplicationSerializerdef include_has_localized_content?SiteSetting.content_localization_enabledend++ def can_localize_topic+ true+ end++ def include_can_localize_topic?+ SiteSetting.content_localization_enabled && scope.can_localize_topic?(object.topic)+ endenddiff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex ddf285b71e3..62aaf0feaad 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -2774,6 +2774,7 @@ en:content_localization_language_switcher: "Show a language switcher in the header, allowing visitors to switch between translated versions of Discourse and user-contributed content. Uses languages defined in 'content localization supported locales'"content_localization_crawler_param: "Serve localized content to web crawlers when 'content localization enabled' and 'set locale from param' is enabled. The list of supported locales is defined in 'content localization supported locales'."content_localization_use_default_locale_when_unsupported: "Serve localized content in the site's default locale to users whose preferred language is not listed in 'content localization supported locales'."+ content_localization_allow_author_localization: "Allow authors update localizations of their own topics and posts via the post menu."enable_upcoming_changes: "Enable upcoming changes"fake_upcoming_change: "This is a fake upcoming change for testing purposes. No need to translate this string."diff --git a/config/site_settings.yml b/config/site_settings.ymlindex e1fe67a4bb2..c63cf7e5ad1 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -1729,6 +1729,10 @@ content_localization:client: truedefault: "1|2" # admin, moderatorarea: "localization"+ content_localization_allow_author_localization:+ default: true+ client: true+ area: "localization"content_localization_supported_locales:default: ""type: listdiff --git a/frontend/discourse/app/components/composer-action-title.gjs b/frontend/discourse/app/components/composer-action-title.gjsindex 76d63f3312a..76affeb795e 100644--- a/frontend/discourse/app/components/composer-action-title.gjs+++ b/frontend/discourse/app/components/composer-action-title.gjs@@ -75,7 +75,6 @@ export default class ComposerActionTitle extends Component {if (this.currentUser &&this.siteSettings.content_localization_enabled &&- this.currentUser.can_localize_content &&allowedActions.includes(this.model.action)) {return true;diff --git a/frontend/discourse/app/components/composer-editor.gjs b/frontend/discourse/app/components/composer-editor.gjsindex 98af1dcb93b..80c4de7469f 100644--- a/frontend/discourse/app/components/composer-editor.gjs+++ b/frontend/discourse/app/components/composer-editor.gjs@@ -992,10 +992,8 @@ export default class ComposerEditor extends Component {}get showTranslationEditor() {- if (- !this.siteSettings.content_localization_enabled ||- !this.currentUser.can_localize_content- ) {+ const post = this.get("composer.model.post");+ if (!post?.can_localize_post) {return false;}diff --git a/frontend/discourse/app/components/modal/post-translations.gjs b/frontend/discourse/app/components/modal/post-translations.gjsindex 22e24dd714a..f3048976ee9 100644--- a/frontend/discourse/app/components/modal/post-translations.gjs+++ b/frontend/discourse/app/components/modal/post-translations.gjs@@ -16,8 +16,6 @@ import { i18n } from "discourse-i18n";export default class PostTranslationsModal extends Component {@service composer;- @service currentUser;- @service siteSettings;@service dialog;@tracked postLocalizations = null;@@ -43,13 +41,13 @@ export default class PostTranslationsModal extends Component {}}+ get canLocalizePost() {+ return this.args.model.post.can_localize_post;+ }+@actionasync editLocalization(locale) {- if (- !this.currentUser ||- !this.siteSettings.content_localization_enabled ||- !this.currentUser.can_localize_content- ) {+ if (!this.canLocalizePost) {return;}@@ -80,6 +78,10 @@ export default class PostTranslationsModal extends Component {@actionasync deleteLocalization(locale) {+ if (!this.canLocalizePost) {+ return;+ }+try {await PostLocalization.destroy(this.args.model.post.id, locale);@@ -129,20 +131,24 @@ export default class PostTranslationsModal extends Component {class="post-translations-modal__locale">{{localization.locale}}</td><td class="post-translations-modal__edit-action">- <DButton- class="btn-primary btn-transparent"- @icon="pencil"- @label="post.localizations.table.edit"- @action={{fn this.editLocalization localization}}- />+ {{#if this.canLocalizePost}}+ <DButton+ class="btn-primary btn-transparent"+ @icon="pencil"+ @label="post.localizations.table.edit"+ @action={{fn this.editLocalization localization}}+ />+ {{/if}}</td><td class="post-translations-modal__delete-action">- <DButton- class="btn-danger btn-transparent"- @icon="trash-can"- @label="post.localizations.table.delete"- @action={{fn this.delete localization.locale}}- />+ {{#if this.canLocalizePost}}+ <DButton+ class="btn-danger btn-transparent"+ @icon="trash-can"+ @label="post.localizations.table.delete"+ @action={{fn this.delete localization.locale}}+ />+ {{/if}}</td></tr>{{/each}}diff --git a/frontend/discourse/app/components/post/menu/buttons/add-translation.gjs b/frontend/discourse/app/components/post/menu/buttons/add-translation.gjsindex d26d2dc2833..ae60fc1dcf7 100644--- a/frontend/discourse/app/components/post/menu/buttons/add-translation.gjs+++ b/frontend/discourse/app/components/post/menu/buttons/add-translation.gjs@@ -16,17 +16,11 @@ import { i18n } from "discourse-i18n";export default class PostMenuAddTranslationButton extends Component {@service composer;@service modal;- @service currentUser;- @service siteSettings;@tracked showComposer = false;get showTranslationButton() {- return (- this.currentUser &&- this.siteSettings.content_localization_enabled &&- this.currentUser.can_localize_content- );+ return this.args.post.can_localize_post;}get addTranslationsLabel() {@@ -52,11 +46,7 @@ export default class PostMenuAddTranslationButton extends Component {@actionasync addTranslation() {- if (- !this.currentUser ||- !this.siteSettings.content_localization_enabled ||- !this.currentUser.can_localize_content- ) {+ if (!this.args.post.can_localize_post) {return;}diff --git a/frontend/discourse/app/controllers/topic.js b/frontend/discourse/app/controllers/topic.jsindex 95a849adafa..5febc87e169 100644--- a/frontend/discourse/app/controllers/topic.js+++ b/frontend/discourse/app/controllers/topic.js@@ -386,7 +386,7 @@ export default class TopicController extends Controller {async editTopic(event) {event?.preventDefault();const canEditTitle = this.get("model.details.can_edit");- const canLocalize = this.currentUser?.can_localize_content;+ const canLocalize = this.get("model.can_localize_topic");if (!canEditTitle && !canLocalize) {return;@@ -401,12 +401,13 @@ export default class TopicController extends Controller {return;}+ const topic = this.model;+ const firstPost = await topic.firstPost();+if (canEditTitle && !canLocalize) {return this._openComposerForEdit(topic, firstPost);}- const topic = this.model;- const firstPost = await topic.firstPost();if (titleLocalized && !canEditTitle) {return this._openComposerForEditTranslation(topic, firstPost);}diff --git a/frontend/discourse/tests/integration/components/post/menu/buttons/add-translation-test.gjs b/frontend/discourse/tests/integration/components/post/menu/buttons/add-translation-test.gjsindex d94c68054c4..52ab77bf1b5 100644--- a/frontend/discourse/tests/integration/components/post/menu/buttons/add-translation-test.gjs+++ b/frontend/discourse/tests/integration/components/post/menu/buttons/add-translation-test.gjs@@ -17,6 +17,7 @@ module(id: 1,topic_id: 1,post_localizations_count: 0,+ can_localize_post: true,});this.post = post;@@ -25,7 +26,6 @@ module(// positive case for menu to always showthis.siteSettings.content_localization_enabled = true;this.currentUser.admin = true;- this.currentUser.can_localize_content = true;pretender.get("/posts/1.json", () => {return [200, {}, { raw: "Test post content" }];@@ -43,8 +43,8 @@ module(.exists("Menu button rendered when user can localize");});- test("does not render when user cannot localize", async function (assert) {- this.currentUser.can_localize_content = false;+ test("does not render when post cannot be localized", async function (assert) {+ this.post.can_localize_post = false;await render(<template><AddTranslation @post={{this.post}} /></template>);@@ -53,16 +53,6 @@ module(.doesNotExist("No menu when user cannot localize");});- test("does not render when localization is disabled", async function (assert) {- this.siteSettings.content_localization_enabled = false;-- await render(<template><AddTranslation @post={{this.post}} /></template>);-- assert- .dom(".update-translations-menu")- .doesNotExist("No menu when localization disabled");- });-test("shows both view and add buttons when localizations exist", async function (assert) {this.post.post_localizations_count = 2;diff --git a/lib/guardian/localization_guardian.rb b/lib/guardian/localization_guardian.rbindex 00fa6b44c0a..4b695e44d08 100644--- a/lib/guardian/localization_guardian.rb+++ b/lib/guardian/localization_guardian.rb@@ -1,8 +1,34 @@# frozen_string_literal: truemodule LocalizationGuardian+ # Users that pass this guard are allowed to localize all content site-widedef can_localize_content?return false if !SiteSetting.content_localization_enabled+ return false if anonymous?+@user.in_any_groups?(SiteSetting.content_localization_allowed_groups_map)end++ def can_localize_post?(post_or_post_id)+ return false if !SiteSetting.content_localization_enabled+ return false if anonymous?++ return true if @user.in_any_groups?(SiteSetting.content_localization_allowed_groups_map)+ return false if !SiteSetting.content_localization_allow_author_localization++ post = post_or_post_id.is_a?(Post) ? post_or_post_id : Post.find_by(id: post_or_post_id)+ post&.user_id == @user.id+ end++ def can_localize_topic?(topic_or_topic_id)+ return false if !SiteSetting.content_localization_enabled+ return false if anonymous?++ return true if @user.in_any_groups?(SiteSetting.content_localization_allowed_groups_map)+ return false if !SiteSetting.content_localization_allow_author_localization++ topic =+ topic_or_topic_id.is_a?(Topic) ? topic_or_topic_id : Topic.find_by(id: topic_or_topic_id)+ topic&.user_id == @user.id+ endenddiff --git a/lib/post_localization_creator.rb b/lib/post_localization_creator.rbindex db5005d2106..6bdb8e8c046 100644--- a/lib/post_localization_creator.rb+++ b/lib/post_localization_creator.rb@@ -2,9 +2,9 @@class PostLocalizationCreatordef self.create(post_id:, locale:, raw:, user:)- Guardian.new(user).ensure_can_localize_content!-post = Post.find_by(id: post_id)++ Guardian.new(user).ensure_can_localize_post!(post)raise Discourse::NotFound unless postlocalization =diff --git a/lib/post_localization_destroyer.rb b/lib/post_localization_destroyer.rbindex aaef3663f6d..01cef3b89ec 100644--- a/lib/post_localization_destroyer.rb+++ b/lib/post_localization_destroyer.rb@@ -2,14 +2,14 @@class PostLocalizationDestroyerdef self.destroy(post_id:, locale:, acting_user:)- Guardian.new(acting_user).ensure_can_localize_content!+ Guardian.new(acting_user).ensure_can_localize_post!(post_id)- localization = PostLocalization.find_by(post_id: post_id, locale: locale)+ localization = PostLocalization.find_by(post_id:, locale:)raise Discourse::NotFound unless localization- post = localization.postlocalization.destroy!- post.publish_change_to_clients! :revised+ post = localization.post+ post.publish_change_to_clients! :revised if postendenddiff --git a/lib/post_localization_updater.rb b/lib/post_localization_updater.rbindex 383a42592ec..026543e533b 100644--- a/lib/post_localization_updater.rb+++ b/lib/post_localization_updater.rb@@ -2,14 +2,15 @@class PostLocalizationUpdaterdef self.update(post_id:, locale:, raw:, user:)- Guardian.new(user).ensure_can_localize_content!+ Guardian.new(user).ensure_can_localize_post!(post_id)localization = PostLocalization.find_by(post_id: post_id, locale: locale)raise Discourse::NotFound unless localization-- post = Post.find_by(id: post_id)+ post = localization.postraise Discourse::NotFound unless post+ return localization if localization.raw == raw+localization.raw = rawlocalization.cooked = PrettyText.cook(raw)localization.localizer_user_id = user.iddiff --git a/lib/topic_localization_creator.rb b/lib/topic_localization_creator.rbindex e9535803d58..ae6e7ae0237 100644--- a/lib/topic_localization_creator.rb+++ b/lib/topic_localization_creator.rb@@ -2,13 +2,10 @@class TopicLocalizationCreatordef self.create(topic_id:, locale:, title:, user:)- Guardian.new(user).ensure_can_localize_content!-- topic = Topic.find_by(id: topic_id)- raise Discourse::NotFound unless topic+ Guardian.new(user).ensure_can_localize_topic!(topic_id)TopicLocalization.create!(- topic_id: topic.id,+ topic_id: topic_id,locale: locale,title: title,fancy_title: Topic.fancy_title(title),diff --git a/lib/topic_localization_destroyer.rb b/lib/topic_localization_destroyer.rbindex 42f66c9a555..8a01a40641c 100644--- a/lib/topic_localization_destroyer.rb+++ b/lib/topic_localization_destroyer.rb@@ -2,7 +2,7 @@class TopicLocalizationDestroyerdef self.destroy(topic_id:, locale:, acting_user:)- Guardian.new(acting_user).ensure_can_localize_content!+ Guardian.new(acting_user).ensure_can_localize_topic!(topic_id)localization = TopicLocalization.find_by(topic_id: topic_id, locale: locale)raise Discourse::NotFound unless localizationdiff --git a/lib/topic_localization_updater.rb b/lib/topic_localization_updater.rbindex 29613a2ceef..f0479ed2216 100644--- a/lib/topic_localization_updater.rb+++ b/lib/topic_localization_updater.rb@@ -2,11 +2,13 @@class TopicLocalizationUpdaterdef self.update(topic_id:, locale:, title:, user:)- Guardian.new(user).ensure_can_localize_content!+ Guardian.new(user).ensure_can_localize_topic!(topic_id)localization = TopicLocalization.find_by(topic_id: topic_id, locale: locale)raise Discourse::NotFound unless localization+ return localization if localization.title == title+localization.title = titlelocalization.fancy_title = Topic.fancy_title(title)localization.localizer_user_id = user.iddiff --git a/plugins/discourse-ai/app/controllers/discourse_ai/translation/translation_controller.rb b/plugins/discourse-ai/app/controllers/discourse_ai/translation/translation_controller.rbindex a651d1961d5..5a588c47ef8 100644--- a/plugins/discourse-ai/app/controllers/discourse_ai/translation/translation_controller.rb+++ b/plugins/discourse-ai/app/controllers/discourse_ai/translation/translation_controller.rb@@ -69,9 +69,7 @@ module DiscourseAiprivatedef check_permissions- if !current_user&.in_any_groups?(SiteSetting.content_localization_allowed_groups_map)- raise Discourse::InvalidAccess- end+ guardian.ensure_can_localize_content!enddef rate_limit!diff --git a/plugins/discourse-ai/spec/requests/translation/translation_controller_spec.rb b/plugins/discourse-ai/spec/requests/translation/translation_controller_spec.rbindex 724248a12fa..d77a500fd87 100644--- a/plugins/discourse-ai/spec/requests/translation/translation_controller_spec.rb+++ b/plugins/discourse-ai/spec/requests/translation/translation_controller_spec.rb@@ -13,6 +13,7 @@ describe DiscourseAi::Translation::TranslationController doSiteSetting.ai_translation_enabled = trueSiteSetting.content_localization_supported_locales = "en"SiteSetting.content_localization_allowed_groups = "#{Group::AUTO_GROUPS[:staff]}|#{group.id}"+ SiteSetting.content_localization_enabled = trueenddescribe "#translate" dodiff --git a/plugins/discourse-presence/plugin.rb b/plugins/discourse-presence/plugin.rbindex de872e358a1..6beb6739291 100644--- a/plugins/discourse-presence/plugin.rb+++ b/plugins/discourse-presence/plugin.rb@@ -78,18 +78,23 @@ after_initialize doconfig = PresenceChannel::Config.newconfig.allowed_group_ids = staff_groups+ config.allowed_user_ids = []if SiteSetting.content_localization_enabledconfig.allowed_group_ids += SiteSetting.content_localization_allowed_groups_mapendif topic.private_message?- config.allowed_user_ids = topic.allowed_users.pluck(:id)+ config.allowed_user_ids += topic.allowed_users.pluck(:id)config.allowed_group_ids += topic.allowed_groups.pluck(:group_id)elsif secure_group_ids = topic.secure_group_idsconfig.allowed_group_ids += secure_group_idsend+ if SiteSetting.content_localization_allow_author_localization+ config.allowed_user_ids += [post.user_id]+ end+config.allowed_group_ids.uniq!configdiff --git a/plugins/discourse-presence/spec/integration/presence_spec.rb b/plugins/discourse-presence/spec/integration/presence_spec.rbindex 31389631ce8..74fd8f57694 100644--- a/plugins/discourse-presence/spec/integration/presence_spec.rb+++ b/plugins/discourse-presence/spec/integration/presence_spec.rb@@ -209,62 +209,75 @@ RSpec.describe "discourse-presence" do)end- it "handles permissions for translate channel on public topics" do- post = Fabricate(:post, topic: public_topic, user: user)- SiteSetting.content_localization_enabled = true- SiteSetting.content_localization_allowed_groups = group.id-- c = PresenceChannel.new("/discourse-presence/translate/#{post.id}")- expect(c.config.public).to eq(false)- expect(c.config.allowed_group_ids).to contain_exactly(- Group::AUTO_GROUPS[:admins],- Group::AUTO_GROUPS[:moderators],- group.id,- )- expect(c.config.allowed_user_ids).to eq(nil)- end-- it "handles permissions for translate channel on secure category topics" do- post = Fabricate(:post, topic: private_topic, user: user)- SiteSetting.content_localization_enabled = true-- c = PresenceChannel.new("/discourse-presence/translate/#{post.id}")- expect(c.config.public).to eq(false)- expect(c.config.allowed_group_ids).to contain_exactly(- Group::AUTO_GROUPS[:admins],- Group::AUTO_GROUPS[:moderators],- group.id,- )- expect(c.config.allowed_user_ids).to eq(nil)- end-- it "handles permissions for translate channel on private messages" do- post = Fabricate(:post, topic: private_message, user: user)- SiteSetting.content_localization_enabled = true-- c = PresenceChannel.new("/discourse-presence/translate/#{post.id}")- expect(c.config.public).to eq(false)- expect(c.config.allowed_group_ids).to contain_exactly(- Group::AUTO_GROUPS[:admins],- Group::AUTO_GROUPS[:moderators],- group.id,- )- expect(c.config.allowed_user_ids).to contain_exactly(- *private_message.topic_allowed_users.pluck(:user_id),- )- end-- it "allows only staff for translate channel when content localization is disabled" do- post = Fabricate(:post, topic: public_topic, user: user)- SiteSetting.content_localization_enabled = false-- c = PresenceChannel.new("/discourse-presence/translate/#{post.id}")- expect(c.config.public).to eq(false)- expect(c.config.allowed_group_ids).to contain_exactly(- Group::AUTO_GROUPS[:admins],- Group::AUTO_GROUPS[:moderators],- )- expect(c.config.allowed_user_ids).to eq(nil)+ describe "content_localization_enabled" do+ before { SiteSetting.content_localization_allow_author_localization = false }++ it "handles permissions for translate channel on public topics" do+ post = Fabricate(:post, topic: public_topic, user: user)+ SiteSetting.content_localization_enabled = true+ SiteSetting.content_localization_allowed_groups = group.id++ c = PresenceChannel.new("/discourse-presence/translate/#{post.id}")+ expect(c.config.public).to eq(false)+ expect(c.config.allowed_group_ids).to contain_exactly(+ Group::AUTO_GROUPS[:admins],+ Group::AUTO_GROUPS[:moderators],+ group.id,+ )+ expect(c.config.allowed_user_ids).to eq([])+ end++ it "handles permissions for translate channel on secure category topics" do+ post = Fabricate(:post, topic: private_topic, user: user)+ SiteSetting.content_localization_enabled = true++ c = PresenceChannel.new("/discourse-presence/translate/#{post.id}")+ expect(c.config.public).to eq(false)+ expect(c.config.allowed_group_ids).to contain_exactly(+ Group::AUTO_GROUPS[:admins],+ Group::AUTO_GROUPS[:moderators],+ group.id,+ )+ expect(c.config.allowed_user_ids).to eq([])+ end++ it "handles permissions for translate channel on private messages" do+ post = Fabricate(:post, topic: private_message, user: user)+ SiteSetting.content_localization_enabled = true++ c = PresenceChannel.new("/discourse-presence/translate/#{post.id}")+ expect(c.config.public).to eq(false)+ expect(c.config.allowed_group_ids).to contain_exactly(+ Group::AUTO_GROUPS[:admins],+ Group::AUTO_GROUPS[:moderators],+ group.id,+ )+ expect(c.config.allowed_user_ids).to contain_exactly(+ *private_message.topic_allowed_users.pluck(:user_id),+ )+ end++ it "allows only staff for translate channel when content localization is disabled" do+ post = Fabricate(:post, topic: public_topic, user: user)+ SiteSetting.content_localization_enabled = false++ c = PresenceChannel.new("/discourse-presence/translate/#{post.id}")+ expect(c.config.public).to eq(false)+ expect(c.config.allowed_group_ids).to contain_exactly(+ Group::AUTO_GROUPS[:admins],+ Group::AUTO_GROUPS[:moderators],+ )+ expect(c.config.allowed_user_ids).to eq([])+ end++ it "adds post author to allowed_user_ids for translate channel" do+ post = Fabricate(:post, topic: public_topic, user: user)+ SiteSetting.content_localization_enabled = true+ SiteSetting.content_localization_allow_author_localization = true++ c = PresenceChannel.new("/discourse-presence/translate/#{post.id}")+ expect(c.config.allowed_user_ids).to contain_exactly(user.id)+ endendendenddiff --git a/spec/jobs/process_localized_cooked_spec.rb b/spec/jobs/process_localized_cooked_spec.rbindex 102b157151e..b4f367566ed 100644--- a/spec/jobs/process_localized_cooked_spec.rb+++ b/spec/jobs/process_localized_cooked_spec.rb@@ -56,7 +56,7 @@ describe Jobs::ProcessLocalizedCooked do}end- it "publishes MessageBus notification when cooked changes" do+ it "publishes MessageBus notification" doprocessed_html = "<p>これはテスト投稿です。</p><div class='onebox'>Processed</div>"LocalizedCookedPostProcessor.any_instance.expects(:html).returns(processed_html)@@ -70,17 +70,6 @@ describe Jobs::ProcessLocalizedCooked doexpect(messages.first.data[:id]).to eq(post.id)end- it "does not publish MessageBus notification when cooked unchanged" do- LocalizedCookedPostProcessor.any_instance.expects(:html).returns(post_localization.cooked)-- messages =- MessageBus.track_publish("/topic/#{post.topic_id}") do- job.execute(post_localization_id: post_localization.id)- end-- expect(messages.length).to eq(0)- end-it "processes oneboxes and images" dostub_image_sizeonebox_html = <<~HTMLdiff --git a/spec/lib/guardian/localization_guardian_spec.rb b/spec/lib/guardian/localization_guardian_spec.rbnew file mode 100644index 00000000000..0cda32f2974--- /dev/null+++ b/spec/lib/guardian/localization_guardian_spec.rb@@ -0,0 +1,148 @@+# frozen_string_literal: true++describe LocalizationGuardian do+ fab!(:user)+ fab!(:admin)+ fab!(:moderator)+ fab!(:post) { Fabricate(:post, user: user) }+ fab!(:other_user_post, :post)+ fab!(:topic) { Fabricate(:topic, user: user) }+ fab!(:other_user_topic, :topic)++ before { SiteSetting.content_localization_enabled = true }++ describe "#can_localize_content?" do+ it "returns false when content localization is disabled" do+ SiteSetting.content_localization_enabled = false+ expect(Guardian.new(admin).can_localize_content?).to eq(false)+ end++ it "returns true for users in allowed groups" do+ SiteSetting.content_localization_allowed_groups = "#{Group::AUTO_GROUPS[:admins]}"+ expect(Guardian.new(admin).can_localize_content?).to eq(true)+ end++ it "returns false for users not in allowed groups" do+ SiteSetting.content_localization_allowed_groups = "#{Group::AUTO_GROUPS[:admins]}"+ expect(Guardian.new(user).can_localize_content?).to eq(false)+ end+ end++ describe "#can_localize_post?" do+ it "returns false when content localization is disabled" do+ SiteSetting.content_localization_enabled = false+ expect(Guardian.new(user).can_localize_post?(post)).to eq(false)+ end++ context "when user is in allowed groups" do+ before { SiteSetting.content_localization_allowed_groups = "#{Group::AUTO_GROUPS[:admins]}" }++ it "returns true for any post" do+ expect(Guardian.new(admin).can_localize_post?(post)).to eq(true)+ expect(Guardian.new(admin).can_localize_post?(post.id)).to eq(true)+ expect(Guardian.new(admin).can_localize_post?(other_user_post)).to eq(true)+ expect(Guardian.new(admin).can_localize_post?(other_user_post.id)).to eq(true)+ end+ end++ context "when author localization is enabled" do+ before do+ SiteSetting.content_localization_allowed_groups = "#{Group::AUTO_GROUPS[:admins]}"+ SiteSetting.content_localization_allow_author_localization = true+ end++ it "returns true when user is the post author" do+ expect(Guardian.new(user).can_localize_post?(post)).to eq(true)+ expect(Guardian.new(user).can_localize_post?(post.id)).to eq(true)+ end++ it "returns false when user is not the post author" do+ expect(Guardian.new(user).can_localize_post?(other_user_post)).to eq(false)+ expect(Guardian.new(user).can_localize_post?(other_user_post.id)).to eq(false)+ end++ it "returns true for users in allowed groups regardless of authorship" do+ expect(Guardian.new(admin).can_localize_post?(post)).to eq(true)+ expect(Guardian.new(admin).can_localize_post?(post.id)).to eq(true)+ expect(Guardian.new(admin).can_localize_post?(other_user_post)).to eq(true)+ expect(Guardian.new(admin).can_localize_post?(other_user_post.id)).to eq(true)+ end+ end++ context "when author localization is disabled" do+ before do+ SiteSetting.content_localization_allowed_groups = "#{Group::AUTO_GROUPS[:admins]}"+ SiteSetting.content_localization_allow_author_localization = false+ end++ it "returns false for post authors not in allowed groups" do+ expect(Guardian.new(user).can_localize_post?(post)).to eq(false)+ expect(Guardian.new(user).can_localize_post?(post.id)).to eq(false)+ end++ it "returns true for users in allowed groups" do+ expect(Guardian.new(admin).can_localize_post?(post)).to eq(true)+ expect(Guardian.new(admin).can_localize_post?(post.id)).to eq(true)+ end+ end+ end++ describe "#can_localize_topic?" do+ it "returns false when content localization is disabled" do+ SiteSetting.content_localization_enabled = false+ expect(Guardian.new(user).can_localize_topic?(topic)).to eq(false)+ end++ context "when user is in allowed groups" do+ before { SiteSetting.content_localization_allowed_groups = "#{Group::AUTO_GROUPS[:admins]}" }++ it "returns true for any topic" do+ expect(Guardian.new(admin).can_localize_topic?(topic)).to eq(true)+ expect(Guardian.new(admin).can_localize_topic?(topic.id)).to eq(true)+ expect(Guardian.new(admin).can_localize_topic?(other_user_topic)).to eq(true)+ expect(Guardian.new(admin).can_localize_topic?(other_user_topic.id)).to eq(true)+ end+ end++ context "when author localization is enabled" do+ before do+ SiteSetting.content_localization_allowed_groups = "#{Group::AUTO_GROUPS[:admins]}"+ SiteSetting.content_localization_allow_author_localization = true+ end++ it "returns true when user is the topic author" do+ expect(Guardian.new(user).can_localize_topic?(topic)).to eq(true)+ expect(Guardian.new(user).can_localize_topic?(topic.id)).to eq(true)+ end++ it "returns false when user is not the topic author" do+ expect(Guardian.new(user).can_localize_topic?(other_user_topic)).to eq(false)+ expect(Guardian.new(user).can_localize_topic?(other_user_topic.id)).to eq(false)+ end++ it "returns true for users in allowed groups regardless of authorship" do+ expect(Guardian.new(admin).can_localize_topic?(topic)).to eq(true)+ expect(Guardian.new(admin).can_localize_topic?(topic.id)).to eq(true)+ expect(Guardian.new(admin).can_localize_topic?(other_user_topic)).to eq(true)+ expect(Guardian.new(admin).can_localize_topic?(other_user_topic.id)).to eq(true)+ end+ end++ context "when author localization is disabled" do+ before do+ SiteSetting.content_localization_allowed_groups = "#{Group::AUTO_GROUPS[:admins]}"+ SiteSetting.content_localization_allow_author_localization = false+ end++ it "returns false for topic authors not in allowed groups" do+ expect(Guardian.new(user).can_localize_topic?(topic)).to eq(false)+ expect(Guardian.new(user).can_localize_topic?(topic.id)).to eq(false)+ end++ it "returns true for users in allowed groups" do+ expect(Guardian.new(admin).can_localize_topic?(topic)).to eq(true)+ expect(Guardian.new(admin).can_localize_topic?(topic.id)).to eq(true)+ end+ end+ end+enddiff --git a/spec/lib/post_localization_creator_spec.rb b/spec/lib/post_localization_creator_spec.rbindex 35d9a7964a9..449937c8e4c 100644--- a/spec/lib/post_localization_creator_spec.rb+++ b/spec/lib/post_localization_creator_spec.rb@@ -37,4 +37,29 @@ describe PostLocalizationCreator doDiscourse::NotFound,)end++ context "with author localization" do+ fab!(:author, :user)+ fab!(:author_post) { Fabricate(:post, user: author) }+ fab!(:other_post, :post)++ before { SiteSetting.content_localization_allow_author_localization = true }++ it "allows post author to create localization for their own post" do+ localization = described_class.create(post_id: author_post.id, locale:, raw:, user: author)++ expect(localization).to have_attributes(+ post_id: author_post.id,+ locale:,+ raw:,+ localizer_user_id: author.id,+ )+ end++ it "raises permission error if user is not the post author" do+ expect {+ described_class.create(post_id: other_post.id, locale:, raw:, user: author)+ }.to raise_error(Discourse::InvalidAccess)+ end+ endenddiff --git a/spec/lib/post_localization_destroyer_spec.rb b/spec/lib/post_localization_destroyer_spec.rbindex d0d45ab157e..93efb07b7b6 100644--- a/spec/lib/post_localization_destroyer_spec.rb+++ b/spec/lib/post_localization_destroyer_spec.rb@@ -6,7 +6,7 @@ describe PostLocalizationDestroyer dofab!(:group)fab!(:localization) { Fabricate(:post_localization, post:, locale: "ja") }- let(:locale) { "ja" }+ fab!(:locale) { "ja" }before doSiteSetting.content_localization_enabled = true@@ -37,4 +37,25 @@ describe PostLocalizationDestroyer doexpect(messages.first.data[:type]).to eq(:revised)expect(messages.first.data[:id]).to eq(post.id)end++ context "with author localization" do+ fab!(:author, :user)+ fab!(:author_post) { Fabricate(:post, user: author) }+ fab!(:other_post, :post)+ fab!(:post_localization) { Fabricate(:post_localization, post: author_post, locale:) }++ before { SiteSetting.content_localization_allow_author_localization = true }++ it "allows post author to create localization for their own post" do+ localization = described_class.destroy(post_id: author_post.id, locale:, acting_user: author)++ expect(localization).to be_nil+ end++ it "raises permission error if user is not the post author" do+ expect {+ described_class.destroy(post_id: other_post.id, locale:, acting_user: author)+ }.to raise_error(Discourse::InvalidAccess)+ end+ endenddiff --git a/spec/lib/post_localization_updater_spec.rb b/spec/lib/post_localization_updater_spec.rbindex 6f24aee85d7..c9fa1328ac7 100644--- a/spec/lib/post_localization_updater_spec.rb+++ b/spec/lib/post_localization_updater_spec.rb@@ -8,7 +8,7 @@ describe PostLocalizationUpdater doFabricate(:post_localization, post: post, locale: "ja", raw: "古いバージョン")end- let(:locale) { "ja" }+ fab!(:locale) { "ja" }let(:new_raw) { "新しいバージョンです" }before do@@ -29,6 +29,14 @@ describe PostLocalizationUpdater do)end+ it "returns the localization unchanged if the raw content is the same" do+ localization =+ described_class.update(post_id: post.id, locale:, raw: post_localization.raw, user:)++ expect(localization.id).to eq(post_localization.id)+ expect(localization.localizer_user_id).not_to eq(user.id)+ end+it "enqueues ProcessLocalizedCook job" doloc = described_class.update(post_id: post.id, locale: locale, raw: new_raw, user: user)@@ -46,4 +54,31 @@ describe PostLocalizationUpdater dodescribed_class.update(post_id: -1, locale: locale, raw: new_raw, user: user)}.to raise_error(Discourse::NotFound)end++ context "with author localization" do+ fab!(:author, :user)+ fab!(:author_post) { Fabricate(:post, user: author) }+ fab!(:other_post, :post)+ fab!(:post_localization) { Fabricate(:post_localization, post: author_post, locale:) }++ before { SiteSetting.content_localization_allow_author_localization = true }++ it "allows post author to create localization for their own post" do+ localization =+ described_class.update(post_id: author_post.id, locale:, raw: new_raw, user: author)++ expect(localization).to have_attributes(+ post_id: author_post.id,+ locale:,+ raw: new_raw,+ localizer_user_id: author.id,+ )+ end++ it "raises permission error if user is not the post author" do+ expect {+ described_class.update(post_id: other_post.id, locale:, raw: new_raw, user: author)+ }.to raise_error(Discourse::InvalidAccess)+ end+ endenddiff --git a/spec/lib/topic_localization_creator_spec.rb b/spec/lib/topic_localization_creator_spec.rbindex 2e9b6a73ea8..9f2e09a05de 100644--- a/spec/lib/topic_localization_creator_spec.rb+++ b/spec/lib/topic_localization_creator_spec.rb@@ -26,9 +26,39 @@ describe TopicLocalizationCreator do)end- it "raises not found if the topic is missing" do- expect { described_class.create(topic_id: -1, locale:, title:, user:) }.to raise_error(- Discourse::NotFound,+ it "raises permission error if user not in allowed groups" do+ group.remove(user)+ expect { described_class.create(topic_id: topic.id, locale:, title:, user:) }.to raise_error(+ Discourse::InvalidAccess,)end++ context "with author localization" do+ fab!(:author, :user)+ fab!(:author_topic) { Fabricate(:topic, user: author) }+ fab!(:other_topic, :topic)++ before do+ SiteSetting.content_localization_allow_author_localization = true+ group.remove(author)+ end++ it "allows topic author to create localization for their own topic" do+ localization =+ described_class.create(topic_id: author_topic.id, locale:, title:, user: author)++ expect(localization).to have_attributes(+ topic_id: author_topic.id,+ locale:,+ title:,+ localizer_user_id: author.id,+ )+ end++ it "raises permission error if user is not the topic author" do+ expect {+ described_class.create(topic_id: other_topic.id, locale:, title:, user: author)+ }.to raise_error(Discourse::InvalidAccess)+ end+ endenddiff --git a/spec/lib/topic_localization_destroyer_spec.rb b/spec/lib/topic_localization_destroyer_spec.rbindex 94ce7278ae7..baed59df6ad 100644--- a/spec/lib/topic_localization_destroyer_spec.rb+++ b/spec/lib/topic_localization_destroyer_spec.rb@@ -26,4 +26,37 @@ describe TopicLocalizationDestroyer dodescribed_class.destroy(topic_id: topic.id, locale: "nope", acting_user: user)}.to raise_error(Discourse::NotFound)end++ it "raises permission error if user not in allowed groups" do+ group.remove(user)+ expect {+ described_class.destroy(topic_id: topic.id, locale: locale, acting_user: user)+ }.to raise_error(Discourse::InvalidAccess)+ end++ context "with author localization" do+ fab!(:author, :user)+ fab!(:author_topic) { Fabricate(:topic, user: author) }+ fab!(:author_localization) { Fabricate(:topic_localization, topic: author_topic, locale: "ja") }++ before do+ SiteSetting.content_localization_allow_author_localization = true+ group.remove(author)+ end++ it "allows topic author to destroy localization for their own topic" do+ expect {+ described_class.destroy(topic_id: author_topic.id, locale: "ja", acting_user: author)+ }.to change { TopicLocalization.count }.by(-1)+ expect { TopicLocalization.find(author_localization.id) }.to raise_error(+ ActiveRecord::RecordNotFound,+ )+ end++ it "raises permission error if user is not the topic author" do+ expect {+ described_class.destroy(topic_id: topic.id, locale: locale, acting_user: author)+ }.to raise_error(Discourse::InvalidAccess)+ end+ endenddiff --git a/spec/lib/topic_localization_updater_spec.rb b/spec/lib/topic_localization_updater_spec.rbindex b674f568ac5..763fba19838 100644--- a/spec/lib/topic_localization_updater_spec.rb+++ b/spec/lib/topic_localization_updater_spec.rb@@ -28,6 +28,14 @@ describe TopicLocalizationUpdater do)end+ it "returns the localization unchanged if the title is the same" do+ localization =+ described_class.update(topic_id: topic.id, locale:, title: topic_localization.title, user:)++ expect(localization.id).to eq(topic_localization.id)+ expect(localization.localizer_user_id).not_to eq(user.id)+ end+it "raises not found if the localization is missing" doexpect {described_class.update(topic_id: topic.id, locale: "nope", title: new_title, user: user)@@ -39,4 +47,46 @@ describe TopicLocalizationUpdater dodescribed_class.update(topic_id: -1, locale: locale, title: new_title, user: user)}.to raise_error(Discourse::NotFound)end++ it "raises permission error if user not in allowed groups" do+ group.remove(user)+ expect {+ described_class.update(topic_id: topic.id, locale: locale, title: new_title, user: user)+ }.to raise_error(Discourse::InvalidAccess)+ end++ context "with author localization" do+ fab!(:author, :user)+ fab!(:author_topic) { Fabricate(:topic, user: author) }+ fab!(:author_topic_localization) do+ Fabricate(:topic_localization, topic: author_topic, locale: "ja", title: "古いバージョン")+ end++ before do+ SiteSetting.content_localization_allow_author_localization = true+ group.remove(author)+ end++ it "allows topic author to update localization for their own topic" do+ localization =+ described_class.update(+ topic_id: author_topic.id,+ locale: "ja",+ title: new_title,+ user: author,+ )++ expect(localization).to have_attributes(+ title: new_title,+ fancy_title: Topic.fancy_title(new_title),+ localizer_user_id: author.id,+ )+ end++ it "raises permission error if user is not the topic author" do+ expect {+ described_class.update(topic_id: topic.id, locale: locale, title: new_title, user: author)+ }.to raise_error(Discourse::InvalidAccess)+ end+ endenddiff --git a/spec/requests/post_localizations_controller_spec.rb b/spec/requests/post_localizations_controller_spec.rbindex 28d1323430f..400a2fa3101 100644--- a/spec/requests/post_localizations_controller_spec.rb+++ b/spec/requests/post_localizations_controller_spec.rb@@ -15,6 +15,56 @@ describe PostLocalizationsController dosign_in(user)end+ describe "#show" do+ it "allows users in allowed groups to view localizations" do+ Fabricate(:post_localization, post: post_record, locale:)++ get "/post_localizations/#{post_record.id}.json"++ expect(response.status).to eq(200)+ json = response.parsed_body+ expect(json["post_localizations"].length).to eq(1)+ end++ it "denies users not in allowed groups" do+ group.remove(user)++ get "/post_localizations/#{post_record.id}.json"++ expect(response.status).to eq(403)+ end++ context "with author localization enabled" do+ fab!(:author, :user)+ fab!(:author_post) { Fabricate(:post, user: author) }++ before do+ SiteSetting.content_localization_allow_author_localization = true+ group.remove(author)+ end++ it "allows post authors to view localizations for their own posts" do+ Fabricate(:post_localization, post: author_post, locale:)+ sign_in(author)++ get "/post_localizations/#{author_post.id}.json"++ expect(response.status).to eq(200)+ json = response.parsed_body+ expect(json["post_localizations"].length).to eq(1)+ end++ it "denies post authors from viewing localizations for others' posts" do+ Fabricate(:post_localization, post: post_record, locale:)+ sign_in(author)++ get "/post_localizations/#{post_record.id}.json"++ expect(response.status).to eq(403)+ end+ end+ end+describe "#create_or_update" docontext "when localization does not exist" doit "creates a new localization" do@@ -22,16 +72,16 @@ describe PostLocalizationsController dopost "/post_localizations/create_or_update.json",params: {post_id: post_record.id,- locale: locale,- raw: raw,+ locale:,+ raw:,}}.to change { PostLocalization.count }.by(1)expect(response.status).to eq(201)localization = PostLocalization.lastexpect(localization).to have_attributes(- locale: locale,- raw: raw,+ locale:,+ raw:,post_id: post_record.id,post_version: post_record.version,localizer_user_id: user.id,@@ -41,7 +91,7 @@ describe PostLocalizationsController docontext "when localization already exists" doit "updates the existing localization" do- localization = Fabricate(:post_localization, post: post_record, locale: locale, raw: "古い翻訳")+ localization = Fabricate(:post_localization, post: post_record, locale:, raw: "古い翻訳")new_user = Fabricate(:user, groups: [group])sign_in(new_user)@@ -49,8 +99,8 @@ describe PostLocalizationsController dopost "/post_localizations/create_or_update.json",params: {post_id: post_record.id,- locale: locale,- raw: raw,+ locale:,+ raw:,}}.not_to change { PostLocalization.count }@@ -67,20 +117,15 @@ describe PostLocalizationsController dopost "/post_localizations/create_or_update.json",params: {post_id: post_record.id,- locale: locale,- raw: raw,+ locale:,+ raw:,}expect(response.status).to eq(403)endit "returns not found if post does not exist" do- post "/post_localizations/create_or_update.json",- params: {- post_id: -1,- locale: locale,- raw: raw,- }+ post "/post_localizations/create_or_update.json", params: { post_id: -1, locale:, raw: }expect(response.status).to eq(404)end@@ -88,14 +133,10 @@ describe PostLocalizationsController dodescribe "#destroy" doit "destroys the localization" do- Fabricate(:post_localization, post: post_record, locale: locale)+ Fabricate(:post_localization, post: post_record, locale:)expect {- delete "/post_localizations/destroy.json",- params: {- post_id: post_record.id,- locale: locale,- }+ delete "/post_localizations/destroy.json", params: { post_id: post_record.id, locale: }}.to change { PostLocalization.count }.by(-1)expect(response.status).to eq(204)@@ -107,4 +148,135 @@ describe PostLocalizationsController doexpect(response.status).to eq(404)endend++ context "with author localization" do+ fab!(:author, :user)+ fab!(:author_post) { Fabricate(:post, user: author, version: 100) }+ fab!(:other_user, :user)++ before { group.remove(author) }++ describe "#create_or_update for post authors" do+ it "allows post author to create localization on their own post" do+ sign_in(author)++ SiteSetting.content_localization_allow_author_localization = false+ expect {+ post "/post_localizations/create_or_update.json",+ params: {+ post_id: author_post.id,+ locale:,+ raw:,+ }+ }.not_to change { PostLocalization.count }++ SiteSetting.content_localization_allow_author_localization = true+ expect {+ post "/post_localizations/create_or_update.json",+ params: {+ post_id: author_post.id,+ locale:,+ raw:,+ }+ }.to change { PostLocalization.count }.by(1)+ expect(response.status).to eq(201)+ localization = PostLocalization.last+ expect(localization.localizer_user_id).to eq(author.id)+ end++ it "denies post author from creating localization on others' posts" do+ SiteSetting.content_localization_allow_author_localization = true+ sign_in(author)++ expect {+ post "/post_localizations/create_or_update.json",+ params: {+ post_id: post_record.id,+ locale:,+ raw:,+ }+ }.not_to change { PostLocalization.count }++ expect(response.status).to eq(403)+ end++ it "allows author to update their own localization" do+ localization =+ Fabricate(+ :post_localization,+ post: author_post,+ locale:,+ raw: "古い翻訳",+ localizer_user_id: author.id,+ )+ sign_in(author)++ SiteSetting.content_localization_allow_author_localization = false+ expect {+ post "/post_localizations/create_or_update.json",+ params: {+ post_id: author_post.id,+ locale:,+ raw:,+ }+ }.not_to change { localization.reload.raw }++ SiteSetting.content_localization_allow_author_localization = true+ post "/post_localizations/create_or_update.json",+ params: {+ post_id: author_post.id,+ locale:,+ raw:,+ }+ expect(response.status).to eq(200)+ localization.reload+ expect(localization.raw).to eq(raw)+ end+ end++ describe "#destroy for post authors" do+ it "allows author to delete their own localization" do+ Fabricate(:post_localization, post: author_post, locale:, localizer_user_id: author.id)+ sign_in(author)++ SiteSetting.content_localization_allow_author_localization = false+ expect {+ delete "/post_localizations/destroy.json", params: { post_id: author_post.id, locale: }+ }.not_to change { PostLocalization.count }++ SiteSetting.content_localization_allow_author_localization = true+ expect {+ delete "/post_localizations/destroy.json", params: { post_id: author_post.id, locale: }+ }.to change { PostLocalization.count }.by(-1)++ expect(response.status).to eq(204)+ end+ end++ it "allows users in allowed groups full access regardless of author setting" do+ localization =+ Fabricate(:post_localization, post: author_post, locale:, localizer_user_id: author.id)+ sign_in(user)++ SiteSetting.content_localization_allow_author_localization = false+ expect {+ post "/post_localizations/create_or_update.json",+ params: {+ post_id: author_post.id,+ locale:,+ raw: "1",+ }+ }.to change { localization.reload.raw }++ SiteSetting.content_localization_allow_author_localization = true+ expect {+ post "/post_localizations/create_or_update.json",+ params: {+ post_id: author_post.id,+ locale:,+ raw: "2",+ }+ }.to change { localization.reload.raw }+ end+ endenddiff --git a/spec/requests/topic_localizations_controller_spec.rb b/spec/requests/topic_localizations_controller_spec.rbindex 4a49230b4b1..8141336d4b3 100644--- a/spec/requests/topic_localizations_controller_spec.rb+++ b/spec/requests/topic_localizations_controller_spec.rb@@ -74,11 +74,6 @@ describe TopicLocalizationsController do}.not_to change { TopicLocalization.count }expect(response.status).to eq(403)end-- it "returns not found if topic does not exist" do- post "/topic_localizations/create_or_update.json", params: { topic_id: -1, locale:, title: }- expect(response.status).to eq(404)- endenddescribe "#destroy" do@@ -106,4 +101,117 @@ describe TopicLocalizationsController doexpect(response.status).to eq(404)endend++ context "with author localization" do+ fab!(:author, :user)+ fab!(:author_topic) { Fabricate(:topic, user: author) }+ fab!(:other_user, :user)++ before { group.remove(author) }++ describe "#create_or_update for topic authors" do+ it "allows topic author to create localization on their own topic" do+ sign_in(author)++ SiteSetting.content_localization_allow_author_localization = false+ expect {+ post "/topic_localizations/create_or_update.json",+ params: {+ topic_id: author_topic.id,+ locale:,+ title:,+ }+ }.not_to change { TopicLocalization.count }++ SiteSetting.content_localization_allow_author_localization = true+ expect {+ post "/topic_localizations/create_or_update.json",+ params: {+ topic_id: author_topic.id,+ locale:,+ title:,+ }+ }.to change { TopicLocalization.count }.by(1)+ expect(response.status).to eq(201)+ localization = TopicLocalization.last+ expect(localization.localizer_user_id).to eq(author.id)+ end++ it "denies topic author from creating localization on others' topics" do+ SiteSetting.content_localization_allow_author_localization = true+ sign_in(author)++ expect {+ post "/topic_localizations/create_or_update.json",+ params: {+ topic_id: topic.id,+ locale:,+ title:,+ }+ }.not_to change { TopicLocalization.count }+ expect(response.status).to eq(403)+ end+ end++ describe "#create_or_update updates for topic authors" do+ it "allows topic author to update localization on their own topic" do+ Fabricate(:topic_localization, topic: author_topic, locale:, title: "Old title")+ SiteSetting.content_localization_allow_author_localization = true+ sign_in(author)++ expect {+ post "/topic_localizations/create_or_update.json",+ params: {+ topic_id: author_topic.id,+ locale:,+ title:,+ }+ }.not_to change { TopicLocalization.count }+ expect(response.status).to eq(200)+ localization = TopicLocalization.find_by(topic: author_topic, locale:)+ expect(localization.title).to eq(title)+ expect(localization.localizer_user_id).to eq(author.id)+ end++ it "denies topic author from updating localization on others' topics" do+ Fabricate(:topic_localization, topic: topic, locale:, title: "Old title")+ SiteSetting.content_localization_allow_author_localization = true+ sign_in(author)++ expect {+ post "/topic_localizations/create_or_update.json",+ params: {+ topic_id: topic.id,+ locale:,+ title:,+ }+ }.not_to change { TopicLocalization.count }+ expect(response.status).to eq(403)+ end+ end++ describe "#destroy for topic authors" do+ it "allows topic author to destroy localization on their own topic" do+ Fabricate(:topic_localization, topic: author_topic, locale:, title: "Title")+ SiteSetting.content_localization_allow_author_localization = true+ sign_in(author)++ expect {+ delete "/topic_localizations/destroy.json", params: { topic_id: author_topic.id, locale: }+ }.to change { TopicLocalization.count }.by(-1)+ expect(response.status).to eq(204)+ end++ it "denies topic author from destroying localization on others' topics" do+ Fabricate(:topic_localization, topic: topic, locale:, title: "Title")+ SiteSetting.content_localization_allow_author_localization = true+ sign_in(author)++ expect {+ delete "/topic_localizations/destroy.json", params: { topic_id: topic.id, locale: }+ }.not_to change { TopicLocalization.count }+ expect(response.status).to eq(403)+ end+ end+ endenddiff --git a/spec/serializers/post_serializer_spec.rb b/spec/serializers/post_serializer_spec.rbindex 967baa531a9..7a08d8445a4 100644--- a/spec/serializers/post_serializer_spec.rb+++ b/spec/serializers/post_serializer_spec.rb@@ -835,4 +835,69 @@ RSpec.describe PostSerializer dos = serialized_post(u)s.as_jsonend++ describe "#can_localize_post" do+ fab!(:author, :user)+ fab!(:author_post) { Fabricate(:post, user: author) }+ fab!(:admin)+ fab!(:group)++ before do+ SiteSetting.content_localization_enabled = true+ SiteSetting.content_localization_allowed_groups = group.id.to_s+ end++ it "is included when user can localize content" do+ group.add(admin)+ json = PostSerializer.new(author_post, scope: Guardian.new(admin), root: false).as_json+ expect(json[:can_localize_post]).to eq(true)+ end++ it "is included when author localization is enabled and user is post author" do+ SiteSetting.content_localization_allow_author_localization = true+ json = PostSerializer.new(author_post, scope: Guardian.new(author), root: false).as_json+ expect(json[:can_localize_post]).to eq(true)++ SiteSetting.content_localization_allow_author_localization = false+ json = PostSerializer.new(author_post, scope: Guardian.new(author), root: false).as_json+ expect(json[:can_localize_post]).to eq(nil)+ end++ it "is not included when user cannot localize post" do+ other_user = Fabricate(:user)+ json = PostSerializer.new(author_post, scope: Guardian.new(other_user), root: false).as_json+ expect(json.key?(:can_localize_post)).to eq(false)+ end+ end++ describe "post_localizations_count" do+ fab!(:author, :user)+ fab!(:author_post) { Fabricate(:post, user: author) }+ fab!(:group)++ before do+ SiteSetting.content_localization_enabled = true+ SiteSetting.content_localization_allowed_groups = group.id.to_s+ Fabricate(:post_localization, post: author_post, locale: "ja")+ end++ it "is included for users in allowed groups" do+ user = Fabricate(:user)+ group.add(user)+ json = PostSerializer.new(author_post, scope: Guardian.new(user), root: false).as_json+ expect(json[:post_localizations_count]).to eq(1)+ end++ it "is included for post authors when author localization is enabled" do+ SiteSetting.content_localization_allow_author_localization = true+ json = PostSerializer.new(author_post, scope: Guardian.new(author), root: false).as_json+ expect(json[:post_localizations_count]).to eq(1)+ end++ it "is not included for users who cannot localize" do+ other_user = Fabricate(:user)+ json = PostSerializer.new(author_post, scope: Guardian.new(other_user), root: false).as_json+ expect(json.key?(:post_localizations_count)).to eq(false)+ end+ endenddiff --git a/spec/system/content_localization_spec.rb b/spec/system/content_localization_spec.rbindex 6231b98e728..30ba89f83a8 100644--- a/spec/system/content_localization_spec.rb+++ b/spec/system/content_localization_spec.rb@@ -449,6 +449,58 @@ describe "Content Localization" doendend+ context "with author localization" do+ fab!(:author) { Fabricate(:user, locale: "en") }+ fab!(:author_post) do+ Fabricate(:post, topic:, user: author, locale: "en", raw: "Author's original post")+ end++ before do+ SiteSetting.allow_user_locale = true+ SiteSetting.content_localization_enabled = true+ SiteSetting.content_localization_allowed_groups = "#{Group::AUTO_GROUPS[:admins]}"+ SiteSetting.content_localization_supported_locales = "en|ja"+ SiteSetting.post_menu = "addTranslation"+ end++ it "only shows globe icon on author's own posts" do+ SiteSetting.content_localization_allow_author_localization = false++ sign_in(author)+ topic_page.visit_topic(topic)++ expect(topic_page).to have_no_post_action_button(post_1, :add_translation)+ expect(topic_page).to have_no_post_action_button(author_post, :add_translation)++ SiteSetting.content_localization_allow_author_localization = true+ page.refresh+ expect(topic_page).to have_post_action_button(author_post, :add_translation)+ topic_page.click_post_action_button(author_post, :add_translation)+ find(".post-action-menu__add-translation").click+ expect(translation_composer).to be_opened++ translation_composer.select_locale("Japanese (日本語)")+ translation_composer.fill_content("著者のオリジナル投稿")+ translation_composer.create++ sign_in(japanese_user)+ topic_page.visit_topic(topic)+ expect(topic_page).to have_post_content(+ post_number: author_post.post_number,+ content: "著者のオリジナル投稿",+ )+ end++ it "shows globe icon for admins on all posts" do+ sign_in(admin)+ topic_page.visit_topic(topic)++ expect(topic_page).to have_post_action_button(post_1, :add_translation)+ expect(topic_page).to have_post_action_button(post_2, :add_translation)+ expect(topic_page).to have_post_action_button(author_post, :add_translation)+ end+ end+context "for site settings" dolet(:settings_page) { PageObjects::Pages::AdminSiteSettings.new }let(:banner) { PageObjects::Components::AdminChangesBanner.new }diff --git a/spec/system/page_objects/components/translation_composer.rb b/spec/system/page_objects/components/translation_composer.rbindex a51e1973dd3..b2fddb392ae 100644--- a/spec/system/page_objects/components/translation_composer.rb+++ b/spec/system/page_objects/components/translation_composer.rb@@ -6,6 +6,17 @@ module PageObjectsdef has_translation_title?(value)expect(find("#reply-control input#translated-topic-title").value).to eq(value)end++ def select_locale(locale)+ selector = ".translation-selector-dropdown"+ select_kit = PageObjects::Components::SelectKit.new(selector)+ select_kit.expand+ select_kit.select_row_by_name(locale)+ end++ def fill_content(content)+ find(".d-editor-input").set(content)+ endendendenddiff --git a/spec/system/page_objects/pages/topic.rb b/spec/system/page_objects/pages/topic.rbindex d8edd9e3607..10d2f3ded81 100644--- a/spec/system/page_objects/pages/topic.rb+++ b/spec/system/page_objects/pages/topic.rb@@ -361,6 +361,8 @@ module PageObjectsdef selector_for_post_action_button(button)case button+ when :add_translation+ ".post-controls .post-action-menu-edit-translations-trigger"when :admin".post-controls .post-action-menu__admin"when :bookmark