#
# Indexes
#
-# users_auth_idx (auth_provider,auth_uid) UNIQUE
-# users_display_name_idx (display_name) UNIQUE
-# users_display_name_lower_idx (lower((display_name)::text))
-# users_email_idx (email) UNIQUE
-# users_email_lower_idx (lower((email)::text))
-# users_home_idx (home_tile)
+# users_auth_idx (auth_provider,auth_uid) UNIQUE
+# users_display_name_canonical_idx (lower(NORMALIZE(display_name, NFKC)))
+# users_display_name_idx (display_name) UNIQUE
+# users_display_name_lower_idx (lower((display_name)::text))
+# users_email_idx (email) UNIQUE
+# users_email_lower_idx (lower((email)::text))
+# users_home_idx (home_tile)
#
class User < ApplicationRecord
validates :display_name, :presence => true, :length => 3..255,
:exclusion => %w[new terms save confirm confirm-email go_public reset-password forgot-password suspended]
validates :display_name, :if => proc { |u| u.display_name_changed? },
- :uniqueness => { :case_sensitive => false }
+ :normalized_uniqueness => { :case_sensitive => false }
validates :display_name, :if => proc { |u| u.display_name_changed? },
:characters => { :url_safe => true },
:whitespace => { :leading => false, :trailing => false }
user = find_by("email = ? OR display_name = ?", options[:username].strip, options[:username])
if user.nil?
- users = where("LOWER(email) = LOWER(?) OR LOWER(display_name) = LOWER(?)", options[:username].strip, options[:username])
+ users = where("LOWER(email) = LOWER(?) OR LOWER(NORMALIZE(display_name, NFKC)) = LOWER(NORMALIZE(?, NFKC))", options[:username].strip, options[:username])
user = users.first if users.count == 1
end
--- /dev/null
+class NormalizedUniquenessValidator < ActiveModel::EachValidator
+ def validate_each(record, attribute, value)
+ relation = if options.fetch(:case_sensitive, true)
+ record.class.where("NORMALIZE(#{attribute}, NFKC) = NORMALIZE(?, NFKC)", value)
+ else
+ record.class.where("LOWER(NORMALIZE(#{attribute}, NFKC)) = LOWER(NORMALIZE(?, NFKC))", value)
+ end
+
+ relation = relation.where.not(record.class.primary_key => [record.id_in_database]) if record.persisted?
+
+ if relation.exists?
+ error_options = options.except(:case_sensitive)
+ error_options[:value] = value
+
+ record.errors.add(attribute, :taken, **error_options)
+ end
+ end
+end
end
def test_unique_display_name
- existing_user = create(:user)
- new_user = build(:user, :display_name => existing_user.display_name)
- assert_not new_user.save
- assert_includes new_user.errors[:display_name], "has already been taken"
+ create(:user, :display_name => "H\u{e9}nryIV")
+
+ %W[H\u{e9}nryIV he\u{301}nryiv H\u{c9}nry\u2163 he\u{301}nry\u2173].each do |name|
+ new_user = build(:user, :display_name => name)
+ assert_not new_user.save
+ assert_includes new_user.errors[:display_name], "has already been taken"
+ end
end
def test_email_valid