Skip to content

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

FIX: Resolve groups when bulk assigning topics (#41867)

Régis Hanol0f5ab6e94080
diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb
index 2216c41b686..d573798864b 100644
--- a/app/controllers/topics_controller.rb
+++ b/app/controllers/topics_controller.rb
@@ -1205,6 +1205,12 @@ class TopicsController < ApplicationController
render_json_dump result
rescue Discourse::InvalidParameters => ex
render_json_error(ex, status: 400)
+ rescue ActiveRecord::RecordInvalid => ex
+ render_json_error(ex, type: :record_invalid, status: 422)
+ rescue Discourse::InvalidAccess
+ render_json_error(I18n.t("invalid_access"), type: :invalid_access, status: 403)
+ rescue Discourse::NotFound
+ render_json_error(I18n.t("not_found"), type: :not_found, status: 404)
end
end
diff --git a/plugins/discourse-assign/app/controllers/discourse_assign/assign_controller.rb b/plugins/discourse-assign/app/controllers/discourse_assign/assign_controller.rb
index d17afb5a20b..1ce1a8c18c3 100644
--- a/plugins/discourse-assign/app/controllers/discourse_assign/assign_controller.rb
+++ b/plugins/discourse-assign/app/controllers/discourse_assign/assign_controller.rb
@@ -58,40 +58,29 @@ module DiscourseAssign
def assign
target_id = params.require(:target_id)
target_type = params.require(:target_type)
- username = params.permit(:username)["username"]
- group_name = params.permit(:group_name)["group_name"]
+ username = params.permit(:username)["username"].presence
+ group_name = params.permit(:group_name)["group_name"].presence
note = params.permit(:note)["note"].presence
status = params.permit(:status)["status"].presence
should_notify = params.permit(:should_notify)["should_notify"]
should_notify = (should_notify.present? ? should_notify.to_s == "true" : true)
- assign_to =
- (
- if username.present?
- User.find_by(username_lower: username.downcase)
- else
- Group.where("LOWER(name) = ?", group_name.downcase).first
- end
- )
-
- raise Discourse::NotFound unless assign_to
- guardian.ensure_can_see_group!(assign_to) if assign_to.is_a?(Group)
raise Discourse::NotFound if !Assignment.valid_type?(target_type)
+
target = target_type.constantize.where(id: target_id).first
raise Discourse::NotFound if target.blank? || !guardian.can_see?(target)
- assign =
- Assigner.new(target, current_user).assign(
- assign_to,
- note: note,
- status: status,
- should_notify: should_notify,
- )
+ assign_to = DiscourseAssign::AssigneeResolver.resolve!(guardian, username:, group_name:)
+
+ assign = Assigner.new(target, current_user).assign(assign_to, note:, status:, should_notify:)
if assign[:success]
render json: success_json
else
- render json: translate_failure(assign[:reason], assign_to), status: :bad_request
+ render json: {
+ error: Assigner.failure_message(assign[:reason], assign_to),
+ },
+ status: :bad_request
end
end
@@ -188,67 +177,6 @@ module DiscourseAssign
private
- def translate_failure(reason, assign_to)
- case reason
- when :forbidden_assigner_not_allowed
- { error: I18n.t("discourse_assign.forbidden_assigner_not_allowed") }
- when :already_assigned
- { error: I18n.t("discourse_assign.already_assigned", username: assign_to.username) }
- when :forbidden_assign_to
- { error: I18n.t("discourse_assign.forbidden_assign_to", username: assign_to.username) }
- when :forbidden_assignee_not_pm_participant
- {
- error:
- I18n.t(
- "discourse_assign.forbidden_assignee_not_pm_participant",
- username: assign_to.username,
- ),
- }
- when :forbidden_assignee_cant_see_topic
- {
- error:
- I18n.t(
- "discourse_assign.forbidden_assignee_cant_see_topic",
- username: assign_to.username,
- ),
- }
- when :group_already_assigned
- { error: I18n.t("discourse_assign.group_already_assigned", group: assign_to.name) }
- when :forbidden_group_assign_to
- { error: I18n.t("discourse_assign.forbidden_group_assign_to", group: assign_to.name) }
- when :forbidden_group_assignee_not_pm_participant
- {
- error:
- I18n.t(
- "discourse_assign.forbidden_group_assignee_not_pm_participant",
- group: assign_to.name,
- ),
- }
- when :forbidden_group_assignee_cant_see_topic
- {
- error:
- I18n.t(
- "discourse_assign.forbidden_group_assignee_cant_see_topic",
- group: assign_to.name,
- ),
- }
- when :too_many_assigns_for_topic
- {
- error:
- I18n.t(
- "discourse_assign.too_many_assigns_for_topic",
- limit: Assigner::ASSIGNMENTS_PER_TOPIC_LIMIT,
- ),
- }
- else
- max = SiteSetting.max_assigned_topics
- {
- error:
- I18n.t("discourse_assign.too_many_assigns", username: assign_to.username, max: max),
- }
- end
- end
-
def ensure_assign_allowed
raise Discourse::InvalidAccess.new unless current_user.can_assign?
end
diff --git a/plugins/discourse-assign/assets/javascripts/discourse/components/bulk-actions/assign-user.gjs b/plugins/discourse-assign/assets/javascripts/discourse/components/bulk-actions/assign-user.gjs
deleted file mode 100644
index 48293ac295a..00000000000
--- a/plugins/discourse-assign/assets/javascripts/discourse/components/bulk-actions/assign-user.gjs
+++ /dev/null
@@ -1,46 +0,0 @@
-import Component from "@glimmer/component";
-import { action } from "@ember/object";
-import { trackedObject } from "@ember/reactive/collections";
-import DButton from "discourse/ui-kit/d-button";
-import AssignUserForm from "../assign-user-form";
-
-export default class AssignUser extends Component {
- model = trackedObject({});
-
- // `submit` property will be mutated by the `AssignUserForm` component
- formApi = {
- submit() {},
- };
-
- @action
- async assign() {
- return this.args.performAndRefresh({
- type: "assign",
- username: this.model.username,
- status: this.model.status,
- note: this.model.note,
- });
- }
-
- <template>
- <div>
- <AssignUserForm
- @model={{this.model}}
- @onSubmit={{this.assign}}
- @formApi={{this.formApi}}
- />
- </div>
-
- <div>
- <DButton
- class="btn-primary"
- @action={{this.formApi.submit}}
- @label={{if
- this.model.reassign
- "discourse_assign.reassign.title"
- "discourse_assign.assign_modal.assign"
- }}
- />
- </div>
- </template>
-}
diff --git a/plugins/discourse-assign/assets/javascripts/discourse/components/bulk-actions/bulk-assign-user.gjs b/plugins/discourse-assign/assets/javascripts/discourse/components/bulk-actions/bulk-assign-user.gjs
index f70e2feba7e..77993939af3 100644
--- a/plugins/discourse-assign/assets/javascripts/discourse/components/bulk-actions/bulk-assign-user.gjs
+++ b/plugins/discourse-assign/assets/javascripts/discourse/components/bulk-actions/bulk-assign-user.gjs
@@ -3,6 +3,7 @@ import { action } from "@ember/object";
import { trackedObject } from "@ember/reactive/collections";
import didInsert from "@ember/render-modifiers/modifiers/did-insert";
import AssignUserForm from "discourse/plugins/discourse-assign/discourse/components/assign-user-form";
+import assignmentPayload from "discourse/plugins/discourse-assign/discourse/lib/assignment-payload";
export default class BulkActionsAssignUser extends Component {
model = trackedObject({});
@@ -12,25 +13,23 @@ export default class BulkActionsAssignUser extends Component {
};
@action
- async assign(performAndRefreshCallback) {
- return performAndRefreshCallback({
+ async submit() {
+ return await this.args.onPerform({
type: "assign",
- username: this.model.username,
- status: this.model.status,
- note: this.model.note,
+ ...assignmentPayload(this.model),
});
}
@action
performRegistration() {
- this.args.onRegisterAction?.(this.assign.bind(this));
+ this.args.onRegisterAction?.(() => this.formApi.submit());
}
<template>
<span {{didInsert this.performRegistration}}></span>
<AssignUserForm
@model={{this.model}}
- @onSubmit={{this.assign}}
+ @onSubmit={{this.submit}}
@formApi={{this.formApi}}
/>
</template>
diff --git a/plugins/discourse-assign/assets/javascripts/discourse/controllers/bulk-assign.js b/plugins/discourse-assign/assets/javascripts/discourse/controllers/bulk-assign.js
deleted file mode 100644
index 79090b121fd..00000000000
--- a/plugins/discourse-assign/assets/javascripts/discourse/controllers/bulk-assign.js
+++ /dev/null
@@ -1,22 +0,0 @@
-import Controller, { inject as controller } from "@ember/controller";
-import { action } from "@ember/object";
-
-export default class BulkAssign extends Controller {
- @controller topicBulkActions;
-
- model;
-
- // `submit` property will be mutated by the `AssignUserForm` component
- formApi = {
- submit() {},
- };
-
- @action
- async assign() {
- return this.topicBulkActions.performAndRefresh({
- type: "assign",
- username: this.model.username,
- note: this.model.note,
- });
- }
-}
diff --git a/plugins/discourse-assign/assets/javascripts/discourse/initializers/extend-for-assigns.js b/plugins/discourse-assign/assets/javascripts/discourse/initializers/extend-for-assigns.js
index cc1b9806483..3dc0b1eae47 100644
--- a/plugins/discourse-assign/assets/javascripts/discourse/initializers/extend-for-assigns.js
+++ b/plugins/discourse-assign/assets/javascripts/discourse/initializers/extend-for-assigns.js
@@ -713,8 +713,8 @@ export default {
class: "assign-topics",
description: "topics.bulk.assign_description",
confirmButtonTranslationKey: "topics.bulk.confirm_assign_topics",
- action({ setComponent }) {
- setComponent(BulkActionsAssignUser);
+ action({ setComponent, performAndRefresh }) {
+ setComponent(BulkActionsAssignUser, { onPerform: performAndRefresh });
},
actionType: "setComponent",
});
diff --git a/plugins/discourse-assign/assets/javascripts/discourse/lib/assignment-payload.js b/plugins/discourse-assign/assets/javascripts/discourse/lib/assignment-payload.js
new file mode 100644
index 00000000000..35cad82d30e
--- /dev/null
+++ b/plugins/discourse-assign/assets/javascripts/discourse/lib/assignment-payload.js
@@ -0,0 +1,8 @@
+export default function assignmentPayload(assignment) {
+ return {
+ username: assignment.username,
+ group_name: assignment.group_name,
+ note: assignment.note,
+ status: assignment.status,
+ };
+}
diff --git a/plugins/discourse-assign/assets/javascripts/discourse/services/task-actions.js b/plugins/discourse-assign/assets/javascripts/discourse/services/task-actions.js
index f7f92a7ba7c..276ebc21d4d 100644
--- a/plugins/discourse-assign/assets/javascripts/discourse/services/task-actions.js
+++ b/plugins/discourse-assign/assets/javascripts/discourse/services/task-actions.js
@@ -4,6 +4,7 @@ import { isEmpty } from "@ember/utils";
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import AssignUser from "../components/modal/assign-user";
+import assignmentPayload from "../lib/assignment-payload";
export default class TaskActions extends Service {
@service modal;
@@ -173,12 +174,9 @@ export default class TaskActions extends Service {
await ajax(path, {
type: "PUT",
data: {
- username: model.username,
- group_name: model.group_name,
+ ...assignmentPayload(model),
target_id: model.target.id,
target_type: model.targetType,
- note: model.note,
- status: model.status,
},
});
@@ -192,12 +190,9 @@ export default class TaskActions extends Service {
await ajax("/assign/assign", {
type: "PUT",
data: {
- username: assignment.username,
- group_name: assignment.group_name,
+ ...assignmentPayload(assignment),
target_id: assignment.targetId,
target_type: assignment.targetType,
- note: assignment.note,
- status: assignment.status,
},
});
}
diff --git a/plugins/discourse-assign/assets/javascripts/discourse/templates/modal/bulk-assign.gjs b/plugins/discourse-assign/assets/javascripts/discourse/templates/modal/bulk-assign.gjs
deleted file mode 100644
index 9cbb60501ee..00000000000
--- a/plugins/discourse-assign/assets/javascripts/discourse/templates/modal/bulk-assign.gjs
+++ /dev/null
@@ -1,24 +0,0 @@
-import DButton from "discourse/ui-kit/d-button";
-import AssignUserForm from "../../components/assign-user-form";
-
-export default <template>
- <div>
- <AssignUserForm
- @model={{@controller.model}}
- @onSubmit={{@controller.assign}}
- @formApi={{@controller.formApi}}
- />
- </div>
-
- <div>
- <DButton
- class="btn-primary"
- @action={{@controller.formApi.submit}}
- @label={{if
- @controller.model.reassign
- "discourse_assign.reassign.title"
- "discourse_assign.assign_modal.assign"
- }}
- />
- </div>
-</template>
diff --git a/plugins/discourse-assign/config/locales/server.en.yml b/plugins/discourse-assign/config/locales/server.en.yml
index e627b2cf4c7..9fb05401a08 100644
--- a/plugins/discourse-assign/config/locales/server.en.yml
+++ b/plugins/discourse-assign/config/locales/server.en.yml
@@ -36,6 +36,7 @@ en:
nobody: "Filter topics that are not assigned to anyone"
anyone: "Filter topics assigned to anyone"
already_claimed: "That topic has already been claimed."
+ no_assignee: "No assignee was selected."
already_assigned: "Topic is already assigned to @%{username}"
too_many_assigns: "@%{username} has already reached the maximum number of assigned topics (%{max})."
too_many_assigns_for_topic: "Limit of %{limit} assignments per topic has been reached."
diff --git a/plugins/discourse-assign/lib/assigner.rb b/plugins/discourse-assign/lib/assigner.rb
index 03b641f5c2e..9042fbf1edf 100644
--- a/plugins/discourse-assign/lib/assigner.rb
+++ b/plugins/discourse-assign/lib/assigner.rb
@@ -115,6 +115,18 @@ class ::Assigner
end
end
+ def self.failure_message(reason, assign_to)
+ name = assign_to.is_a?(User) ? assign_to.username : assign_to&.name
+
+ I18n.t(
+ "discourse_assign.#{reason}",
+ username: name,
+ group: name,
+ limit: ASSIGNMENTS_PER_TOPIC_LIMIT,
+ max: SiteSetting.max_assigned_topics,
+ )
+ end
+
def self.publish_topic_tracking_state(topic, user_id)
if topic.private_message?
MessageBus.publish("/private-messages/assigned", { topic_id: topic.id }, user_ids: [user_id])
@@ -260,17 +272,30 @@ class ::Assigner
allow_self_reassign: false,
should_notify: true
)
+ return { success: false, reason: :no_assignee } if assign_to.blank?
+
assigned_to_type = assign_to.is_a?(User) ? "User" : "Group"
- if !@assigned_by&.guardian&.can_assign?(@target)
+ if !guardian.can_assign?(@target)
return { success: false, reason: :forbidden_assigner_not_allowed }
end
if topic.private_message? && SiteSetting.invite_on_assign
- if assigned_to_type == "Group"
- invite_group(assign_to, should_notify)
- else
- invite_user(assign_to)
+ group = assign_to.is_a?(Group)
+
+ begin
+ group ? invite_group(assign_to, should_notify) : invite_user(assign_to)
+ rescue Discourse::InvalidAccess,
+ Topic::NotAllowed,
+ Topic::UserExists,
+ RateLimiter::LimitExceeded
+ reason =
+ if group
+ :forbidden_group_assignee_not_pm_participant
+ else
+ :forbidden_assignee_not_pm_participant
+ end
+ return { success: false, reason: }
end
end
@@ -323,7 +348,7 @@ class ::Assigner
status: status,
)
- first_post.publish_change_to_clients!(:revised, reload_topic: true)
+ first_post&.publish_change_to_clients!(:revised, reload_topic: true)
queue_notification(assignment) if should_notify
# This assignment should never be notified
@@ -559,7 +584,8 @@ class ::Assigner
end
def no_assignee_change?(assignee)
- @target.assignment&.assigned_to_id == assignee.id
+ @target.assignment&.assigned_to_id == assignee.id &&
+ @target.assignment&.assigned_to_type == assignee.class.name
end
def details_change?(note, status)
diff --git a/plugins/discourse-assign/lib/discourse_assign/assignee_resolver.rb b/plugins/discourse-assign/lib/discourse_assign/assignee_resolver.rb
new file mode 100644
index 00000000000..c979b36f257
--- /dev/null
+++ b/plugins/discourse-assign/lib/discourse_assign/assignee_resolver.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+
+module DiscourseAssign
+ module AssigneeResolver
+ def self.resolve!(guardian, username: nil, group_name: nil)
+ username = username.to_s.strip.presence
+ group_name = group_name.to_s.strip.presence
+
+ raise Discourse::InvalidParameters.new(:assignee) if username.blank? && group_name.blank?
+
+ return User.find_by_username(username) || raise(Discourse::NotFound) if username
+
+ group = Group.find_by("LOWER(name) = ?", group_name.downcase)
+ raise Discourse::NotFound if group.blank?
+ guardian.ensure_can_see_group!(group)
+
+ group
+ end
+ end
+end
diff --git a/plugins/discourse-assign/lib/discourse_workflows/nodes/assign_topic/v1.rb b/plugins/discourse-assign/lib/discourse_workflows/nodes/assign_topic/v1.rb
index e7bfb071273..1d10d2b6b4b 100644
--- a/plugins/discourse-assign/lib/discourse_workflows/nodes/assign_topic/v1.rb
+++ b/plugins/discourse-assign/lib/discourse_workflows/nodes/assign_topic/v1.rb
@@ -103,7 +103,7 @@ if defined?(DiscourseWorkflows)
raise_node_error!(
I18n.t(
"discourse_assign.discourse_workflows.assign_topic.error",
- reason: result[:reason],
+ reason: Assigner.failure_message(result[:reason], assignee),
),
)
end
diff --git a/plugins/discourse-assign/plugin.rb b/plugins/discourse-assign/plugin.rb
index 50c64282ef2..f31b0a7f68c 100644
--- a/plugins/discourse-assign/plugin.rb
+++ b/plugins/discourse-assign/plugin.rb
@@ -19,6 +19,7 @@ module ::DiscourseAssign
end
require_relative "lib/discourse_assign/engine"
+require_relative "lib/discourse_assign/assignee_resolver"
require_relative "lib/discourse_assign/assignment_permissions"
require_relative "lib/discourse_assign/guardian_extensions"
require_relative "lib/validators/assign_statuses_validator"
@@ -708,27 +709,43 @@ after_initialize do
# TopicsBulkAction
TopicsBulkAction.register_operation("assign") do
- if @user.can_assign?
- assign_user = User.find_by_username(@operation[:username])
- topics.each do |topic|
- next if [email protected]_assign?(topic)
+ raise Discourse::InvalidAccess if !guardian.can_assign?
+
+ assign_to =
+ DiscourseAssign::AssigneeResolver.resolve!(
+ guardian,
+ username: @operation[:username],
+ group_name: @operation[:group_name],
+ )
+
+ topics.each do |topic|
+ next if !guardian.can_see?(topic)
+ result =
Assigner.new(topic, @user).assign(
- assign_user,
- status: @operation[:status],
- note: @operation[:note],
+ assign_to,
+ status: @operation[:status].presence,
+ note: @operation[:note].presence,
)
+
+ if result[:success] || %i[already_assigned group_already_assigned].include?(result[:reason])
+ @changed_ids << topic.id
+ else
+ @errors[Assigner.failure_message(result[:reason], assign_to)] += 1
end
end
end
TopicsBulkAction.register_operation("unassign") do
- if @user.can_assign?
- topics.each { |topic| Assigner.new(topic, @user).unassign if guardian.can_assign?(topic) }
+ topics.each do |topic|
+ next if !guardian.can_see?(topic) || !guardian.can_assign?(topic)
+
+ Assigner.new(topic, @user).unassign
end
end
register_permitted_bulk_action_parameter :username
+ register_permitted_bulk_action_parameter :group_name
register_permitted_bulk_action_parameter :status
register_permitted_bulk_action_parameter :note
diff --git a/plugins/discourse-assign/spec/components/topics_bulk_action_spec.rb b/plugins/discourse-assign/spec/components/topics_bulk_action_spec.rb
index 9ab57e8291a..22d903992c7 100644
--- a/plugins/discourse-assign/spec/components/topics_bulk_action_spec.rb
+++ b/plugins/discourse-assign/spec/components/topics_bulk_action_spec.rb
@@ -17,45 +17,93 @@ describe TopicsBulkAction do
before { add_to_assign_allowed_group(user) }
describe "assign_topics" do
- it "assigns multiple topics to user" do
- TopicsBulkAction.new(
- user,
- [post.topic.id, post1.topic.id],
- { type: "assign", username: user.username, note: "foobar" },
- ).perform!
+ it "assigns multiple topics to a user" do
+ changed_ids =
+ TopicsBulkAction.new(
+ user,
+ [post.topic.id, post1.topic.id],
+ { type: "assign", username: user.username, note: "foobar" },
+ ).perform!
+
+ expect(changed_ids).to contain_exactly(post.topic.id, post1.topic.id)
+ expect(post.topic.reload.assignment).to have_attributes(
+ assigned_to: user,
+ assigned_to_type: "User",
+ note: "foobar",
+ )
+ expect(post1.topic.reload.assignment.assigned_to).to eq(user)
+ end
- assigned_topics = TopicQuery.new(user, { page: 0 }).list_messages_assigned(user).topics
+ it "reports a user who does not belong to an assign allowed group" do
+ action =
+ TopicsBulkAction.new(
+ user,
+ [post.topic.id, post1.topic.id],
+ { type: "assign", username: user2.username },
+ )
+
+ expect(action.perform!).to be_empty
+ expect(action.errors).to eq(
+ I18n.t("discourse_assign.forbidden_assign_to", username: user2.username) => 2,
+ )
+ end
- expect(assigned_topics.length).to eq(2)
+ it "refuses a user who is not allowed to assign" do
+ expect {
+ TopicsBulkAction.new(
+ user2,
+ [post.topic.id, post1.topic.id, post2.topic.id],
+ { type: "assign", username: user.username },
+ ).perform!
+ }.to raise_error(Discourse::InvalidAccess)
+
+ expect(Assignment.count).to eq(0)
+ end
- expect(assigned_topics).to contain_exactly(post.topic, post1.topic)
+ it "reports a group that cannot be assigned" do
+ unassignable_group = Fabricate(:group)
- expect(post.topic.assignment.note).to eq "foobar"
- expect(post1.topic.assignment.note).to eq "foobar"
+ action =
+ TopicsBulkAction.new(
+ user,
+ [post.topic.id, post1.topic.id],
+ { type: "assign", group_name: unassignable_group.name },
+ )
+
+ expect(action.perform!).to be_empty
+ expect(action.errors).to eq(
+ I18n.t("discourse_assign.forbidden_group_assign_to", group: unassignable_group.name) => 2,
+ )
end
- it "doesn't allows to assign to user not in assign_allowed_group" do
- TopicsBulkAction.new(
- user,
- [post.topic.id, post1.topic.id],
- { type: "assign", username: user2.username },
- ).perform!
+ it "does not report topics already assigned to the same assignee" do
+ Assigner.new(post.topic, user).assign(user)
- assigned_topics = TopicQuery.new(user, { page: 0 }).list_messages_assigned(user2).topics
+ action =
+ TopicsBulkAction.new(
+ user,
+ [post.topic.id, post1.topic.id],
+ { type: "assign", username: user.username },
+ )
- expect(assigned_topics.length).to eq(0)
+ expect(action.perform!).to contain_exactly(post.topic.id, post1.topic.id)
+ expect(action.errors).to be_empty
end
- it "user who is not in assign_allowed_group can't assign topics" do
- TopicsBulkAction.new(
- user2,
- [post.topic.id, post1.topic.id, post2.topic.id],
- { type: "assign", username: user.username },
- ).perform!
+ it "skips topics the acting user cannot see" do
+ secret_topic =
+ Fabricate(:topic, category: Fabricate(:private_category, group: Fabricate(:group)))
- assigned_topics = TopicQuery.new(user, { page: 0 }).list_messages_assigned(user).topics
+ action =
+ TopicsBulkAction.new(
+ user,
+ [secret_topic.id, post.topic.id],
+ { type: "assign", group_name: assign_allowed_group.name },
+ )
- expect(assigned_topics.length).to eq(0)
+ expect(action.perform!).to contain_exactly(post.topic.id)
+ expect(action.errors).to be_empty
+ expect(secret_topic.reload.assignment).to be_blank
end
end
diff --git a/plugins/discourse-assign/spec/lib/assigner_spec.rb b/plugins/discourse-assign/spec/lib/assigner_spec.rb
index 011ee42d4dd..4a24c931c04 100644
--- a/plugins/discourse-assign/spec/lib/assigner_spec.rb
+++ b/plugins/discourse-assign/spec/lib/assigner_spec.rb
@@ -21,6 +21,11 @@ RSpec.describe Assigner do
let(:assigner) { described_class.new(topic, moderator_2) }
let(:assigner_self) { described_class.new(topic, moderator) }
+ it "returns a failure instead of raising when there is no assignee" do
+ expect(assigner.assign(nil)).to eq({ success: false, reason: :no_assignee })
+ expect(topic.reload.assignment).to be_blank
+ end
+
it "can assign and unassign correctly" do
expect_enqueued_with(job: :assign_notification) { assigner.assign(moderator) }
@@ -1001,7 +1006,11 @@ RSpec.describe Assigner do
messageable_level: Group::ALIAS_LEVELS[:nobody],
)
group.add(Fabricate(:user))
- expect { assigner.assign(group) }.to raise_error(Discourse::InvalidAccess)
+ expect(assigner.assign(group)).to eq(
+ { success: false, reason: :forbidden_group_assignee_not_pm_participant },
+ )
+ expect(topic.reload.allowed_groups).to be_empty
+ expect(topic.assignment).to be_blank
end
end
diff --git a/plugins/discourse-assign/spec/requests/assign_controller_spec.rb b/plugins/discourse-assign/spec/requests/assign_controller_spec.rb
index 73b08dbc236..562c53c7bf9 100644
--- a/plugins/discourse-assign/spec/requests/assign_controller_spec.rb
+++ b/plugins/discourse-assign/spec/requests/assign_controller_spec.rb
@@ -277,6 +277,13 @@ RSpec.describe DiscourseAssign::AssignController do
expect(post.topic.reload.assignment).to be_nil
end
+ it "returns a 400 when neither a username nor a group name is given" do
+ put "/assign/assign.json", params: { target_id: post.topic_id, target_type: "Topic" }
+
+ expect(response.status).to eq(400)
+ expect(post.topic.reload.assignment).to be_nil
+ end
+
it "assigns topic with note to a user" do
put "/assign/assign.json",
params: {
diff --git a/plugins/discourse-assign/spec/requests/topics_bulk_assign_spec.rb b/plugins/discourse-assign/spec/requests/topics_bulk_assign_spec.rb
new file mode 100644
index 00000000000..7e7c3d7ad96
--- /dev/null
+++ b/plugins/discourse-assign/spec/requests/topics_bulk_assign_spec.rb
@@ -0,0 +1,143 @@
+# frozen_string_literal: true
+
+require_relative "../support/assign_allowed_group"
+
+RSpec.describe TopicsController do
+ include_context "with group that is allowed to assign"
+
+ fab!(:actor, :user)
+ fab!(:allowed_user, :user)
+ fab!(:topic1) { Fabricate(:post).topic }
+ fab!(:topic2) { Fabricate(:post).topic }
+
+ before do
+ SiteSetting.assign_enabled = true
+ SiteSetting.enable_assign_status = true
+ add_to_assign_allowed_group(actor)
+ sign_in(actor)
+ end
+
+ def bulk_assign(operation, ids: [topic1.id, topic2.id])
+ put "/topics/bulk.json", params: { topic_ids: ids, operation: operation }
+ end
+
+ describe "#bulk with the assign operation" do
+ it "assigns the selected topics to a group, carrying the note and the status" do
+ bulk_assign(
+ {
+ type: "assign",
+ group_name: assign_allowed_group.name,
+ note: "handle this",
+ status: "In Progress",
+ },
+ )
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["topic_ids"]).to contain_exactly(topic1.id, topic2.id)
+ expect(topic1.reload.assignment).to have_attributes(
+ assigned_to: assign_allowed_group,
+ assigned_to_type: "Group",
+ note: "handle this",
+ status: "In Progress",
+ )
+ expect(topic2.reload.assignment.assigned_to).to eq(assign_allowed_group)
+ end
+
+ it "returns a 400 when the operation names no assignee" do
+ bulk_assign({ type: "assign" })
+
+ expect(response.status).to eq(400)
+ expect(Assignment.count).to eq(0)
+ end
+
+ it "returns a 404 when the group name matches no group" do
+ bulk_assign({ type: "assign", group_name: "no-such-group" })
+
+ expect(response.status).to eq(404)
+ end
+
+ it "returns a 422 when the status is not one of the configured statuses" do
+ bulk_assign({ type: "assign", group_name: assign_allowed_group.name, status: "Bogus" })
+
+ expect(response.status).to eq(422)
+ expect(Assignment.count).to eq(0)
+ end
+
+ it "returns a 403 without disclosing a group the acting user cannot see" do
+ hidden_group =
+ Fabricate(
+ :group,
+ visibility_level: Group.visibility_levels[:staff],
+ assignable_level: Group::ALIAS_LEVELS[:everyone],
+ )
+
+ bulk_assign({ type: "assign", group_name: hidden_group.name })
+
+ expect(response.status).to eq(403)
+ expect(response.body).not_to include(hidden_group.name)
+ expect(Assignment.count).to eq(0)
+ end
+
+ it "returns a 403 when the acting user is not allowed to assign" do
+ sign_in(Fabricate(:user))
+
+ bulk_assign({ type: "assign", group_name: assign_allowed_group.name })
+
+ expect(response.status).to eq(403)
+ expect(Assignment.count).to eq(0)
+ end
+
+ it "returns the per-topic failures alongside the topics that succeeded" do
+ add_to_assign_allowed_group(allowed_user)
+ private_group = Fabricate(:group, users: [actor])
+ hidden_topic = Fabricate(:topic, category: Fabricate(:private_category, group: private_group))
+
+ bulk_assign(
+ { type: "assign", username: allowed_user.username },
+ ids: [hidden_topic.id, topic2.id],
+ )
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["topic_ids"]).to contain_exactly(topic2.id)
+ expect(response.parsed_body["errors"]).to eq(
+ I18n.t(
+ "discourse_assign.forbidden_assignee_cant_see_topic",
+ username: allowed_user.username,
+ ) =>
+ 1,
+ )
+ end
+
+ it "reports the topics already assigned to the same assignee as succeeded" do
+ Assigner.new(topic1, actor).assign(assign_allowed_group)
+
+ bulk_assign({ type: "assign", group_name: assign_allowed_group.name })
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["topic_ids"]).to contain_exactly(topic1.id, topic2.id)
+ expect(response.parsed_body["errors"]).to be_blank
+ end
+
+ it "keeps assigning the rest of the selection when a PM invite is refused" do
+ SiteSetting.invite_on_assign = true
+ pm = Fabricate(:private_message_topic, user: actor, recipient: allowed_user)
+ Fabricate(:post, topic: pm)
+ assign_allowed_group.add(Fabricate(:user))
+
+ bulk_assign(
+ { type: "assign", group_name: assign_allowed_group.name },
+ ids: [topic1.id, pm.id],
+ )
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["topic_ids"]).to contain_exactly(topic1.id)
+ expect(response.parsed_body["errors"]).to eq(
+ I18n.t(
+ "discourse_assign.forbidden_group_assignee_not_pm_participant",
+ group: assign_allowed_group.name,
+ ) =>
+ 1,
+ )
+ end
+ end
+end
diff --git a/plugins/discourse-assign/spec/system/bulk_assign_spec.rb b/plugins/discourse-assign/spec/system/bulk_assign_spec.rb
index 179e0c5d390..46c6fe602af 100644
--- a/plugins/discourse-assign/spec/system/bulk_assign_spec.rb
+++ b/plugins/discourse-assign/spec/system/bulk_assign_spec.rb
@@ -7,6 +7,7 @@ describe "Assign | Bulk Assign" do
let(:topic_list) { PageObjects::Components::TopicList.new }
fab!(:staff_user) { Fabricate(:user, groups: [Group[:staff]]) }
fab!(:admin)
+ fab!(:assignable_group) { Fabricate(:group, assignable_level: Group::ALIAS_LEVELS[:everyone]) }
fab!(:topics) { Fabricate.times(10, :post).map(&:topic) }
before do
@@ -33,16 +34,7 @@ describe "Assign | Bulk Assign" do
expect(topic_list_header).to have_bulk_select_modal
# Assign User
- assignee = staff_user.username
- select_kit = PageObjects::Components::SelectKit.new("#assignee-chooser")
-
- # This initial collapse is needed because for some reason the modal is
- # opening with `is-expanded` property, but it isn't actually expanded.
- select_kit.collapse
-
- select_kit.search(assignee)
- select_kit.select_row_by_value(assignee)
- select_kit.collapse
+ assign_modal.assignee = staff_user
# Click Confirm
topic_list_header.click_bulk_topics_confirm
@@ -72,5 +64,27 @@ describe "Assign | Bulk Assign" do
visit "/latest"
expect(topic_list).to have_unassigned_status(topic)
end
+
+ it "can assign topics to a group" do
+ visit "/latest"
+ topic = topics.first
+
+ topic_list_header.click_bulk_select_button
+ topic_list.click_topic_checkbox(topic)
+
+ topic_list_header.click_bulk_select_topics_dropdown
+ expect(topic_list_header).to have_assign_topics_button
+ topic_list_header.click_assign_topics_button
+ expect(topic_list_header).to have_bulk_select_modal
+
+ assign_modal.assignee = assignable_group
+
+ topic_list_header.click_bulk_topics_confirm
+
+ expect(assign_modal).to be_closed
+
+ visit "/latest"
+ expect(topic_list).to have_assigned_status(topic)
+ end
end
end
diff --git a/plugins/discourse-assign/test/javascripts/integration/components/bulk-assign-user-test.gjs b/plugins/discourse-assign/test/javascripts/integration/components/bulk-assign-user-test.gjs
new file mode 100644
index 00000000000..c43aecfef9c
--- /dev/null
+++ b/plugins/discourse-assign/test/javascripts/integration/components/bulk-assign-user-test.gjs
@@ -0,0 +1,44 @@
+import { render, settled } from "@ember/test-helpers";
+import { module, test } from "qunit";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import pretender, { response } from "discourse/tests/helpers/create-pretender";
+import { i18n } from "discourse-i18n";
+import BulkActionsAssignUser from "discourse/plugins/discourse-assign/discourse/components/bulk-actions/bulk-assign-user";
+
+module("Integration | Component | BulkActionsAssignUser", function (hooks) {
+ setupRenderingTest(hooks);
+
+ hooks.beforeEach(function () {
+ pretender.get("/assign/suggestions", () =>
+ response({
+ suggestions: [],
+ assign_allowed_on_groups: [],
+ assign_allowed_for_groups: [],
+ })
+ );
+ });
+
+ test("does not submit when no assignee has been chosen", async function (assert) {
+ let registeredAction;
+ let performed = false;
+ const onRegisterAction = (callback) => (registeredAction = callback);
+ const onPerform = () => (performed = true);
+
+ await render(
+ <template>
+ <BulkActionsAssignUser
+ @onRegisterAction={{onRegisterAction}}
+ @onPerform={{onPerform}}
+ />
+ </template>
+ );
+
+ await registeredAction();
+ await settled();
+
+ assert.false(performed, "no bulk operation is sent");
+ assert
+ .dom(".assignee-error .error-label")
+ .hasText(i18n("discourse_assign.assign_modal.choose_assignee"));
+ });
+});