FIX: do not allow tag with name 'none' (#9867)

https://meta.discourse.org/t/none-tag-is-uneditable/152003
This commit is contained in:
Arpit Jalan 2020-05-26 08:15:45 +05:30 committed by GitHub
parent 878f06f1fe
commit 5462fe9462
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 3 deletions

View File

@ -5,15 +5,16 @@ class Tag < ActiveRecord::Base
include HasDestroyedWebHook
RESERVED_TAGS = [
'c'
'c',
'none'
]
validates :name,
presence: true,
uniqueness: { case_sensitive: false },
exclusion: { in: RESERVED_TAGS }
uniqueness: { case_sensitive: false }
validate :target_tag_validator, if: Proc.new { |t| t.new_record? || t.will_save_change_to_target_tag_id? }
validate :name_validator
scope :where_name, ->(name) do
name = Array(name).map(&:downcase)
@ -181,6 +182,14 @@ class Tag < ActiveRecord::Base
true
end
end
private
def name_validator
if name.present? && RESERVED_TAGS.include?(self.name.strip.downcase)
errors.add(:name, :invalid)
end
end
end
# == Schema Information

View File

@ -0,0 +1,17 @@
# frozen_string_literal: true
class RemoveNoneTags < ActiveRecord::Migration[6.0]
def up
none_tag_id = DB.query_single("SELECT id FROM tags WHERE lower(name) = 'none'").first
if none_tag_id.present?
[:tag_users, :topic_tags, :category_tag_stats, :category_tags, :tag_group_memberships].each do |table_name|
execute "DELETE FROM #{table_name} WHERE tag_id = #{none_tag_id}"
end
execute "DELETE FROM tags WHERE id = #{none_tag_id} OR target_tag_id = #{none_tag_id}"
end
end
def down
raise ActiveRecord::IrreversibleMigration
end
end

View File

@ -36,6 +36,9 @@ describe Tag do
expect { Fabricate.build(:tag, name: "hElLo").save! }.to raise_error(ActiveRecord::RecordInvalid)
end
it 'does not allow creation of tag with name in "RESERVED_TAGS"' do
expect { Fabricate.build(:tag, name: "None").save! }.to raise_error(ActiveRecord::RecordInvalid)
end
end
describe 'destroy' do