Fix #3272: Unicode tags are still being allowed.
* Don't allow adding tags with invalid names when they already exist in the tags table. * If an invalid tag is added, show an warning and ignore the tag instead of failing with a hard error. * Move the _(cosplay) tag validation into the tag name validator.
This commit is contained in:
@@ -17,14 +17,21 @@ class TagNameValidator < ActiveModel::EachValidator
|
||||
record.errors[attribute] << "'#{value}' cannot end with an underscore"
|
||||
when /__/
|
||||
record.errors[attribute] << "'#{value}' cannot contain consecutive underscores"
|
||||
when /[^[[:graph:]]]/
|
||||
when /[^[:graph:]]/
|
||||
record.errors[attribute] << "'#{value}' cannot contain non-printable characters"
|
||||
when /[^[[:ascii:]]]/
|
||||
when /[^[:ascii:]]/
|
||||
record.errors[attribute] << "'#{value}' must consist of only ASCII characters"
|
||||
when /\A(#{Tag::METATAGS.join("|")}):(.+)\z/i
|
||||
record.errors[attribute] << "'#{value}' cannot begin with '#{$1}:'"
|
||||
when /\A(#{Tag.categories.regexp}):(.+)\z/i
|
||||
record.errors[attribute] << "'#{value}' cannot begin with '#{$1}:'"
|
||||
when /\A(.+)_\(cosplay\)\z/i
|
||||
tag_name = TagAlias.to_aliased([$1]).first
|
||||
tag = Tag.find_by_name(tag_name)
|
||||
|
||||
if tag.present? && tag.category != Tag.categories.character
|
||||
record.errors[attribute] << "#{tag_name} must be a character tag"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -21,7 +21,6 @@ class Post < ApplicationRecord
|
||||
before_validation :remove_parent_loops
|
||||
validates_uniqueness_of :md5, :on => :create, message: ->(obj, data) { "duplicate: #{Post.find_by_md5(obj.md5).id}"}
|
||||
validates_inclusion_of :rating, in: %w(s q e), message: "rating must be s, q, or e"
|
||||
validate :tag_names_are_valid
|
||||
validate :added_tags_are_valid
|
||||
validate :removed_tags_are_valid
|
||||
validate :has_artist_tag
|
||||
@@ -662,12 +661,16 @@ class Post < ApplicationRecord
|
||||
set_tag_string(normalized_tags.join(" "))
|
||||
end
|
||||
|
||||
def remove_invalid_tags(tags)
|
||||
invalid_tags = Tag.invalid_cosplay_tags(tags)
|
||||
if invalid_tags.present?
|
||||
self.warnings[:base] << "The root tag must be a character tag: #{invalid_tags.map {|tag| "[b]#{tag}[/b]" }.join(", ")}"
|
||||
def remove_invalid_tags(tag_names)
|
||||
invalid_tags = tag_names.map { |name| Tag.new(name: name) }.select { |tag| tag.invalid?(:name) }
|
||||
|
||||
invalid_tags.each do |tag|
|
||||
tag.errors.messages.each do |attribute, messages|
|
||||
warnings[:base] << "Couldn't add tag: #{messages.join(';')}"
|
||||
end
|
||||
end
|
||||
tags - invalid_tags
|
||||
|
||||
tag_names - invalid_tags.map(&:name)
|
||||
end
|
||||
|
||||
def remove_negated_tags(tags)
|
||||
@@ -1755,22 +1758,6 @@ class Post < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
def tag_names_are_valid
|
||||
# only validate new tags; allow invalid names for tags that already exist.
|
||||
added_tags = tag_array - tag_array_was
|
||||
new_tags = added_tags - Tag.where(name: added_tags).pluck(:name)
|
||||
|
||||
new_tags.each do |name|
|
||||
tag = Tag.new
|
||||
tag.name = name
|
||||
tag.valid?
|
||||
|
||||
tag.errors.messages.each do |attribute, messages|
|
||||
errors[:tag_string] << "tag #{attribute} #{messages.join(';')}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def added_tags_are_valid
|
||||
new_tags = added_tags.select { |t| t.post_count <= 0 }
|
||||
new_general_tags = new_tags.select { |t| t.category == Tag.categories.general }
|
||||
|
||||
@@ -52,7 +52,8 @@ class Tag < ApplicationRecord
|
||||
has_many :antecedent_implications, -> {active}, :class_name => "TagImplication", :foreign_key => "antecedent_name", :primary_key => "name"
|
||||
has_many :consequent_implications, -> {active}, :class_name => "TagImplication", :foreign_key => "consequent_name", :primary_key => "name"
|
||||
|
||||
validates :name, uniqueness: true, tag_name: true, on: :create
|
||||
validates :name, tag_name: true, uniqueness: true, on: :create
|
||||
validates :name, tag_name: true, on: :name
|
||||
validates_inclusion_of :category, in: TagCategory.category_ids
|
||||
|
||||
before_save :update_category_cache, if: :category_changed?
|
||||
@@ -1019,14 +1020,6 @@ class Tag < ApplicationRecord
|
||||
cosplay_tags.grep(/\A(.+)_\(cosplay\)\Z/) { "#{TagAlias.to_aliased([$1]).first}_(cosplay)" } + other_tags
|
||||
end
|
||||
|
||||
def self.invalid_cosplay_tags(tags)
|
||||
tags.grep(/\A(.+)_\(cosplay\)\Z/) {|match| [match,TagAlias.to_aliased([$1]).first] }.
|
||||
select do |name|
|
||||
tag = Tag.find_by_name(name[1])
|
||||
!tag.nil? && tag.category != Tag.categories.character
|
||||
end.map {|tag| tag[0]}
|
||||
end
|
||||
|
||||
def editable_by?(user)
|
||||
return true if user.is_admin?
|
||||
return true if !is_locked? && user.is_builder? && post_count < 1_000
|
||||
|
||||
Reference in New Issue
Block a user