2013-06-06 10:40:10 -04:00
|
|
|
module UserNameSuggester
|
2016-01-27 03:04:11 -05:00
|
|
|
GENERIC_NAMES = ['i', 'me', 'info', 'support', 'admin', 'webmaster', 'hello', 'mail', 'office', 'contact', 'team']
|
2013-06-06 10:40:10 -04:00
|
|
|
|
2019-04-23 06:22:47 -04:00
|
|
|
def self.suggest(name_or_email, allowed_username = nil)
|
|
|
|
return unless name_or_email.present?
|
|
|
|
|
|
|
|
name = parse_name_from_email(name_or_email)
|
|
|
|
find_available_username_based_on(name, allowed_username)
|
2013-06-06 10:40:10 -04:00
|
|
|
end
|
|
|
|
|
2019-04-23 06:22:47 -04:00
|
|
|
def self.parse_name_from_email(name_or_email)
|
|
|
|
return name_or_email if name_or_email !~ User::EMAIL
|
|
|
|
|
|
|
|
# When 'walter@white.com' take 'walter'
|
|
|
|
name = Regexp.last_match[1]
|
|
|
|
|
|
|
|
# When 'me@eviltrout.com' take 'eviltrout'
|
|
|
|
name = Regexp.last_match[2] if GENERIC_NAMES.include?(name)
|
2013-06-06 10:40:10 -04:00
|
|
|
name
|
|
|
|
end
|
|
|
|
|
2019-04-23 06:22:47 -04:00
|
|
|
def self.find_available_username_based_on(name, allowed_username = nil)
|
2014-08-25 04:48:29 -04:00
|
|
|
name = fix_username(name)
|
2013-06-06 10:40:10 -04:00
|
|
|
i = 1
|
|
|
|
attempt = name
|
2019-04-23 06:22:47 -04:00
|
|
|
until attempt == allowed_username || User.username_available?(attempt) || i > 100
|
2013-06-06 10:40:10 -04:00
|
|
|
suffix = i.to_s
|
2019-04-23 06:22:47 -04:00
|
|
|
max_length = User.username_length.end - suffix.length
|
|
|
|
attempt = "#{truncate(name, max_length)}#{suffix}"
|
2013-06-06 10:40:10 -04:00
|
|
|
i += 1
|
|
|
|
end
|
2019-04-23 06:22:47 -04:00
|
|
|
until attempt == allowed_username || User.username_available?(attempt) || i > 200
|
2017-03-29 08:49:28 -04:00
|
|
|
attempt = SecureRandom.hex[1..SiteSetting.max_username_length]
|
2017-02-27 15:28:56 -05:00
|
|
|
i += 1
|
|
|
|
end
|
2013-06-06 10:40:10 -04:00
|
|
|
attempt
|
|
|
|
end
|
|
|
|
|
2014-08-25 04:48:29 -04:00
|
|
|
def self.fix_username(name)
|
|
|
|
rightsize_username(sanitize_username(name))
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.sanitize_username(name)
|
2019-04-23 06:22:47 -04:00
|
|
|
name = name.to_s
|
|
|
|
|
|
|
|
if SiteSetting.unicode_usernames
|
|
|
|
name.unicode_normalize!
|
|
|
|
else
|
|
|
|
name = ActiveSupport::Inflector.transliterate(name)
|
|
|
|
end
|
|
|
|
|
|
|
|
name.gsub!(UsernameValidator.invalid_char_pattern, '_')
|
|
|
|
name.chars.map! { |c| UsernameValidator.whitelisted_char?(c) ? c : '_' } if UsernameValidator.char_whitelist_exists?
|
|
|
|
name.gsub!(UsernameValidator::INVALID_LEADING_CHAR_PATTERN, '')
|
2016-01-20 09:37:34 -05:00
|
|
|
name.gsub!(UsernameValidator::CONFUSING_EXTENSIONS, "_")
|
2019-04-23 06:22:47 -04:00
|
|
|
name.gsub!(UsernameValidator::INVALID_TRAILING_CHAR_PATTERN, '')
|
|
|
|
name.gsub!(UsernameValidator::REPEATED_SPECIAL_CHAR_PATTERN, '_')
|
2016-01-20 09:37:34 -05:00
|
|
|
name
|
2013-06-06 10:40:10 -04:00
|
|
|
end
|
|
|
|
|
2019-04-23 06:22:47 -04:00
|
|
|
def self.rightsize_username(name)
|
|
|
|
name = truncate(name, User.username_length.end)
|
|
|
|
name.gsub!(UsernameValidator::INVALID_TRAILING_CHAR_PATTERN, '')
|
|
|
|
|
|
|
|
missing_char_count = User.username_length.begin - name.grapheme_clusters.size
|
|
|
|
name << '1' * missing_char_count if missing_char_count > 0
|
2016-02-21 17:11:52 -05:00
|
|
|
name
|
|
|
|
end
|
|
|
|
|
2019-04-23 06:22:47 -04:00
|
|
|
def self.truncate(name, max_grapheme_clusters)
|
|
|
|
clusters = name.grapheme_clusters
|
|
|
|
|
|
|
|
if clusters.size > max_grapheme_clusters
|
|
|
|
clusters = clusters[0..max_grapheme_clusters - 1]
|
|
|
|
name = clusters.join
|
|
|
|
end
|
2013-06-06 10:40:10 -04:00
|
|
|
|
2019-04-23 06:22:47 -04:00
|
|
|
while name.length > UsernameValidator::MAX_CHARS
|
|
|
|
clusters.pop
|
|
|
|
name = clusters.join
|
|
|
|
end
|
|
|
|
|
|
|
|
name
|
|
|
|
end
|
2014-03-18 18:02:33 -04:00
|
|
|
end
|