FIX: dont allow assigning user to topic when post assigned (#259)

When post is already assigned to user/group then don't allow assigning a topic to that user/group.

Similarly, right now when topic is already assigned we are not allowing to assign a post that that user.
This commit is contained in:
Krzysztof Kotlarek 2021-12-06 06:09:04 +01:00 committed by GitHub
parent 2930cda049
commit 3eaefb60dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 15 deletions

View File

@ -181,7 +181,6 @@ class ::Assigner
end
def forbidden_reasons(assign_to:, type:)
forbidden_reason =
case
when assign_to.is_a?(User) && !can_assignee_see_target?(assign_to)
topic.private_message? ? :forbidden_assignee_not_pm_participant : :forbidden_assignee_cant_see_topic
@ -191,6 +190,8 @@ class ::Assigner
assign_to.is_a?(User) ? :forbidden_assign_to : :forbidden_group_assign_to
when topic.assignment&.assigned_to_id == assign_to.id && topic.assignment&.assigned_to_type == type
assign_to.is_a?(User) ? :already_assigned : :group_already_assigned
when @target.is_a?(Topic) && Assignment.where(topic_id: topic.id, target_type: "Post").any? { |assignment| assignment.assigned_to_id == assign_to.id && assignment.assigned_to_type == type }
assign_to.is_a?(User) ? :already_assigned : :group_already_assigned
when Assignment.where(topic: topic).count >= ASSIGNMENTS_PER_TOPIC_LIMIT
:too_many_assigns_for_topic
when !can_assign_to?(assign_to)

View File

@ -146,6 +146,32 @@ describe 'integration tests' do
end
end
context 'already assigned' do
fab!(:post) { Fabricate(:post) }
fab!(:post_2) { Fabricate(:post, topic: post.topic) }
let(:topic) { post.topic }
fab!(:user) { Fabricate(:user) }
include_context 'A group that is allowed to assign'
it 'does not allow to assign topic if post is already assigned' do
add_to_assign_allowed_group(user)
assigner = Assigner.new(post, user)
response = assigner.assign(user)
expect(response[:success]).to be true
assigner = Assigner.new(post_2, user)
response = assigner.assign(user)
expect(response[:success]).to be true
assigner = Assigner.new(topic, user)
response = assigner.assign(user)
expect(response[:success]).to be false
expect(response[:reason]).to eq(:already_assigned)
end
end
context 'move post' do
fab!(:old_topic) { Fabricate(:topic) }
fab!(:post) { Fabricate(:post, topic: old_topic) }