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
|
|
|
|
2015-03-26 19:10:53 -04:00
|
|
|
def self.suggest(name, allow_username = nil)
|
2013-06-06 10:40:10 -04:00
|
|
|
return unless name.present?
|
|
|
|
name = parse_name_from_email(name)
|
2015-03-26 19:10:53 -04:00
|
|
|
find_available_username_based_on(name, allow_username)
|
2013-06-06 10:40:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.parse_name_from_email(name)
|
|
|
|
if name =~ User::EMAIL
|
|
|
|
# When 'walter@white.com' take 'walter'
|
|
|
|
name = Regexp.last_match[1]
|
|
|
|
# When 'me@eviltrout.com' take 'eviltrout'
|
2016-01-27 03:04:11 -05:00
|
|
|
name = Regexp.last_match[2] if GENERIC_NAMES.include?(name)
|
2013-06-06 10:40:10 -04:00
|
|
|
end
|
|
|
|
name
|
|
|
|
end
|
|
|
|
|
2015-03-26 19:10:53 -04:00
|
|
|
def self.find_available_username_based_on(name, allow_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
|
2017-02-27 15:28:56 -05:00
|
|
|
until attempt == allow_username || User.username_available?(attempt) || i > 100
|
2013-06-06 10:40:10 -04:00
|
|
|
suffix = i.to_s
|
|
|
|
max_length = User.username_length.end - suffix.length - 1
|
|
|
|
attempt = "#{name[0..max_length]}#{suffix}"
|
|
|
|
i += 1
|
|
|
|
end
|
2017-02-27 15:28:56 -05:00
|
|
|
until attempt == allow_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)
|
2016-01-20 09:37:34 -05:00
|
|
|
name = ActiveSupport::Inflector.transliterate(name)
|
|
|
|
# 1. replace characters that aren't allowed with '_'
|
|
|
|
name.gsub!(UsernameValidator::CONFUSING_EXTENSIONS, "_")
|
|
|
|
name.gsub!(/[^\w.-]/, "_")
|
|
|
|
# 2. removes unallowed leading characters
|
|
|
|
name.gsub!(/^\W+/, "")
|
|
|
|
# 3. removes unallowed trailing characters
|
2016-02-21 17:11:52 -05:00
|
|
|
name = remove_unallowed_trailing_characters(name)
|
2016-01-20 09:37:34 -05:00
|
|
|
# 4. unify special characters
|
|
|
|
name.gsub!(/[-_.]{2,}/, "_")
|
|
|
|
name
|
2013-06-06 10:40:10 -04:00
|
|
|
end
|
|
|
|
|
2016-02-21 17:11:52 -05:00
|
|
|
def self.remove_unallowed_trailing_characters(name)
|
|
|
|
name.gsub!(/[^A-Za-z0-9]+$/, "")
|
|
|
|
name
|
|
|
|
end
|
|
|
|
|
2013-06-06 10:40:10 -04:00
|
|
|
def self.rightsize_username(name)
|
2016-02-21 17:11:52 -05:00
|
|
|
name = name[0, User.username_length.end]
|
|
|
|
name = remove_unallowed_trailing_characters(name)
|
|
|
|
name.ljust(User.username_length.begin, '1')
|
2013-06-06 10:40:10 -04:00
|
|
|
end
|
|
|
|
|
2014-03-18 18:02:33 -04:00
|
|
|
end
|