A platform for community discussion. Free, open, simple.
FEATURE: Allow `topic_timers_allowed_groups` to manage topic timers (#41126)
Mark VanLandingham1171b86bb77d
diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rbindex fe405526715..a832403e133 100644--- a/app/controllers/topics_controller.rb+++ b/app/controllers/topics_controller.rb@@ -655,7 +655,7 @@ class TopicsController < ApplicationControllerparams.require(:duration_minutes) if based_on_last_posttopic = Topic.find_by(id: params[:topic_id])- guardian.ensure_can_moderate!(topic)+ guardian.ensure_can_set_topic_timer!(topic)guardian.ensure_can_delete!(topic) if TopicTimer.destructive_types.values.include?(status_type)diff --git a/app/jobs/regular/bump_topic.rb b/app/jobs/regular/bump_topic.rbindex a8cd9fa35b4..4bce5dd11c7 100644--- a/app/jobs/regular/bump_topic.rb+++ b/app/jobs/regular/bump_topic.rb@@ -3,7 +3,9 @@module Jobsclass BumpTopic < ::Jobs::TopicTimerBasedef execute_timer_action(topic_timer, topic)- if Guardian.new(topic_timer.user).can_create_post_on_topic?(topic)+ guardian = Guardian.new(topic_timer.user)++ if guardian.can_create_post_on_topic?(topic) || guardian.can_set_topic_timer?(topic)topic.add_small_action(Discourse.system_user, "autobumped", nil, bump: true)enddiff --git a/app/jobs/regular/close_topic.rb b/app/jobs/regular/close_topic.rbindex 501d161b115..2dcf7b098e8 100644--- a/app/jobs/regular/close_topic.rb+++ b/app/jobs/regular/close_topic.rb@@ -11,7 +11,7 @@ module Jobsreturnend- if !Guardian.new(user).can_close_topic?(topic)+ if !Guardian.new(user).can_set_topic_timer?(topic)topic_timer.destroy!topic.reloaddiff --git a/app/jobs/regular/open_topic.rb b/app/jobs/regular/open_topic.rbindex 3100c46d985..1be6132aaf2 100644--- a/app/jobs/regular/open_topic.rb+++ b/app/jobs/regular/open_topic.rb@@ -5,7 +5,7 @@ module Jobsdef execute_timer_action(topic_timer, topic)user = topic_timer.user- if !Guardian.new(user).can_open_topic?(topic) || topic.open?+ if !Guardian.new(user).can_set_topic_timer?(topic) || topic.open?topic_timer.destroy!topic.reloaddiff --git a/app/jobs/regular/publish_topic_to_category.rb b/app/jobs/regular/publish_topic_to_category.rbindex d66e15c6ac9..25c97b2d760 100644--- a/app/jobs/regular/publish_topic_to_category.rb+++ b/app/jobs/regular/publish_topic_to_category.rb@@ -8,7 +8,7 @@ module Jobscategory = topic_timer.categoryreturn if category.blank?- return unless guardian.can_see?(topic)+ return unless guardian.can_set_topic_timer?(topic)return unless guardian.can_create_topic_on_category?(category)return if topic.private_message? && !guardian.can_convert_topic?(topic)diff --git a/app/jobs/regular/toggle_topic_closed.rb b/app/jobs/regular/toggle_topic_closed.rbindex 6a1c811eead..7c5f5b8a7d5 100644--- a/app/jobs/regular/toggle_topic_closed.rb+++ b/app/jobs/regular/toggle_topic_closed.rb@@ -24,7 +24,7 @@ module Jobsuser = topic_timer.user- if Guardian.new(user).can_close_topic?(topic)+ if Guardian.new(user).can_set_topic_timer?(topic)if state == false && topic.auto_close_threshold_reached?topic.set_or_create_timer(TopicTimer.types[:open],diff --git a/app/models/topic.rb b/app/models/topic.rbindex cbb5fc3bcdf..89f1b5f1f7c 100644--- a/app/models/topic.rb+++ b/app/models/topic.rb@@ -1697,12 +1697,12 @@ class Topic < ActiveRecord::Baseendif topic_timer.execute_at- if by_user&.staff? || by_user&.trust_level == TrustLevel[4]+ if Guardian.new(by_user).can_set_topic_timer?(self)topic_timer.user = by_userelsetopic_timer.user ||=(- if user.staff? || user.trust_level == TrustLevel[4]+ if Guardian.new(user).can_set_topic_timer?(self)userelseDiscourse.system_userdiff --git a/app/serializers/current_user_serializer.rb b/app/serializers/current_user_serializer.rbindex 774ca080541..0810f6d716a 100644--- a/app/serializers/current_user_serializer.rb+++ b/app/serializers/current_user_serializer.rb@@ -53,6 +53,7 @@ class CurrentUserSerializer < BasicUserSerializer:primary_group_id,:flair_group_id,:can_create_topic,+ :can_set_topic_timer,:can_create_category,:can_create_group,:link_posting_access,@@ -145,6 +146,10 @@ class CurrentUserSerializer < BasicUserSerializerscope.can_create_topic?(nil)end+ def can_set_topic_timer+ scope.can_set_topic_timer?+ end+def can_create_categorytrueenddiff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex 7236395f0e6..3bd57dc9894 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -2324,6 +2324,7 @@ en:edit_all_post_groups: "Allow users in this group to edit other users' posts"create_topic_allowed_groups: "Groups that are allowed to create new topics. Admins and moderators can always create topics."+ topic_timers_allowed_groups: "Groups that are allowed to set topic timers. Admins and moderators can always set topic timers."allow_flagging_staff: "If enabled, users can flag posts from staff accounts."edit_wiki_post_allowed_groups: "Groups that are allowed to edit posts marked as wiki. Admins and moderators can always edit posts marked as wiki."diff --git a/config/site_settings.yml b/config/site_settings.ymlindex 1c63ae9e202..d4a123fcbdd 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -2647,6 +2647,14 @@ trust:refresh: truedisallowed_groups: "4"area: "group_permissions"+ topic_timers_allowed_groups:+ default: "1|2|14" # auto group admins, moderators, trust_level_4+ mandatory_values: "1|2" # auto group admins, moderators+ type: group_list+ allow_any: false+ refresh: true+ disallowed_groups: "4"+ area: "group_permissions"edit_wiki_post_allowed_groups:default: "1|2|11" # auto group admins, moderators, trust_level_1mandatory_values: "1|2" # auto group admins, moderatorsdiff --git a/frontend/discourse/app/components/topic-admin-menu.gjs b/frontend/discourse/app/components/topic-admin-menu.gjsindex ccdf361631d..6f386adcf13 100644--- a/frontend/discourse/app/components/topic-admin-menu.gjs+++ b/frontend/discourse/app/components/topic-admin-menu.gjs@@ -110,12 +110,47 @@ export default class TopicAdminMenu extends Component {get showAdminButton() {return (this.currentUser?.canManageTopic ||+ this.currentUser?.canSetTopicTimer ||this.details?.can_archive_topic ||this.details?.can_close_topic ||this.details?.can_split_merge_topic);}+ get showTopicTimerItem() {+ return this.currentUser?.canSetTopicTimer;+ }++ get showTopicManagementSection() {+ return this.currentUser?.canManageTopic || this.showTopicTimerItem;+ }++ get showTopicManagementSectionDivider() {+ const showsMultiSelect =+ this.currentUser?.canManageTopic || this.details?.can_split_merge_topic;+ const showsDeleteOrRecover =+ (this.currentUser?.canManageTopic ||+ this.details?.can_moderate_category) &&+ (this.canDelete || this.canRecover);+ const showsPin =+ this.details?.can_pin_unpin_topic &&+ !this.isPrivateMessage &&+ (this.visible || this.featured || this.details?.can_banner_topic);+ const showsArchive =+ this.details?.can_archive_topic && !this.isPrivateMessage;++ return (+ this.showTopicManagementSection &&+ (showsMultiSelect ||+ showsDeleteOrRecover ||+ this.details?.can_close_topic ||+ showsPin ||+ showsArchive ||+ this.details?.can_toggle_topic_visibility ||+ this.details?.can_convert_topic)+ );+ }+get showNestedRepliesToggle() {return (this.siteSettings.nested_replies_enabled &&@@ -302,42 +337,48 @@ export default class TopicAdminMenu extends Component {</dropdown.item>{{/if}}- <dropdown.divider />-- {{#if this.currentUser.canManageTopic}}- <dropdown.item class="admin-topic-timer-update">- <DButton- @label="topic.actions.timed_update"- @action={{fn this.onButtonAction "showTopicTimerModal"}}- @icon="far-clock"- />- </dropdown.item>+ {{#if this.showTopicManagementSection}}+ {{#if this.showTopicManagementSectionDivider}}+ <dropdown.divider />+ {{/if}}- {{#if this.currentUser.staff}}- <dropdown.item class="topic-admin-change-timestamp">+ {{#if this.showTopicTimerItem}}+ <dropdown.item class="admin-topic-timer-update"><DButton- @label="topic.change_timestamp.title"- @action={{fn this.onButtonAction "showChangeTimestamp"}}- @icon="calendar-days"+ @label="topic.actions.timed_update"+ @action={{fn this.onButtonAction "showTopicTimerModal"}}+ @icon="far-clock"/></dropdown.item>{{/if}}- <dropdown.item class="topic-admin-reset-bump-date">- <DButton- @label="topic.actions.reset_bump_date"- @action={{fn this.onButtonAction "resetBumpDate"}}- @icon="anchor"- />- </dropdown.item>+ {{#if this.currentUser.canManageTopic}}+ {{#if this.currentUser.staff}}+ <dropdown.item class="topic-admin-change-timestamp">+ <DButton+ @label="topic.change_timestamp.title"+ @action={{fn this.onButtonAction "showChangeTimestamp"}}+ @icon="calendar-days"+ />+ </dropdown.item>+ {{/if}}- <dropdown.item class="topic-admin-slow-mode">- <DButton- @label="topic.actions.slow_mode"- @action={{fn this.onButtonAction "showTopicSlowModeUpdate"}}- @icon="hourglass-start"- />- </dropdown.item>+ <dropdown.item class="topic-admin-reset-bump-date">+ <DButton+ @label="topic.actions.reset_bump_date"+ @action={{fn this.onButtonAction "resetBumpDate"}}+ @icon="anchor"+ />+ </dropdown.item>++ <dropdown.item class="topic-admin-slow-mode">+ <DButton+ @label="topic.actions.slow_mode"+ @action={{fn this.onButtonAction "showTopicSlowModeUpdate"}}+ @icon="hourglass-start"+ />+ </dropdown.item>+ {{/if}}{{/if}}{{#ifdiff --git a/frontend/discourse/app/components/topic-timer-info.gjs b/frontend/discourse/app/components/topic-timer-info.gjsindex ba1d13a8439..408a7968ec3 100644--- a/frontend/discourse/app/components/topic-timer-info.gjs+++ b/frontend/discourse/app/components/topic-timer-info.gjs@@ -37,9 +37,9 @@ export default class TopicTimerInfo extends Component {}}- @computed+ @computed("currentUser.canSetTopicTimer")get canModifyTimer() {- return this.currentUser && this.currentUser.get("canManageTopic");+ return this.currentUser?.get("canSetTopicTimer");}@computed("canModifyTimer", "removeTopicTimer")diff --git a/frontend/discourse/app/models/user.js b/frontend/discourse/app/models/user.jsindex 62d0f83f128..d9036c22d98 100644--- a/frontend/discourse/app/models/user.js+++ b/frontend/discourse/app/models/user.js@@ -298,6 +298,11 @@ export default class User extends RestModel.extend(Evented) {return this.staff || this.isLeader;}+ @computed("can_set_topic_timer", "canManageTopic")+ get canSetTopicTimer() {+ return this.can_set_topic_timer ?? this.canManageTopic;+ }+@computed("sidebar_category_ids")get sidebarCategoryIds() {return this.sidebar_category_ids;diff --git a/frontend/discourse/tests/acceptance/topic-admin-menu-test.js b/frontend/discourse/tests/acceptance/topic-admin-menu-test.jsindex 99e0657b926..d52e4157694 100644--- a/frontend/discourse/tests/acceptance/topic-admin-menu-test.js+++ b/frontend/discourse/tests/acceptance/topic-admin-menu-test.js@@ -32,6 +32,25 @@ acceptance("Topic - Admin Menu", function (needs) {assert.dom(".topic-admin-delete").exists("the delete item is rendered");});+ test("Enter as a user with topic timer permissions", async function (assert) {+ updateCurrentUser({+ admin: false,+ moderator: false,+ trust_level: 1,+ can_set_topic_timer: true,+ });++ await visit("/t/internationalization-localization/280");+ await click(".toggle-admin-menu");++ assert+ .dom(".admin-topic-timer-update")+ .exists("the topic timer item is rendered");+ assert+ .dom(".topic-admin-slow-mode")+ .doesNotExist("topic management items are not rendered");+ });+test("Enter as a user with moderator and admin permissions", async function (assert) {updateCurrentUser({ moderator: true, admin: true, trust_level: 4 });diff --git a/lib/guardian/topic_guardian.rb b/lib/guardian/topic_guardian.rbindex c761a5a3ad0..a567e9ce607 100644--- a/lib/guardian/topic_guardian.rb+++ b/lib/guardian/topic_guardian.rb@@ -59,6 +59,15 @@ module TopicGuardian(!category || Category.topic_create_allowed(self).where(id: category_id).count == 1)end+ def can_set_topic_timer?(topic = nil)+ return false if anonymous? || is_silenced?+ return true if @user.is_system_user?+ return false if topic && !can_see_topic?(topic)+ return true if is_staff?++ @user.in_any_groups?(SiteSetting.topic_timers_allowed_groups_map)+ end+def can_move_topic_to_category?(category)category =(diff --git a/spec/jobs/close_topic_spec.rb b/spec/jobs/close_topic_spec.rbindex 4078fd7941c..d072deea088 100644--- a/spec/jobs/close_topic_spec.rb+++ b/spec/jobs/close_topic_spec.rb@@ -50,6 +50,26 @@ RSpec.describe Jobs::CloseTopic doendend+ describe "when user is allowed to set topic timers" do+ fab!(:user)+ fab!(:group)+ fab!(:topic) { Fabricate(:topic_timer, user: user).topic }++ before do+ group.add(user)+ user.reload+ SiteSetting.topic_timers_allowed_groups = group.id.to_s+ end++ it "closes the topic" do+ freeze_time(topic.public_topic_timer.execute_at + 1.minute)++ described_class.new.execute(topic_timer_id: topic.public_topic_timer.id)++ expect(topic.reload.closed).to eq(true)+ end+ end+describe "when user is no longer authorized to close topics" dofab!(:user)diff --git a/spec/jobs/open_topic_spec.rb b/spec/jobs/open_topic_spec.rbindex 7a26466178c..946c459fc98 100644--- a/spec/jobs/open_topic_spec.rb+++ b/spec/jobs/open_topic_spec.rb@@ -51,6 +51,27 @@ RSpec.describe Jobs::OpenTopic doendend+ describe "when user is allowed to set topic timers" do+ fab!(:user)+ fab!(:group)+ fab!(:topic) { Fabricate(:topic_timer, status_type: TopicTimer.types[:open], user: user).topic }++ before do+ topic.update!(closed: true)+ group.add(user)+ user.reload+ SiteSetting.topic_timers_allowed_groups = group.id.to_s+ end++ it "opens the topic" do+ freeze_time(topic.public_topic_timer.execute_at + 1.minute)++ described_class.new.execute(topic_timer_id: topic.public_topic_timer.id)++ expect(topic.reload.open?).to eq(true)+ end+ end+describe "when user is no longer authorized to open topics" dofab!(:user)diff --git a/spec/lib/guardian/topic_guardian_spec.rb b/spec/lib/guardian/topic_guardian_spec.rbindex 588cd227f02..fb7762de1b9 100644--- a/spec/lib/guardian/topic_guardian_spec.rb+++ b/spec/lib/guardian/topic_guardian_spec.rb@@ -338,6 +338,25 @@ RSpec.describe TopicGuardian doendend+ describe "#can_set_topic_timer?" do+ it "uses topic_timers_allowed_groups and requires topic visibility" do+ expect(Guardian.new(admin).can_set_topic_timer?(topic)).to eq(true)+ expect(Guardian.new(moderator).can_set_topic_timer?(topic)).to eq(true)+ expect(Guardian.new(tl4_user).can_set_topic_timer?(topic)).to eq(true)+ expect(Guardian.new(tl3_user).can_set_topic_timer?(topic)).to eq(false)++ group.add(user)+ SiteSetting.topic_timers_allowed_groups = group.id.to_s++ inaccessible_private_topic =+ Fabricate(:topic, category: Fabricate(:private_category, group: Fabricate(:group)))++ expect(Guardian.new(user.reload).can_set_topic_timer?(topic)).to eq(true)+ expect(Guardian.new(tl4_user).can_set_topic_timer?(topic)).to eq(false)+ expect(Guardian.new(user).can_set_topic_timer?(inaccessible_private_topic)).to eq(false)+ end+ end+describe "#can_see_unlisted_topics?" doit "is allowed for staff users" doexpect(Guardian.new(moderator).can_see_unlisted_topics?).to eq(true)diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rbindex 52a7d635acf..a41dc3fb233 100644--- a/spec/models/topic_spec.rb+++ b/spec/models/topic_spec.rb@@ -2375,9 +2375,10 @@ RSpec.describe Topic doendit "sets topic status update user to topic creator if it is a TL4 user" do- tl4_topic = Fabricate.build(:topic, user: Fabricate.build(:trust_level_4, id: 998))+ topic_creator = Fabricate(:trust_level_4)+ tl4_topic = Fabricate.build(:topic, user: topic_creator)tl4_topic.set_or_create_timer(TopicTimer.types[:close], 3)- expect(tl4_topic.topic_timers.first.user_id).to eq(998)+ expect(tl4_topic.topic_timers.first.user).to eq(topic_creator)endit "removes close topic status update if arg is nil" dodiff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rbindex 4cf08cc3c91..54e73b6e27d 100644--- a/spec/requests/topics_controller_spec.rb+++ b/spec/requests/topics_controller_spec.rb@@ -6360,6 +6360,32 @@ RSpec.describe TopicsController doendend+ context "when logged in as a user in the topic timers allowed groups" do+ fab!(:topic_timer_group, :group)++ before do+ topic_timer_group.add(user)+ user.reload+ SiteSetting.topic_timers_allowed_groups = topic_timer_group.id.to_s+ sign_in(user)+ end++ it "allows creating a topic timer" do+ post "/t/#{topic.id}/timer.json", params: { time: "24", status_type: TopicTimer.types[1] }++ expect(response.status).to eq(200)+ expect(topic.reload.public_topic_timer.user).to eq(user)+ end++ it "requires delete permissions for destructive timers" do+ post "/t/#{topic.id}/timer.json", params: { time: "24", status_type: "delete" }++ expect(response.status).to eq(403)+ expect(response.parsed_body["error_type"]).to eq("invalid_access")+ expect(topic.reload.public_topic_timer).to eq(nil)+ end+ end+context "when time is in the past" doit "returns an error" dofreeze_timediff --git a/spec/serializers/current_user_serializer_spec.rb b/spec/serializers/current_user_serializer_spec.rbindex 6e2bb7e4157..f7da46d3ffe 100644--- a/spec/serializers/current_user_serializer_spec.rb+++ b/spec/serializers/current_user_serializer_spec.rb@@ -168,6 +168,19 @@ RSpec.describe CurrentUserSerializer doendend+ describe "#can_set_topic_timer" do+ let(:payload) { serializer.as_json }++ it "reflects the topic_timers_allowed_groups setting" do+ group = Fabricate(:group)+ group.add(user)+ user.reload+ SiteSetting.topic_timers_allowed_groups = group.id.to_s++ expect(payload[:can_set_topic_timer]).to eq(true)+ end+ end+describe "#pending_posts_count" dosubject(:pending_posts_count) { serializer.pending_posts_count }