A platform for community discussion. Free, open, simple.
DEV: Add site setting for allowed groups to change post ownership (#38122)
Chris Alberti7cec198b3be4
diff --git a/app/serializers/current_user_serializer.rb b/app/serializers/current_user_serializer.rbindex 72d27d4a35f..c24810036c8 100644--- a/app/serializers/current_user_serializer.rb+++ b/app/serializers/current_user_serializer.rb@@ -82,7 +82,8 @@ class CurrentUserSerializer < BasicUserSerializer:can_localize_content?,:effective_locale,:can_see_ip,- :is_impersonating+ :is_impersonating,+ :can_change_post_ownerdelegate :user_stat, to: :object, private: truedelegate :any_posts, :draft_count, :pending_posts_count, :read_faq?, to: :user_stat@@ -106,6 +107,17 @@ class CurrentUserSerializer < BasicUserSerializer!!object.is_impersonatingend+ def include_can_change_post_owner?+ return true if admin?+ return true if SiteSetting.moderators_change_post_ownership && moderator?+ return true if object.in_any_groups?(SiteSetting.change_post_ownership_allowed_groups_map)+ false+ end++ def can_change_post_owner+ true+ end+def groupsowned_group_ids = GroupUser.where(user_id: id, owner: true).pluck(:group_id).to_setdiff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex e91f6df6f0e..e14dfcbe8e0 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -1916,6 +1916,7 @@ en:moderators_manage_categories: "Allow moderators to create and manage categories"moderators_manage_groups: "Allow moderators to create and manage groups"moderators_change_post_ownership: "Allow moderators to change post ownership"+ change_post_ownership_allowed_groups: "Allow users in these groups to change post ownership"cors_origins: "Allowed origins for cross-origin requests (CORS). Each origin must include http:// or https://. The DISCOURSE_ENABLE_CORS env variable must be set to true to enable CORS."use_admin_ip_allowlist: "Admins can only log in if they are at an IP address defined in the Screened IPs list (Admin > Logs > Screened Ips)."blocked_ip_blocks: "A list of private IP blocks that should never be crawled by Discourse"diff --git a/config/site_settings.yml b/config/site_settings.ymlindex 52757b2d6cb..65b5306f1ac 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -2680,6 +2680,10 @@ security:moderators_change_post_ownership:client: truedefault: false+ change_post_ownership_allowed_groups:+ type: group_list+ list_type: compact+ default: ""moderators_view_emails:client: truedefault: falsediff --git a/frontend/discourse/app/components/admin-post-menu.gjs b/frontend/discourse/app/components/admin-post-menu.gjsindex b46e873d609..ef34fb2b806 100644--- a/frontend/discourse/app/components/admin-post-menu.gjs+++ b/frontend/discourse/app/components/admin-post-menu.gjs@@ -24,6 +24,10 @@ export default class AdminPostMenu extends Component {.filter(Boolean);}+ get canChangePostOwner() {+ return this.currentUser?.canChangePostOwner;+ }+@actionasync topicAction(actionName) {await this.args.close();@@ -107,15 +111,7 @@ export default class AdminPostMenu extends Component {</dropdown.item>{{/if}}- {{#if- (or- this.currentUser.admin- (and- this.siteSettings.moderators_change_post_ownership- this.currentUser.staff- )- )- }}+ {{#if this.canChangePostOwner}}<dropdown.item><DButton@label="post.controls.change_owner"diff --git a/frontend/discourse/app/components/post/menu/buttons/admin.gjs b/frontend/discourse/app/components/post/menu/buttons/admin.gjsindex c1b3ebba3e7..9fe8114a038 100644--- a/frontend/discourse/app/components/post/menu/buttons/admin.gjs+++ b/frontend/discourse/app/components/post/menu/buttons/admin.gjs@@ -2,9 +2,12 @@ import Component from "@glimmer/component";import DButton from "discourse/components/d-button";export default class PostMenuAdminButton extends Component {- static shouldRender(args) {+ static shouldRender(args, context) {return (- args.post.canManage || args.post.can_wiki || args.post.canEditStaffNotes+ args.post.canManage ||+ args.post.can_wiki ||+ args.post.canEditStaffNotes ||+ context.currentUser?.canChangePostOwner);}diff --git a/frontend/discourse/app/controllers/topic.js b/frontend/discourse/app/controllers/topic.jsindex eba22aabfc8..9e124aad854 100644--- a/frontend/discourse/app/controllers/topic.js+++ b/frontend/discourse/app/controllers/topic.js@@ -1763,17 +1763,13 @@ export default class TopicController extends Controller {}@computed(- "currentUser.admin",- "currentUser.staff",- "siteSettings.moderators_change_post_ownership","selectedPostsCount",- "selectedPostsUsername"+ "selectedPostsUsername",+ "currentUser.canChangePostOwner")get canChangeOwner() {return (- (this.currentUser?.admin ||- (this.siteSettings?.moderators_change_post_ownership &&- this.currentUser?.staff)) &&+ !!this.currentUser?.canChangePostOwner &&this.selectedPostsCount > 0 &&this.selectedPostsUsername !== undefined);diff --git a/frontend/discourse/app/models/user.js b/frontend/discourse/app/models/user.jsindex 7a77b2a4ee3..3f6ceae61ff 100644--- a/frontend/discourse/app/models/user.js+++ b/frontend/discourse/app/models/user.js@@ -268,6 +268,7 @@ export default class User extends RestModel.extend(Evented) {@filterBy("groups", "has_messages", true) groupsWithMessages;@alias("can_pick_theme_with_custom_homepage") canPickThemeWithCustomHomepage;@alias("can_edit_tags") canEditTags;+ @alias("can_change_post_owner") canChangePostOwner;numGroupsToDisplay = 2;diff --git a/frontend/discourse/tests/integration/components/post/post-test.gjs b/frontend/discourse/tests/integration/components/post/post-test.gjsindex 0109f9c920b..4d7b6ae22e1 100644--- a/frontend/discourse/tests/integration/components/post/post-test.gjs+++ b/frontend/discourse/tests/integration/components/post/post-test.gjs@@ -590,7 +590,7 @@ module("Integration | Component | Post", function (hooks) {});test("change owner", async function (assert) {- this.currentUser.admin = true;+ this.currentUser.can_change_post_owner = true;await renderComponent(this.post, {changePostOwner: () => assert.step("change post owner called"),diff --git a/frontend/discourse/tests/unit/controllers/topic-test.js b/frontend/discourse/tests/unit/controllers/topic-test.jsindex dec99c855b2..8f647f1d071 100644--- a/frontend/discourse/tests/unit/controllers/topic-test.js+++ b/frontend/discourse/tests/unit/controllers/topic-test.js@@ -352,8 +352,11 @@ module("Unit | Controller | topic", function (hooks) {);});- test("canChangeOwner", function (assert) {- const currentUser = this.store.createRecord("user", { admin: false });+ test("admin canChangeOwner", function (assert) {+ const currentUser = this.store.createRecord("user", {+ admin: false,+ can_change_post_owner: false,+ });const model = topicWithStream.call(this, {posts: [{ id: 1, username: "gary" },@@ -372,6 +375,9 @@ module("Unit | Controller | topic", function (hooks) {assert.false(controller.canChangeOwner, "false when not admin");currentUser.set("admin", true);+ // For admin, can_change_post_owner will be set on the model+ currentUser.set("can_change_post_owner", true);+assert.true(controller.canChangeOwner,"true when admin and one post is selected"@@ -384,8 +390,11 @@ module("Unit | Controller | topic", function (hooks) {);});- test("modCanChangeOwner", function (assert) {- const currentUser = this.store.createRecord("user", { moderator: false });+ test("moderator canChangeOwner", function (assert) {+ const currentUser = this.store.createRecord("user", {+ moderator: false,+ can_change_post_owner: false,+ });const model = topicWithStream.call(this, {posts: [{ id: 1, username: "gary" },@@ -407,6 +416,9 @@ module("Unit | Controller | topic", function (hooks) {assert.false(controller.canChangeOwner, "false when not moderator");currentUser.set("moderator", true);+ // For a moderator, can_change_post_owner would be set on the model+ currentUser.set("can_change_post_owner", true);+assert.true(controller.canChangeOwner,"true when moderator and one post is selected"@@ -419,6 +431,43 @@ module("Unit | Controller | topic", function (hooks) {);});+ test("canChangeOwner", function (assert) {+ const currentUser = this.store.createRecord("user", {+ can_change_post_owner: false,+ });+ const model = topicWithStream.call(this, {+ posts: [+ { id: 1, username: "gary" },+ { id: 2, username: "lili" },+ ],+ stream: [1, 2],+ });+ model.set("currentUser", currentUser);++ const controller = getOwner(this).lookup("controller:topic");+ controller.setProperties({ model, currentUser });++ assert.false(controller.canChangeOwner, "false when no posts are selected");++ controller.selectedPostIds.push(1);+ assert.false(+ controller.canChangeOwner,+ "false when can_change_post_owner is false"+ );++ currentUser.set("can_change_post_owner", true);+ assert.true(+ controller.canChangeOwner,+ "true when can_change_post_owner and one post is selected"+ );++ controller.selectedPostIds.push(2);+ assert.false(+ controller.canChangeOwner,+ "false when can_change_post_owner but more than 1 user"+ );+ });+test("canMergePosts", function (assert) {const model = topicWithStream.call(this, {posts: [diff --git a/lib/guardian/post_guardian.rb b/lib/guardian/post_guardian.rbindex aa47fcfbdb0..c902746b7a5 100644--- a/lib/guardian/post_guardian.rb+++ b/lib/guardian/post_guardian.rb@@ -360,8 +360,9 @@ module PostGuardiandef can_change_post_owner?return true if is_admin?-- SiteSetting.moderators_change_post_ownership && is_staff?+ return true if SiteSetting.moderators_change_post_ownership && is_staff?+ return true if @user.in_any_groups?(SiteSetting.change_post_ownership_allowed_groups_map)+ falseenddef can_change_post_timestamps?diff --git a/spec/lib/guardian/post_guardian_spec.rb b/spec/lib/guardian/post_guardian_spec.rbindex 92c1dbdc6a7..4a300cd7089 100644--- a/spec/lib/guardian/post_guardian_spec.rb+++ b/spec/lib/guardian/post_guardian_spec.rb@@ -583,11 +583,26 @@ RSpec.describe PostGuardian doexpect(Guardian.new(moderator).can_change_post_owner?).to be_truthyend- it "returns true for a moderator when not allowed" do+ it "returns false for a moderator when not allowed" doSiteSetting.moderators_change_post_ownership = falseexpect(Guardian.new(moderator).can_change_post_owner?).to be_falseyend++ describe "with allowed groups" do+ fab!(:allowed_group, :group)+ fab!(:allowed_group_user) { Fabricate(:user, groups: [allowed_group]) }++ before { SiteSetting.change_post_ownership_allowed_groups = "#{allowed_group.id}" }++ it "returns true for user in allowed group" do+ expect(Guardian.new(allowed_group_user).can_change_post_owner?).to be_truthy+ end++ it "returns false for user not in allowed group" do+ expect(Guardian.new(user).can_change_post_owner?).to be_falsy+ end+ endenddescribe "#can_change_post_timestamps?" dodiff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rbindex 58877a01c77..aa2f3e13693 100644--- a/spec/requests/topics_controller_spec.rb+++ b/spec/requests/topics_controller_spec.rb@@ -1075,6 +1075,9 @@ RSpec.describe TopicsController dofab!(:p1) { Fabricate(:post, user: post_author1, topic: topic) }fab!(:p2) { Fabricate(:post, user: post_author2, topic: topic) }+ fab!(:allowed_group, :group)+ fab!(:allowed_group_user) { Fabricate(:user, groups: [allowed_group]) }+describe "moderator signed in" dolet!(:editor) { sign_in(moderator) }@@ -1185,6 +1188,37 @@ RSpec.describe TopicsController doendend+ describe "user in group signed in" do+ fab!(:allowed_group, :group)+ fab!(:allowed_group_user) { Fabricate(:user, groups: [allowed_group]) }+ fab!(:topic_allowed_user_can_see) { Fabricate(:topic, category: category) }+ fab!(:post_allowed_user_can_see) { Fabricate(:post, topic: topic_allowed_user_can_see) }++ before { sign_in(allowed_group_user) }++ it "returns 200 when group is allow listed" do+ SiteSetting.change_post_ownership_allowed_groups = "#{allowed_group.id}"++ post "/t/#{topic_allowed_user_can_see.id}/change-owner.json",+ params: {+ username: user_a.username_lower,+ post_ids: [post_allowed_user_can_see.id],+ }+ expect(response.status).to eq(200)+ end++ it "returns 403 when group is not allow listed" do+ SiteSetting.change_post_ownership_allowed_groups = nil++ post "/t/#{topic_allowed_user_can_see.id}/change-owner.json",+ params: {+ username: user_a.username_lower,+ post_ids: [post_allowed_user_can_see.id],+ }+ expect(response.status).to eq(403)+ end+ end+context "with API key" dolet(:api_key) { Fabricate(:api_key, user: admin, created_by: admin) }@@ -1285,6 +1319,47 @@ RSpec.describe TopicsController doexpect(private_post.reload.user).to eq(user_a)endend++ describe "user in allowed group signed in" do+ fab!(:pm_topic_allowed_user_can_see) do+ Fabricate(:private_message_topic, user: allowed_group_user)+ end+ fab!(:pm_post_allowed_user_can_see) do+ Fabricate(:post, topic: pm_topic_allowed_user_can_see, user: allowed_group_user)+ end++ before do+ SiteSetting.change_post_ownership_allowed_groups = "#{allowed_group.id}"+ sign_in(allowed_group_user)+ end++ it "returns 200 for visible PM" do+ post "/t/#{pm_topic_allowed_user_can_see.id}/change-owner.json",+ params: {+ username: user_a.username_lower,+ post_ids: [pm_post_allowed_user_can_see.id],+ }+ expect(response.status).to eq(200)+ end++ it "returns 403 for not visible PM" do+ post "/t/#{pm_topic.id}/change-owner.json",+ params: {+ username: user_a.username,+ post_ids: [pm_post.id],+ }+ expect(response.status).to eq(403)+ end++ it "returns 403 for post in private category" do+ post "/t/#{private_topic.id}/change-owner.json",+ params: {+ username: user_a.username,+ post_ids: [private_post.id],+ }+ expect(response.status).to eq(403)+ end+ endendendenddiff --git a/spec/system/post_menu_spec.rb b/spec/system/post_menu_spec.rbindex eb03be4cd5e..759c17188b2 100644--- a/spec/system/post_menu_spec.rb+++ b/spec/system/post_menu_spec.rb@@ -134,6 +134,31 @@ describe "Post menu", type: :system dotopic_page.click_post_action_button(post, :admin)expect(topic_page).to have_post_admin_menuend++ describe "with change_post_ownership_allowed_groups" do+ fab!(:allowed_group, :group)+ fab!(:allowed_group_user) { Fabricate(:user, groups: [allowed_group]) }++ before { SiteSetting.change_post_ownership_allowed_groups = "#{allowed_group.id}" }++ it "displays the admin button when the group is allowed" do+ sign_in(allowed_group_user)++ topic_page.visit_topic(post.topic)++ expect(topic_page).to have_post_action_button(post, :admin)+ expect(topic_page).to have_post_action_button(post2, :admin)+ end+ it "does not display the admin button when the group is not allowed" do+ SiteSetting.change_post_ownership_allowed_groups = ""+ sign_in(allowed_group_user)++ topic_page.visit_topic(post.topic)++ expect(topic_page).to have_no_post_action_button(post, :admin)+ expect(topic_page).to have_no_post_action_button(post2, :admin)+ end+ endenddescribe "bookmark" do