Skip to content

A platform for community discussion. Free, open, simple.

FIX: Bulk tag, category, and read-state ops no longer lie or overreach (#42784)

Régis Hanoldf98ee1d638f
diff --git a/app/models/post_timing.rb b/app/models/post_timing.rb
index af7d4e8435d..4a5648c21ad 100644
--- a/app/models/post_timing.rb
+++ b/app/models/post_timing.rb
@@ -78,19 +78,22 @@ class PostTiming < ActiveRecord::Base
last_read = post_number - 1
PostTiming.transaction do
- PostTiming.where(
- "topic_id = ? AND user_id = ? AND post_number > ?",
- topic.id,
- user.id,
- last_read,
- ).delete_all
+ destroyed_count =
+ PostTiming.where(
+ "topic_id = ? AND user_id = ? AND post_number > ?",
+ topic.id,
+ user.id,
+ last_read,
+ ).delete_all
last_read = nil if last_read < 1
+ # the TopicUser reset below must still run without timing rows;
+ # only the read count must not go negative
TopicUser.where(user_id: user.id, topic_id: topic.id).update_all(
last_read_post_number: last_read,
)
- topic.posts.find_by(post_number: post_number).decrement!(:reads)
+ topic.posts.find_by(post_number: post_number)&.decrement!(:reads) if destroyed_count > 0
if topic.private_message?
set_minimum_first_unread_pm!(topic: topic, user_id: user.id, date: topic.updated_at)
diff --git a/frontend/discourse/app/components/bulk-select-topics-dropdown.gjs b/frontend/discourse/app/components/bulk-select-topics-dropdown.gjs
index f803d8cdea3..0f44aaf1ad9 100644
--- a/frontend/discourse/app/components/bulk-select-topics-dropdown.gjs
+++ b/frontend/discourse/app/components/bulk-select-topics-dropdown.gjs
@@ -46,6 +46,7 @@ export default class BulkSelectTopicsDropdown extends Component {
@service router;
@service modal;
@service currentUser;
+ @service site;
@service siteSettings;
@service toasts;
@@ -95,8 +96,10 @@ export default class BulkSelectTopicsDropdown extends Component {
id: "manage-tags",
icon: "tag",
name: i18n("topic_bulk_actions.manage_tags.name"),
- visible: ({ currentUser, siteSettings }) =>
- siteSettings.tagging_enabled && currentUser.canManageTopic,
+ visible: ({ currentUser, siteSettings, site }) =>
+ siteSettings.tagging_enabled &&
+ site.can_tag_topics &&
+ currentUser.canManageTopic,
},
{
id: "pin-topics",
@@ -201,6 +204,7 @@ export default class BulkSelectTopicsDropdown extends Component {
return visible({
topics: this.args.bulkSelectHelper.selected,
currentUser: this.currentUser,
+ site: this.site,
siteSettings: this.siteSettings,
router: this.router,
});
diff --git a/frontend/discourse/tests/integration/components/bulk-select-topics-dropdown-test.gjs b/frontend/discourse/tests/integration/components/bulk-select-topics-dropdown-test.gjs
index 242c93cf49f..c4fd5cfd370 100644
--- a/frontend/discourse/tests/integration/components/bulk-select-topics-dropdown-test.gjs
+++ b/frontend/discourse/tests/integration/components/bulk-select-topics-dropdown-test.gjs
@@ -48,6 +48,7 @@ module("Integration | Component | BulkSelectTopicsDropdown", function (hooks) {
test("actions all topics can perform", async function (assert) {
this.currentUser.admin = true;
+ this.site.set("can_tag_topics", true);
this.bulkSelectHelper = createBulkSelectHelper(this);
await render(
@@ -128,6 +129,23 @@ module("Integration | Component | BulkSelectTopicsDropdown", function (hooks) {
.doesNotExist();
});
+ test("does not allow tagging actions if the user cannot tag topics", async function (assert) {
+ this.currentUser.admin = true;
+ this.site.set("can_tag_topics", false);
+ this.bulkSelectHelper = createBulkSelectHelper(this);
+
+ await render(
+ <template>
+ <BulkSelectTopicsDropdown @bulkSelectHelper={{this.bulkSelectHelper}} />
+ </template>
+ );
+
+ await click(".bulk-select-topics-dropdown-trigger");
+ assert
+ .dom(".fk-d-menu__inner-content .dropdown-menu__item .manage-tags")
+ .doesNotExist();
+ });
+
test("does not allow tagging actions if user cannot manage topic", async function (assert) {
this.bulkSelectHelper = createBulkSelectHelper(this);
diff --git a/lib/topics_bulk_action.rb b/lib/topics_bulk_action.rb
index 4c919f76251..45dddc827b7 100644
--- a/lib/topics_bulk_action.rb
+++ b/lib/topics_bulk_action.rb
@@ -168,18 +168,27 @@ class TopicsBulkAction
def destroy_post_timing
topics.each do |t|
+ next unless guardian.can_see?(t)
+
PostTiming.destroy_last_for(@user, topic: t)
@changed_ids << t.id
end
end
def change_category
- updatable_topics = topics.where.not(category_id: @operation[:category_id])
+ category_id = @operation[:category_id]
+ updatable_topics = topics.where.not(category_id: category_id)
+
+ # mirrors the PostRevisor gate, which skips the check when moving to uncategorized
+ if category_id.to_i > 0 && !guardian.can_move_topic_to_category?(category_id)
+ @errors[I18n.t("category.errors.move_topic_to_category_disallowed")] += updatable_topics.count
+ return
+ end
updatable_topics.each do |t|
next unless guardian.can_edit?(t) && t.first_post
- changes = { category_id: @operation[:category_id] }
+ changes = { category_id: category_id }
if t.first_post.revise(@user, changes, bulk_revision_opts)
@changed_ids << t.id
else
@@ -271,6 +280,8 @@ class TopicsBulkAction
def reset_bump_dates
if guardian.can_update_bumped_at?
topics.each do |t|
+ next unless guardian.can_see?(t)
+
t.reset_bumped_at
@changed_ids << t.id
end
@@ -383,7 +394,10 @@ class TopicsBulkAction
end
def apply_tag_revision(topic, tag_names)
- return false unless guardian.can_edit?(topic)
+ unless guardian.can_edit_tags?(topic)
+ @errors[I18n.t("tags.user_not_permitted")] += 1
+ return false
+ end
return false unless topic.first_post
if topic.first_post.revise(@user, { tags: tag_names }, bulk_revision_opts)
@@ -442,7 +456,7 @@ class TopicsBulkAction
end
def topics
- @topics ||= Topic.where(id: @topic_ids)
+ @topics ||= Topic.where(id: @topic_ids).includes(:category)
end
def topics_with_tags
diff --git a/spec/lib/topics_bulk_action_spec.rb b/spec/lib/topics_bulk_action_spec.rb
index 7a8d1694297..9f178bb9837 100644
--- a/spec/lib/topics_bulk_action_spec.rb
+++ b/spec/lib/topics_bulk_action_spec.rb
@@ -275,6 +275,30 @@ RSpec.describe TopicsBulkAction do
end
end
+ context "when the user can't move topics to the destination category" do
+ it "records an error and creates no revision" do
+ restricted_category = Fabricate(:private_category, group: Fabricate(:group))
+ original_category = topic.category
+
+ bulk_action =
+ TopicsBulkAction.new(
+ topic.user,
+ [topic.id],
+ type: "change_category",
+ category_id: restricted_category.id,
+ )
+ topic_ids = bulk_action.perform!
+
+ expect(topic_ids).to eq([])
+ expect(topic.reload.category).to eq(original_category)
+ expect(first_post.reload.version).to eq(1)
+ expect(PostRevision.where(post: first_post)).to be_empty
+ expect(bulk_action.errors).to eq(
+ I18n.t("category.errors.move_topic_to_category_disallowed") => 1,
+ )
+ end
+ end
+
context "when destination category does not allow the topic's tags" do
fab!(:destination_category, :category)
fab!(:other_tag) { Fabricate(:tag, name: "other-tag") }
@@ -467,6 +491,19 @@ RSpec.describe TopicsBulkAction do
expect { topic_ids = tba.perform! }.to change { PostTiming.count }.by(-1)
expect(topic_ids).to contain_exactly(topic.id)
end
+
+ it "skips topics the user can't see" do
+ pm = Fabricate(:private_message_topic)
+ pm_post = Fabricate(:post, topic: pm)
+ stranger = Fabricate(:user)
+
+ topic_ids = nil
+ expect {
+ topic_ids = TopicsBulkAction.new(stranger, [pm.id], type: "destroy_post_timing").perform!
+ }.not_to change { pm_post.reload.reads }
+
+ expect(topic_ids).to eq([])
+ end
end
describe "delete" do
@@ -603,6 +640,29 @@ RSpec.describe TopicsBulkAction do
expect(topic_ids).to eq([])
expect(topic.reload.bumped_at).to eq_time(bumped_at)
end
+
+ it "skips topics the user can't see" do
+ trust_level_4 = Fabricate(:trust_level_4)
+ private_topic =
+ Fabricate(:topic, category: Fabricate(:private_category, group: Fabricate(:group)))
+ pm = Fabricate(:private_message_topic)
+ bumped_at = 1.hour.ago
+ [private_topic, pm].each do |restricted_topic|
+ Fabricate(:post, topic: restricted_topic, created_at: 1.day.ago)
+ restricted_topic.update!(bumped_at: bumped_at)
+ end
+
+ topic_ids =
+ TopicsBulkAction.new(
+ trust_level_4,
+ [private_topic.id, pm.id],
+ type: "reset_bump_dates",
+ ).perform!
+
+ expect(topic_ids).to eq([])
+ expect(private_topic.reload.bumped_at).to eq_time(bumped_at)
+ expect(pm.reload.bumped_at).to eq_time(bumped_at)
+ end
end
describe "#pin" do
@@ -757,7 +817,7 @@ RSpec.describe TopicsBulkAction do
fab!(:tag3, :tag)
it "doesn't change the tags" do
- Guardian.any_instance.expects(:can_edit?).returns(false)
+ Guardian.any_instance.expects(:can_edit_topic?).returns(false)
topic_ids =
TopicsBulkAction.new(
@@ -772,6 +832,24 @@ RSpec.describe TopicsBulkAction do
end
end
+ context "when the user can't tag topics" do
+ fab!(:tag3, :tag)
+
+ it "records an error and creates no revision" do
+ SiteSetting.tag_topic_allowed_groups = Group::AUTO_GROUPS[:staff]
+
+ bulk_action =
+ TopicsBulkAction.new(topic.user, [topic.id], type: "change_tags", tag_ids: [tag3.id])
+ topic_ids = bulk_action.perform!
+
+ expect(topic_ids).to eq([])
+ expect(topic.reload.tags).to contain_exactly(tag1, tag2)
+ expect(first_post.reload.version).to eq(1)
+ expect(PostRevision.where(post: first_post)).to be_empty
+ expect(bulk_action.errors).to eq(I18n.t("tags.user_not_permitted") => 1)
+ end
+ end
+
it "does not send notifications" do
fab_tag3 = Fabricate(:tag)
topic_watcher = Fabricate(:user)
@@ -853,7 +931,7 @@ RSpec.describe TopicsBulkAction do
context "when the user can't edit the topic" do
it "doesn't change the tags" do
- Guardian.any_instance.expects(:can_edit?).returns(false)
+ Guardian.any_instance.expects(:can_edit_topic?).returns(false)
topic_ids =
TopicsBulkAction.new(
@@ -914,7 +992,7 @@ RSpec.describe TopicsBulkAction do
context "when the user can't edit the topic" do
it "doesn't remove the tags" do
- Guardian.any_instance.expects(:can_edit?).returns(false)
+ Guardian.any_instance.expects(:can_edit_topic?).returns(false)
topic_ids = TopicsBulkAction.new(topic.user, [topic.id], type: "remove_tags").perform!
diff --git a/spec/models/post_timing_spec.rb b/spec/models/post_timing_spec.rb
index b056b55f0a3..f28132eeffa 100644
--- a/spec/models/post_timing_spec.rb
+++ b/spec/models/post_timing_spec.rb
@@ -179,6 +179,14 @@ RSpec.describe PostTiming do
end
describe ".destroy_last_for" do
+ it "does not decrement reads when the user had no timing on the last post" do
+ post = Fabricate(:post, reads: 5)
+
+ expect {
+ PostTiming.destroy_last_for(Fabricate(:user), topic_id: post.topic_id)
+ }.not_to change { post.reload.reads }
+ end
+
it "updates first unread for a user correctly when topic is public" do
post.topic.update!(updated_at: 10.minutes.ago)
PostTiming.process_timings(post.user, post.topic_id, 1, [[post.post_number, 100]])