FIX: Enforce tag group count validation before sending to review queue (#12728)

There is a category setting that enforces 1 or more tags must be added to a topic from a specific tag group before creating it. This validation was not being run before the topic was being sent to a review queue for categories that have that setting enabled.

There was an existing validation in `TopicCreator` but it was not correct; it was only validating when the tags did _not_ exist and also only happened on `create`. I now run the validation in `TopicCreator.valid?`

I also improved the error message shown to the user when they have not added the tags required (showing the tag names from the tag group), and changed the composer tag selector to not show "optional" if there are N tags required from a certain group.
This commit is contained in:
Martin Brennan
2021-04-19 09:43:50 +10:00
committed by GitHub
parent 8c6ea967ae
commit e3b1f5721b
10 changed files with 146 additions and 46 deletions

View File

@ -24,6 +24,15 @@ class TopicCreator
# this allows us to add errors
valid = topic.valid?
category = find_category
if category.present? && guardian.can_tag?(topic)
tags = @opts[:tags].present? ? Tag.where(name: @opts[:tags]) : (@opts[:tags] || [])
# both add to topic.errors
DiscourseTagging.validate_min_required_tags_for_category(guardian, topic, category, tags)
DiscourseTagging.validate_required_tags_from_group(guardian, topic, category, tags)
end
DiscourseEvent.trigger(:after_validate_topic, topic, self)
valid &&= topic.errors.empty?
@ -160,21 +169,12 @@ class TopicCreator
end
def setup_tags(topic)
if @opts[:tags].blank?
unless @guardian.is_staff? || !guardian.can_tag?(topic)
category = find_category
return if @opts[:tags].blank?
if !DiscourseTagging.validate_min_required_tags_for_category(@guardian, topic, category) ||
!DiscourseTagging.validate_required_tags_from_group(@guardian, topic, category)
rollback_from_errors!(topic)
end
end
else
valid_tags = DiscourseTagging.tag_topic_by_names(topic, @guardian, @opts[:tags])
unless valid_tags
topic.errors.add(:base, :unable_to_tag)
rollback_from_errors!(topic)
end
valid_tags = DiscourseTagging.tag_topic_by_names(topic, @guardian, @opts[:tags])
unless valid_tags
topic.errors.add(:base, :unable_to_tag)
rollback_from_errors!(topic)
end
end