2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-09-06 04:35:29 -05:00
|
|
|
class UsernameCheckerService
|
2018-07-31 22:08:45 -05:00
|
|
|
def initialize(allow_reserved_username: false)
|
|
|
|
@allow_reserved_username = allow_reserved_username
|
|
|
|
end
|
2013-09-06 04:35:29 -05:00
|
|
|
|
|
|
|
def check_username(username, email)
|
2013-11-19 13:15:05 -06:00
|
|
|
if username && username.length > 0
|
|
|
|
validator = UsernameValidator.new(username)
|
|
|
|
if !validator.valid_format?
|
2017-07-27 20:20:09 -05:00
|
|
|
{ errors: validator.errors }
|
2013-11-19 13:15:05 -06:00
|
|
|
else
|
2016-03-03 12:01:31 -06:00
|
|
|
check_username_availability(username, email)
|
2013-11-19 13:15:05 -06:00
|
|
|
end
|
2013-09-06 04:35:29 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-03 12:01:31 -06:00
|
|
|
def check_username_availability(username, email)
|
2018-07-31 22:08:45 -05:00
|
|
|
available = User.username_available?(
|
|
|
|
username,
|
|
|
|
email,
|
|
|
|
allow_reserved_username: @allow_reserved_username
|
|
|
|
)
|
|
|
|
|
|
|
|
if available
|
2016-03-03 12:01:31 -06:00
|
|
|
{ available: true, is_developer: is_developer?(email) }
|
2013-09-06 04:35:29 -05:00
|
|
|
else
|
|
|
|
{ available: false, suggestion: UserNameSuggester.suggest(username) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-03 12:01:31 -06:00
|
|
|
def is_developer?(value)
|
|
|
|
Rails.configuration.respond_to?(:developer_emails) && Rails.configuration.developer_emails.include?(value)
|
|
|
|
end
|
|
|
|
|
2016-08-05 10:57:13 -05:00
|
|
|
def self.is_developer?(email)
|
|
|
|
UsernameCheckerService.new.is_developer?(email)
|
|
|
|
end
|
|
|
|
|
2013-09-06 04:35:29 -05:00
|
|
|
end
|